Page 1 of 1

Condition = Specific Unit Destroyed

Posted: Tue Jul 27, 2021 12:42 pm
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]

RE: Condition = Specific Unit Destroyed

Posted: Tue Jul 27, 2021 1:10 pm
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

RE: Condition = Specific Unit Destroyed

Posted: Tue Jul 27, 2021 1:12 pm
by tmoilanen
and obviously replace the side and unit name information with your info.

RE: Condition = Specific Unit Destroyed

Posted: Tue Jul 27, 2021 1:35 pm
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.

RE: Condition = Specific Unit Destroyed

Posted: Tue Jul 27, 2021 1:43 pm
by BeirutDude
if ScenEdit_GetUnit({Side='CONUS Aimpoints', Name='NAS Key West (Geographic/Aimpoint)',}) == nil then
end


?????????

RE: Condition = Specific Unit Destroyed

Posted: Tue Jul 27, 2021 2:04 pm
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!

RE: Condition = Specific Unit Destroyed

Posted: Tue Jul 27, 2021 2:58 pm
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
 

RE: Condition = Specific Unit Destroyed

Posted: Tue Jul 27, 2021 2:59 pm
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

RE: Condition = Specific Unit Destroyed

Posted: Tue Jul 27, 2021 3:04 pm
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.

RE: Condition = Specific Unit Destroyed

Posted: Tue Jul 27, 2021 3:35 pm
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.

RE: Condition = Specific Unit Destroyed

Posted: Tue Jul 27, 2021 3:58 pm
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.

RE: Condition = Specific Unit Destroyed

Posted: Tue Jul 27, 2021 4:16 pm
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.

RE: Condition = Specific Unit Destroyed

Posted: Tue Jul 27, 2021 5:09 pm
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!

RE: Condition = Specific Unit Destroyed

Posted: Tue Jul 27, 2021 6:08 pm
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.

RE: Condition = Specific Unit Destroyed

Posted: Wed Jul 28, 2021 12:18 am
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. [:)]

RE: Condition = Specific Unit Destroyed

Posted: Wed Aug 18, 2021 3:36 am
by aerosky
Hi, BeirutDude
You need use the latest version of CMO first.