Condition = Specific Unit Destroyed

All discussions & material related to Command's Lua interface

Moderators: RoryAndersonCDT, michaelm75au, angster, MOD_Command

Post Reply
User avatar
BeirutDude
Posts: 2758
Joined: Sat Apr 27, 2013 9:44 am
Location: Jacksonville, FL, USA

Condition = Specific Unit Destroyed

Post by BeirutDude »

Ok, guys, I'm old and stupid and I admit it but this should be simple. As a CONDITION for an event I want to test if a specific unit is destroyed. I really don't understand why this isn't a base option for conditions but it isn't.

So how do I test for...
Side = CONUS aimpoints
Specific Unit = NAS Key West
Status = Destroyed

I just want to test for it to be destroyed for subsequent actions to be taken. Sure it is simple but I'm going through the Lau docs and my head is spinning.

"One small step for a young kid who loves programing, one giant leap for a old dude who sucks at it!" [:D]
"Some people spend an entire lifetime wondering if they made a difference. The Marines don't have that problem."
PRESIDENT RONALD REAGAN, 1985

I was Navy, but Assigned TAD to the 24th MAU Hq in Beirut. By far the finest period of my service!
tmoilanen
Posts: 78
Joined: Wed Oct 19, 2011 4:28 pm

RE: Condition = Specific Unit Destroyed

Post by tmoilanen »

Hi BeirutDude - if you're just checking to see if the unit still exists the script below should work.

You can replace the AddWeaponToUnitMagazine part with whatever action you're looking to perform.


if ScenEdit_GetUnit({Side='Taiwan', Name='Kinmen AP Ammo 1',}) ~= nil then

ScenEdit_AddWeaponToUnitMagazine({unitname='Kinmen AP Ammo 1', wpn_dbid='683', number=64}) -- AGM-114K Hellfire II
ScenEdit_AddWeaponToUnitMagazine({unitname='Kinmen AP Ammo 1', wpn_dbid='1929', number=112}) -- HYDRA 70mm Rocket


end
tmoilanen
Posts: 78
Joined: Wed Oct 19, 2011 4:28 pm

RE: Condition = Specific Unit Destroyed

Post by tmoilanen »

and obviously replace the side and unit name information with your info.
User avatar
BeirutDude
Posts: 2758
Joined: Sat Apr 27, 2013 9:44 am
Location: Jacksonville, FL, USA

RE: Condition = Specific Unit Destroyed

Post by BeirutDude »

I just want to check to see if the unit has been destroyed as a condition. If so in the actions I want to send a message, if not I want the event to fil.
"Some people spend an entire lifetime wondering if they made a difference. The Marines don't have that problem."
PRESIDENT RONALD REAGAN, 1985

I was Navy, but Assigned TAD to the 24th MAU Hq in Beirut. By far the finest period of my service!
User avatar
BeirutDude
Posts: 2758
Joined: Sat Apr 27, 2013 9:44 am
Location: Jacksonville, FL, USA

RE: Condition = Specific Unit Destroyed

Post by BeirutDude »

if ScenEdit_GetUnit({Side='CONUS Aimpoints', Name='NAS Key West (Geographic/Aimpoint)',}) == nil then
end


?????????
"Some people spend an entire lifetime wondering if they made a difference. The Marines don't have that problem."
PRESIDENT RONALD REAGAN, 1985

I was Navy, but Assigned TAD to the 24th MAU Hq in Beirut. By far the finest period of my service!
User avatar
BeirutDude
Posts: 2758
Joined: Sat Apr 27, 2013 9:44 am
Location: Jacksonville, FL, USA

RE: Condition = Specific Unit Destroyed

Post by BeirutDude »

[/quote]
if ScenEdit_GetUnit({Side='CONUS Aimpoints', Name='NAS Key West (Geographic/Aimpoint)',}) == nil then
end

No that didn't work. Thanks for the help, guess I'm just stupid!
"Some people spend an entire lifetime wondering if they made a difference. The Marines don't have that problem."
PRESIDENT RONALD REAGAN, 1985

I was Navy, but Assigned TAD to the 24th MAU Hq in Beirut. By far the finest period of my service!
User avatar
Kushan04
Posts: 703
Joined: Tue Jun 28, 2005 9:27 pm

RE: Condition = Specific Unit Destroyed

Post by Kushan04 »

Conditions have to return a true/false.

Example:

Code: Select all

 local mission = ScenEdit_GetMission('Test', 'Test')
 
 if mission.isactive == true then
  return true
 else
  return false
 end
 

