trigger Lua?

All discussions & material related to Command's Lua interface

Moderators: angster, RoryAndersonCDT, michaelm75au, MOD_Command

Parel803
Posts: 941
Joined: Thu Oct 10, 2019 3:39 pm
Location: Netherlands

trigger Lua?

Post by 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
KnightHawk75
Posts: 1850
Joined: Thu Nov 15, 2018 7:24 pm

RE: trigger Lua?

Post by KnightHawk75 »

No.. in the sense you can't add trigger types like to the gui... but yes in the sense you can creatively build way of trapping things that occur, depending on what it might be.

You basically create your own trigger or sub-system for what you need based off regular timers or other things that are available. For instance unit goes airborne.. well you could write something that runs every 1 or 5 seconds checking all aircraft that you want to potentially track that for (the more narrow the more performant it will be) and keep track of their lastknown status vs checking current status, if determined to change and that change is from a non-airborne type status code to an airborne one, you run your "gone-airborne" related code on the affected units, otherwise just update the lastknown status (if even needed). If you build your sub-system for such in a scenario generic way detecting other similar "events" is less of a chore later when you come across the need.

It's quite doable, it's definitely not a simple 20 lines of code though. ;)
Parel803
Posts: 941
Joined: Thu Oct 10, 2019 3:39 pm
Location: Netherlands

RE: trigger Lua?

Post by Parel803 »

KnightHawk, Thanks for answering. I even understand the answer :-) But a I have a long way to go, LOL
with regards Gert-Jan
User avatar
TitaniumTrout
Posts: 469
Joined: Mon Oct 20, 2014 9:06 am
Location: Michigan

RE: trigger Lua?

Post by TitaniumTrout »

ORIGINAL: 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

To build on what Knighthawk said, you can use something like the " regular time" trigger and a condition to check for altitude. Or a "Unit remains in area" trigger set to a short time along with a altitude condition. You can also use the Lua part of the action to perform queries and such on the triggering unit.
Parel803
Posts: 941
Joined: Thu Oct 10, 2019 3:39 pm
Location: Netherlands

RE: trigger Lua?

Post by Parel803 »

Trout thank you for the extra info. I'm surely gonna try something (and see hoe it goes).

with regards Gert-Jan
KnightHawk75
Posts: 1850
Joined: Thu Nov 15, 2018 7:24 pm

RE: trigger Lua?

Post by KnightHawk75 »

Just keep in mind altitude isn't always accurate gauge, in that if on the ground it will contain the lat\lon elevation.
Parel803
Posts: 941
Joined: Thu Oct 10, 2019 3:39 pm
Location: Netherlands

RE: trigger Lua?

Post by Parel803 »

Rgr, thx
with regards GJ
KnightHawk75
Posts: 1850
Joined: Thu Nov 15, 2018 7:24 pm

RE: trigger Lua?

Post by KnightHawk75 »

May be of use to you,adapt function names as need.

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;
   end

Code: 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;
       end

Code: 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
 
Parel803
Posts: 941
Joined: Thu Oct 10, 2019 3:39 pm
Location: Netherlands

RE: trigger Lua?

Post by Parel803 »

thank you knightHawk for your time and function
p1t1o
Posts: 274
Joined: Mon Apr 06, 2015 11:35 am

RE: trigger Lua?

Post by p1t1o »

For aircraft, this function is a little more specific and will definitely tell you if a unit is airborne:

local unit = ScenEdit_GetUnit({Side="SIDE1", Name="UNIT1"})
if unit ~= nil and unit.airbornetime ~= '0' then
return true
else
return false
end



If UNIT1 exists and is airborne, this function returns true.
If UNIT1 exists and is on the ground, this function returns false.
If UNIT1 has been destroyed, this function returns false.
KnightHawk75
Posts: 1850
Joined: Thu Nov 15, 2018 7:24 pm

RE: trigger Lua?

Post by 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.

Parel803
Posts: 941
Joined: Thu Oct 10, 2019 3:39 pm
Location: Netherlands

RE: trigger Lua?

Post by Parel803 »

Thanks all for the help. Nice to learn more about it all.
with regards GJ
p1t1o
Posts: 274
Joined: Mon Apr 06, 2015 11:35 am

RE: trigger Lua?

Post by p1t1o »

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.


Thats good to know, thanks.

FunFact: 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 ;)
Parel803
Posts: 941
Joined: Thu Oct 10, 2019 3:39 pm
Location: Netherlands

RE: trigger Lua?

Post by 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
KnightHawk75
Posts: 1850
Joined: Thu Nov 15, 2018 7:24 pm

RE: trigger Lua?

Post by KnightHawk75 »

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

Other bad things can happen when doing RTB'ing at the wrong time if already on the ground, like it can get stuck in a loop (only way to clear it is to move the unit to another base and move it back to clear it).
KnightHawk75
Posts: 1850
Joined: Thu Nov 15, 2018 7:24 pm

RE: trigger Lua?

Post by KnightHawk75 »

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
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.
Assuming 16 aircraft it could look something like this.

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
 
Parel803
Posts: 941
Joined: Thu Oct 10, 2019 3:39 pm
Location: Netherlands

RE: trigger Lua?

Post by Parel803 »

Thank you for your time and anrswer, gonna try tonight. Don't wanna go to a tanker but keep em in air a bit longer. Not sure if one is shot done and and I see a message that it didn't went through that could cause problems inn the game. In this case I wanna keep the UAS longer in the air than the DB allows.
So I though I might need a condition that they still exist.
thx again, with regards GJ
User avatar
michaelm75au
Posts: 12457
Joined: Sat May 05, 2001 8:00 am
Location: Melbourne, Australia

RE: trigger Lua?

Post by michaelm75au »

I had played around with creating a dynamic Lua script trigger, but it would need to run every game cycle which may drag down performance if it was complicated.
Using a time interval trigger with a condition seemed to cover most test cases I tried.
Michael
Parel803
Posts: 941
Joined: Thu Oct 10, 2019 3:39 pm
Location: Netherlands

RE: trigger Lua?

Post by Parel803 »

Michael, thx for yor time and advice
with regards GJ
Parel803
Posts: 941
Joined: Thu Oct 10, 2019 3:39 pm
Location: Netherlands

RE: trigger Lua?

Post by Parel803 »

Knighthawk I change the myunit inn line 9 for unit. Is that correct?
Looks like it's working,
thx again, GJ
Post Reply

Return to “Lua Legion”