
I'm now making (or trying to) actions in the events with Lua.
Is there a way to make another trigger than the ones provided?
Like when a unit goes airborne? But many more to think of I guess.
with regards Gert-Jan
Moderators: angster, RoryAndersonCDT, michaelm75au, MOD_Command


 But a I have a long way to go, LOL
 But a I have a long way to go, LOLORIGINAL: Parel803
With the risk this might be a stupid Question I'm asking it anyway
I'm now making (or trying to) actions in the events with Lua.
Is there a way to make another trigger than the ones provided?
Like when a unit goes airborne? But many more to think of I guess.
with regards Gert-Jan
Code: Select all
-- Function to return true if a it's very likely a aircraft unit is currently in the air.
 -- takes in unit wrapper of unit to check
 function gKH.Unit: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
  -- Function to return true if a it's very likely a ship\sub unit is docked at a port.
   -- takes in unit wrapper of unit to check
   -- does not account for edge cases where alt > 0 while in transition to port inside the override arcs of a port.
   function gKH.Unit:isShipSubAtDock(u)
     if (u.condition_v == "Docked" or u.altitude > 0.0) or u.condition_v == "Docking" or u.condition_v == "DeployingUnderway" then
       return true;
     end
     return false;
   endCode: Select all
   -- Function to return true if a given unit wrapper currently contains the guid of a specific unit.
   -- Useful when checking if something is currently "at base" or not, and for other purposes.
   -- input u: The unit wrapper to check.
   -- input thetype: The string type unit to check "Aircraft or Boats".
   -- input theguid: The guid of the unit to search for.
   -- returns: true|false
   function gKH.Unit:checkForHostedUnitGuid(u,theType,theGuid)
     fn = "gKH.Unit:checkForHostedUnitGuid(): ";
     if (theType == nil) or type(theType) ~= "string" then theType = "Aircraft" end
     if (theGuid == nil) or type(theGuid) ~= "string" or string.len(theGuid) < 20 then print(fn .. "Missing guid. aborting."); return false; end
     if(u~=nil) then 
       local tmptable;
       if (u.embarkedUnits ~= nil) and theType == "Aircraft" then
         tmptable = u.embarkedUnits.Aircraft;
       elseif (u.embarkedUnits ~= nil) and theType == "Boats" then
         tmptable = u.embarkedUnits.Boats;
       else
         print(fn .. "Could not determine search type, try Aircraft or Boats. Aborting.")
         return false;
       endCode: Select all
       if (tmptable ~=nil ) and #tmptable > 0 then
         for k = 1, #tmptable do
           if(tmptable[k] == theGuid) then return true; end --found match
         end
       else
           return false; -- no units to check.
       end
       return false; -- checked but no matches.
     else
       print(fn .. "Error: Unit wrapper missing, returning false")
       return false;
     end
   end
 ORIGINAL: KnightHawk75
@p1t1o - "and will definitely tell you if a unit is airborne".
It's useful but doesn't cover the case of aircraft who just landed and are on the ground in "completing landing phase", or "taxiing to parking" phase (which in total lasts about 2m:30sec combined), during which it would report the airborne time (making you think it's still in the air) and not 0. I mention this in case you run into a case where that matters, depending on what's being done after the determination it may or may not.

I found that code after needing it because if you use Lua to give an RTB order to a unit on the ground, it gets teleported halfway around the globe
Do you just want to fill up new one's that are to a certain point with fuel or you trying to trigger them to goto a tanker? In any case no you don't need 16 actions.ORIGINAL: Parel803
I try to use what I learned but guess this is not possible?
local unit = ScenEdit_GetUnit({side='Redland',name='Sting #'..i})
if unit ~= nil then
return true
else
return false
end
Like to check 16 units on there fuel. The action is to get new fuel.
Or do I make 16 events for each unit to check and refuel?
with regards Gert-Jan
Code: Select all
 local unit;
 for i=1,16 do  --do the following 16 times incrementing i during each iteration.
   unit = ScenEdit_GetUnit({side='Redland',name='Sting #'..i})
   if unit ~= nil then
     local newfuel = unit.fuel --get a copy of the fuel table.
     for k,v in pairs(newfuel) do -- check ever single fuel entry.
       if (v.current / v.max) <= 50.0 then -- just an example if less then 50% then refuel.
          v.current = v.max -- update our copy's current property to be that of the max value.
          unit.fuel = newfuel --replace old fuel table with new data. 
       end
     end
   else 
     --print('could not find or get unit. ' .. 'Sting #'..i)
   end
   unit = nil;
 end