In your case I think it would look something like this:

Code: Select all

 local unit = ScenEdit_UnitX()
 
 if unit.name == 'NAS Key West' then
  return true
 else
  return false
 end
 
User avatar
BeirutDude
Posts: 2758
Joined: Sat Apr 27, 2013 9:44 am
Location: Jacksonville, FL, USA

RE: Condition = Specific Unit Destroyed

Post by BeirutDude »

Would this work? Use a counter?

Can I set up a counter called "targetshit" in a Lua script in an action

local targetshit = 0

Then set up another Action Lua script that keeps track of each target hit when an event fires

Local targetshit = targetshit + 1


Then for each message I want to send a Lua script in the Conditions to see if the condition is met...

if targetshit == 1 then
return true
else
return false
end
"Some people spend an entire lifetime wondering if they made a difference. The Marines don't have that problem."
PRESIDENT RONALD REAGAN, 1985

I was Navy, but Assigned TAD to the 24th MAU Hq in Beirut. By far the finest period of my service!
User avatar
Kushan04
Posts: 703
Joined: Tue Jun 28, 2005 9:27 pm

RE: Condition = Specific Unit Destroyed

Post by Kushan04 »

Updated my post with what I think your condition would look like.

Why does using the

Code: Select all

 block add so many extra lines?
 
 Edit: Instead of using a condition you could also just use a specific damaged/destroyed trigger.
User avatar
BeirutDude
Posts: 2758
Joined: Sat Apr 27, 2013 9:44 am
Location: Jacksonville, FL, USA

RE: Condition = Specific Unit Destroyed

Post by BeirutDude »

I so frigging confused I'm ready to trash this scenario, this should be so easy!

So look here is my problem

some time after an event (the nuclear destruction of a facility), say 5 10 or 15 minutes later I want to send the player a message. So I need to use a "condition" that somehow keeps track of the event occurring and only a Lua script will do that. I actually think just a simple counter might work better but I can't event write a frigging "if statement" and all the ones I call up online are so frigging complicated I can't follow them. That's why I gave up on coding my mind doesn't work that way!

I think I just need a simple if statement to test if the local variable 'targetshit' is equal to a certain value or not and each time an event is triggered add 1 to it.
"Some people spend an entire lifetime wondering if they made a difference. The Marines don't have that problem."
PRESIDENT RONALD REAGAN, 1985

I was Navy, but Assigned TAD to the 24th MAU Hq in Beirut. By far the finest period of my service!
User avatar
BeirutDude
Posts: 2758
Joined: Sat Apr 27, 2013 9:44 am
Location: Jacksonville, FL, USA

RE: Condition = Specific Unit Destroyed

Post by BeirutDude »

Kushan04 Thanks for your help! I think I figured it out (I hope, man I have a headache!)

Lua Script on Scenario load to set the counter to 0 with each load

local targetshit = 0

Advance the counter by 1 with each event

targetshit = targetshit + 1

Conditional Test to see if a message should be sent...

if (targetshit == 1) then
return true
else
return false
end


I wouldn't be here without your IF statement to mimic/follow TY!!!!

BTW I think this scenario might be one you would enjoy when I am done. I've been working for weeks on just the nuclear aspect and that is just the chrome.
"Some people spend an entire lifetime wondering if they made a difference. The Marines don't have that problem."
PRESIDENT RONALD REAGAN, 1985

I was Navy, but Assigned TAD to the 24th MAU Hq in Beirut. By far the finest period of my service!
User avatar
BeirutDude
Posts: 2758
Joined: Sat Apr 27, 2013 9:44 am
Location: Jacksonville, FL, USA

RE: Condition = Specific Unit Destroyed

Post by BeirutDude »

Nope that won't work either because the event will keep sending the message over and over.

But if each message had it's on variable (targethit01, targethit02, etc) and then when the event is triggered that variable goes back to 0 that might work. Just toggle it on for one event.
"Some people spend an entire lifetime wondering if they made a difference. The Marines don't have that problem."
PRESIDENT RONALD REAGAN, 1985

I was Navy, but Assigned TAD to the 24th MAU Hq in Beirut. By far the finest period of my service!
User avatar
BeirutDude
Posts: 2758
Joined: Sat Apr 27, 2013 9:44 am
Location: Jacksonville, FL, USA

RE: Condition = Specific Unit Destroyed

Post by BeirutDude »

Nope that doesn't work. I did a test and for some reason it deactivates the event. This shouldn't be this hard!
"Some people spend an entire lifetime wondering if they made a difference. The Marines don't have that problem."
PRESIDENT RONALD REAGAN, 1985

