[Answered] Return to Base

Post bug reports and ask for game support here.

Moderator: MOD_Command

Post Reply
kritter
Posts: 350
Joined: Mon Dec 03, 2007 8:23 pm

[Answered] Return to Base

Post by kritter »

Is there a way to have all aircraft on a mission return to base programmatically? On a trigger I want the mission to end and all aircraft return to base. I can deactivate the mission but I do not see how to get all the aircraft to return to base. The ScenEdit_SetUnit command looks like it will return one aircraft but not all. The other problem is I do not know which aircraft are on the mission when the trigger happens.

Any Ideas?
KnightHawk75
Posts: 1557
Joined: Thu Nov 15, 2018 7:24 pm

RE: Return to Base

Post by KnightHawk75 »

Is there a way to have all aircraft on a mission return to base programmatically?
Yes, very simply.
Basically same as this fb.asp?m=4988001 with the "action" being different.

The snippet would be this if you know the mission name ahead of time.

Code: Select all

local missionX = ScenEdit_GetMission("TheSideTheMissionIsContainedOn","MissionNameHereX")
 if (missionX ~=nil) and missionX.unitlist ~=nil then
   for key,value in pairs(missionX.unitlist) do  --each value is just a guid in this case. 
     ScenEdit_SetUnit({guid=value, RTB=true}); -- issue RTB on each unit in the mission. 
   end
 end
 
Extended answer:
If there are mission members that could be on the ground when this is issued though you should check if airborne first. Here is some more full featured code that contains error trapping and is in more reusable form, these are local function samples but you can\should make them global if reusing them a bunch, it's way more code but it also will not cause any popup errors if something goes wrong or if called with bad data, and will not issue RTB order to units on the ground which can cause issues sometimes.

Code: Select all

 -- function to always find out if a unit is airborne regardless of elevation.
 local function isAircraftAirborne(u)
     if ((u.condition_v == "Airborne") or u.condition_v == "RTB") or u.condition_V == "Landing_PreTouchdown" or
      u.condition_v == "ManoeuveringToRefuel" or u.condition_v == "Refuelling" or u.condition_v == "OffloadingFuel" or 
      u.condition_v == "DeployingDippingSonar" or u.condition_v == "EmergencyLanding" or  u.condition_v == "BVRAttack" or 
      u.condition_v == "BVRCrank" or u.condition_v == "Dogfight" or u.condition_v == "BVRDrag" or u.condition_v == "TransferringCargo" then
       return true;
     end
     return false;
 end

Code: Select all

 --sample to issue RTB order, for safety only on airborne aircraft.
 local function issueRTBOrderOnAircraft(value)
   local retval,u = pcall(ScenEdit_GetUnit,{guid=value}) -- go get this unit's wrapper.
   if ((retval == true) and u ~= nil) then
     if (u.type == 'Aircraft') and isAircraftAirborne(u) == true then --check if airborne first.
       retval,u = pcall(ScenEdit_SetUnit,{guid=u.guid, RTB=true}); -- issue RTB on each unit in the mission. 
       if (retval == true) and u ~=nil then --remove this if you don't want it logged
         print("RTB order has been given to unit " .. tostring(u.name));
       else
         print("Failed to RTB unit " .. tostring(u.name) .. ' ' ..tostring(u));
       end
     else 
       --print("Skipping, unit is not an aircraft or not airborne. " .. tostring(u.name));
     end
   else
     print("There was a problem obtaining unit with guid " ..tostring(value));
   end
   u,retval = nil,false;
 end
 

Code: Select all

--this function loops though all members of a mission and calls a function for-each.
 local function forEachMissionMemberDo(missionside,missionname,func1)
   if missionside ==nil or missionname==nil or func1 == nil then print("invalid parameters, aborted.");end
   local retval,missionX = pcall(ScenEdit_GetMission,missionside,missionname);
   if((retval == true) and missionX ~=nil) and missionX.unitlist ~=nil then
     for key,value in pairs(missionX.unitlist) do  --each value is just a guid in this case.
       func1(value) --pass that value onto whatever function was named.
     end
   else
     print("Could not obtain the list of members for mission " ..tostring(missionname) .. " on side " .. tostring(missionside));
   end
 end
 
 -- call the above.
 forEachMissionMemberDo('TheSideNameHere','TheMissionNameHere',issueRTBOrderOnAircraft);

kritter
Posts: 350
Joined: Mon Dec 03, 2007 8:23 pm

RE: Return to Base

Post by kritter »

Thanks for the help, I was hoping there was a simpler way than running through each aircraft like a set mission with a RTB parameter.

Thanks
Post Reply

Return to “Tech Support”