LUA - Detect if multiple units have been damaged or destroyed

All discussions & material related to Command's Lua interface

Moderators: michaelm75au, angster, RoryAndersonCDT, MOD_Command

Post Reply
User avatar
killjoy73au
Posts: 31
Joined: Mon Jul 10, 2017 5:56 am

LUA - Detect if multiple units have been damaged or destroyed

Post by killjoy73au »

G'day all.

So despite my best detective work i've been unable to find a feasible way to have an action occur when multiple units have been destroyed within the same event, or find a LUA code to detect if a unit has been destroyed.

From what I gather, if you have multiple triggers in an event, the event doesn't wait for all triggers to have fired, it only requires one of the triggers to fire before an action (nothing in the conditions helps with this either) fires.

What I need is for an action to fire off only after multiple enemy units have been destroyed.

So say I have 3 enemy surface units that need to be sunk, I need a message to pop up once those 3 specific units have been sunk.

A bit of a visualisation of what I would ideally like to happen (I'm aware there is no 'and' function for triggers though):


EVENT - enemy SAG sunk

Triggers:
ship 1 destroyed
and
ship 2 destroyed
and
ship 3 destroyed

Action:
Message - enemy SAG has been sunk



Cheers.




User avatar
michaelm75au
Posts: 12457
Joined: Sat May 05, 2001 8:00 am
Location: Melbourne, Australia

RE: LUA - Detect if multiple units have been damaged or destroyed

Post by michaelm75au »

Currently multiple triggers in same event are 'OR' checks - any one of them will trigger the action.
What you need to do for the example is to create a 'counter' to keep track of how many units have been destroyed.
Basic example

Event: Unit Destroyed
Trigger: unit destroyed
Action:
lastCount = ScenEdit_GetKeyValue('ShipsSunk');
lastCountN = tonumber(lastCount);
if lastCountN == nil then
lastCountN = 0;
end
lastCountN = lastCountN +1;
ScenEdit_SetKeyValue('ShipsSunk', tostring(lastCountN));
if lastCountN > 3 then
ScenEdit_MsgBox ("SAG sunk", 1)
end
Michael
User avatar
killjoy73au
Posts: 31
Joined: Mon Jul 10, 2017 5:56 am

RE: LUA - Detect if multiple units have been damaged or destroyed

Post by killjoy73au »

Thanks very much for reply, I'm looking more to call a script to check if specific units still exist.

I tried this script that gets called every 15 seconds by a trigger...

if ScenEdit_GetUnit({side="PLAAF", unitname="Anshan DD-101"}) == nil
then ScenEdit_SpecialMessage('United States Navy','Anshan Destroyed')
ScenEdit_SetEvent('PLAN SAG - Destroyed', {IsActive= false})
end

I got this working. Now what i'm looking to do is have this script check for multiple different units, resulting in just the one msg.


Okay I got it working. Once both units are destroyed the message pops up and the trigger/event stop checking. It doesn't look overly professional but it works damnit!

if ScenEdit_GetUnit({side="PLAN", unitname="Anshan DD-101"}) == nil
and
ScenEdit_GetUnit({side="PLAN", unitname="Changchun DD-103"}) == nil
then ScenEdit_SpecialMessage('United States Navy','Anshan and Changchun Destroyed')
ScenEdit_SetEvent('PLAN SAG - Destroyed', {IsActive= false})
end
User avatar
tjhkkr
Posts: 2431
Joined: Wed Jun 02, 2010 11:15 pm
Contact:

RE: LUA - Detect if multiple units have been damaged or destroyed

Post by tjhkkr »

Yes, this was good!
Remember that the evil which is now in the world will become yet more powerful, and that it is not evil which conquers evil, but only love -- Olga Romanov.
Post Reply

Return to “Lua Legion”