I was Navy, but Assigned TAD to the 24th MAU Hq in Beirut. By far the finest period of my service!
User avatar
BeirutDude
Posts: 2758
Joined: Sat Apr 27, 2013 9:44 am
Location: Jacksonville, FL, USA

RE: Condition = Specific Unit Destroyed

Post by BeirutDude »

OK, just got the toggle idea working. Defining the variable as "Local" was screwing things up. I have the messages in my test scenario firing.
"Some people spend an entire lifetime wondering if they made a difference. The Marines don't have that problem."
PRESIDENT RONALD REAGAN, 1985

I was Navy, but Assigned TAD to the 24th MAU Hq in Beirut. By far the finest period of my service!
KnightHawk75
Posts: 1590
Joined: Thu Nov 15, 2018 7:24 pm

RE: Condition = Specific Unit Destroyed

Post by KnightHawk75 »

some time after an event (the nuclear destruction of a facility), say 5 10 or 15 minutes later I want to send the player a message. So I need to use a "condition" that somehow keeps track of the event occurring and only a Lua script will do that. I actually think just a simple counter might work better but I can't event write a frigging "if statement" and all the ones I call up online are so frigging complicated I can't follow them. That's why I gave up on coding my mind doesn't work that way!

Ok so using a Condition is certainly possible, as Kushan said condition's need a return value:
Setup a function for yourself called something like the following in your OnSceneLoad stuff.

Code: Select all

--This function returns true only if a unit does not exist.
 function DoesThisUnitNotExist(theSide,theName)
   local retval,u = pcall(ScenEdit_GetUnit, {Side=theSide, Name=theName}); --get the unit.
   if (retval ==true) and u ~=nil then --did the call succeed, did it return a unitwrapper?
     return false; --unit exists, call succeeded and it returned something.
   else 
     return true; --unit does not exists or the GetUnit call failed for another reason.
   end 
 end
 
 
Condition Name (I'm assuming the action is a display message action): Does SomeUnitname Exist?

Code: Select all

return DoesThisUnitNotExist("SomeSide","TheUnitName"); -- calls the function and returns for the condition what comes back from the function.
 

There are 1/2 a dozen different ways to do what you want, depending on how you want said message displayed, just find what works best for you\the scene, don't get frustrated we can walk you through any of this. For instance you didn't really mentioned if the message you want displayed is per-unit destroyed, depends on how many of particular units are destroyed, or only displayed when say all of a list of units are destroyed. I'm assuming it's a simple 'if unit1 destroyed then display prepared message 1' ... 'if unit2 destroyed then display prepared message 2' etc since that avoids lua.
the event will keep sending the message over and over.
-- Not if you disable it after running the first time.

OK, just got the counter idea working. Defining the variable as "Local" was screwing things up. I have the messages in my test scenario firing.
If you go that route don't forget you likely now have to save the state of those variables now upon every change to them, to the key store, and load them up\restore them on scene load, if you want saves to work reliably.

If it helps you I attached a very simple scene Red and Blue, 4 targets exist on blue, 1 sub to nuke them on Red.
It uses the above code + conditions.
Every minute it checks if each of the monitored units exist, if they do not, you will get a specific message for each one ONCE, and only ONCE. It involves NO variables that need to be tracked, and instead relies on disabling the events after the messages is displayed.
It's not how I would necessarily do things, but it does show how to avoid Lua to the maximum extent possible, and removes having to manage any state at the cost of more events, more conditions, hard coding, and otherwise unnecessary duplication. It assume you actually want a message per unit involved, and not one message only after all\some are destroyed though that is certainly doable by changing the condition and message.

MessageOnIntervalShowOnceMinLuaSample.zip (build 1147.29 | db3k 489)
Just load it and have the sub destroy the target(s) you want and watch as you get the messages and then the events involved disable themselves, make sure you watch from side red so that you actually get the messages.
Why does using the

Code: Select all

 block add so many extra lines? [/quote]
 idk but yeah it's annoying. [:)]
Attachments
MessageOnI..uaSample.zip
(9.24 KiB) Downloaded 4 times
User avatar
aerosky
Posts: 42
Joined: Tue Jul 20, 2021 6:38 am
Location: USA

RE: Condition = Specific Unit Destroyed

Post by aerosky »

Hi, BeirutDude
You need use the latest version of CMO first.
In the future, no space, no win
Post Reply

Return to “Lua Legion”