quote:
Is there a way to have all aircraft on a mission return to base programmatically?
Yes, very simply.
Basically same as this http://www.matrixgames.com/forums/fb.asp?m=4988001 with the "action" being different.
The snippet would be this if you know the mission name ahead of time.
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.
-- 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
--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
--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);