trigger Lua?

All discussions & material related to Command's Lua interface

Moderators: angster, RoryAndersonCDT, michaelm75au, MOD_Command

KnightHawk75
Posts: 1850
Joined: Thu Nov 15, 2018 7:24 pm

RE: trigger Lua?

Post by KnightHawk75 »

ORIGINAL: Parel803

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

yeah sorry typo in line 9 "myunit" should read "unit"! I will edit original.
Parel803
Posts: 941
Joined: Thu Oct 10, 2019 3:39 pm
Location: Netherlands

RE: trigger Lua?

Post by Parel803 »

no sorry, no problem. Like to play with it.
regards GJ
Parel803
Posts: 941
Joined: Thu Oct 10, 2019 3:39 pm
Location: Netherlands

RE: trigger Lua?

Post by Parel803 »

Follow up Q: I have a unit with a boat in docking bay. When the boat is status "Docked" and Time to Ready is "Ready".
Made event with a time trigger and a message action. Want this to fire in this status/condition.
I came up with:
local u1 = ScenEdit_GetUnit({guid='50PVO4-0HMD9K449JTMQ'})
if (u1.condition_v == 'Docked' then
return true
else
return false
end

When Readying 25min this
local u1 = ScenEdit_GetUnit({guid='50PVO4-0HMD9K449JTMQ'})
print (u.unitstate)
print (u.condition)
print (u.condition_v)
gives me:
OnPlottedCourse
Airborne
Airborne

When Docked and Ready I get same results.

Got this from hosted units:
1 50PVO4-0HMD9K449JTMQ - Expeditionary Warfare USV #86
2 50PVO4-0HMD9K449JTM0 - Expeditionary Warfare USV #85

also checked: https://www.matrixgames.com/forums/tm.a ... n&#4819613

Wondering how to make a condition that geves me a reslut to fire the message when boat docked and ready.
I tried several options, haven't found it yet.

best regards GJ
KnightHawk75
Posts: 1850
Joined: Thu Nov 15, 2018 7:24 pm

RE: trigger Lua?

Post by KnightHawk75 »

Wondering how to make a condition that geves me a reslut to fire the message when boat docked and ready.

Code: Select all

 ---[some global func in this example but doesn't have to be]
 ---function returns true if a passed in unit is not operating, is 'Docked' state and is also fully ready.
 ---@param u unitWrapper: unit wrapper to operate on.
 ---@return boolean
 ---@requires:  CMO build 1147.34+  u.readytime_v returning digit is new.
 function isUnitDockedAndReady(u)
   if ((u ~=nil) and u.isOperating == false) and u.condition_v == 'Docked' and u.readytime_v == 0 then return true; end
   return false;
 end

Code: Select all

 ---[in case you need a list of units that qualify and not just a hard-coded one. included here for the hell of it ignore otherwise]
 ---@param hosting CMO__UnitWrapper - the host.
 ---@return table - table of CMO__UnitWrappers one entry for each available docked unit.
 function buildlistOfWrappersDockedAndReadyForHost(h)
   local rettbl = {};
   if ( h ~= nil) and #h.embarkedUnits.Boats > 0 then
     local r,u;
     for i=1,#h.embarkedUnits.Boats do
       r,u = pcall(SE_GetUnit,{guid=h.embarkedUnits.Boats[i]})
       if ((r) and u~=nil) and isUnitDockedAndReady(u) then table.insert(rettbl,u); end
       r,u = nil,nil;
     end
   end
   return rettbl;
 end
 

Code: Select all

 ---[condition]
 local r,u = pcall(ScenEdit_GetUnit,{guid='50PVO4-0HMD9K449JTMQ'}); --get unit, suppress errors on fail.
 if ((r) and u~=nil) and isUnitDockedAndReady(u) then --success,not nil, and is docked and ready..
   return true; -- Condition met
 else 
   return false; --was problem getting the unit or it was simply not docked and ready.
 end
 
Tested with a MOB launching and recovering 7m RHIBs without issue.
Parel803
Posts: 941
Joined: Thu Oct 10, 2019 3:39 pm
Location: Netherlands

RE: trigger Lua?

Post by Parel803 »

Thanks (again), Something to do tomorrow :-)
best regards GJ

Got it for one Docked unit working, great.

Not sure if I understand the list of units correctly? It is still all very complex for my peanut brain.
Should I get true if on of the boats is docked&ready? If so, how to call it?

best regards GJ
KnightHawk75
Posts: 1850
Joined: Thu Nov 15, 2018 7:24 pm

RE: trigger Lua?

Post by KnightHawk75 »

Not sure if I understand the list of units correctly?

You don't need it, remove it, it's not actually used by what you specifically asked for.

I provided it because I though you might run into a situation with what you may be doing where you might need to return a list of boat units (full wrappers) that are ready and docked on a particular ship/host.

If you're just asking...You would call it with a host unit, so in my scene it was a mobile offshore base, it had 3 RHIBs on it. If for example say I wanted a list of unitwrappers of which of those were ready and docked I could do...

Code: Select all

 local mob = SE_GetUnit({guid=guidofmob});
 local t = buildlistOfWrappersDockedAndReadyForHost(mob) 
 --t will now contain a table of unit wrappers for all unit that were docked and ready.
 if #t > 0 then -- there are 1 or more that are ready..
   local tmpstring = ''; firstrun=true;
   for entryNumber,unitWrapper in pairs(t) do
      --- do something... maybe make a list of names like here 
      --- or maybe do something to each or for each.. 
     if firstrun == true then tmpstring = unitWrapper.name; firstrun=false;
     else tmpstring = tmpstring .. ' , ' .. unitWrapper.name;    
     end
   end
   ScenEdit_SpecialMessage('playerside','<P> The following boats are now available for launch on unit ' .. mob.name .. ':</P><P>' .. tmpstring .. '</P>');
 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 patience.

Very usefull to attend my player that he/she got more to play with.

regards GJ
Post Reply

Return to “Lua Legion”