LUA for determining the shooter.

Take command of air and naval assets from post-WW2 to the near future in tactical and operational scale, complete with historical and hypothetical scenarios and an integrated scenario editor.

Moderator: MOD_Command

Post Reply
Exilemnord
Posts: 12
Joined: Tue May 31, 2022 4:33 pm

LUA for determining the shooter.

Post by Exilemnord »

I've got a scenario with a lot of NEUTRAL merchant ships and planes and two side's (Let's say GOOD side and BAD side). I want to create an event where if one of the merchants is hit with a weapon it removes points from the side that shot it (and only that side.) So I need to determine the shooter. The Event trigger's if the neutral is damaged. The action is to remove points from the side that shot. So I'm looking at LUA conditions that check the shooter's side. My guess is this involves SceneEdit_UnitY() or UnitX(). (I believe it's Y that indicates the unit causing the trigger (the damaged merchant.)

I've tried this:

local unit = SceneEdit_UnitY()
print (unit.side)
If unit.side == "Good"
return true
else
return false
end

and inevitably i get the error attempt to call a nil value (Global 'SceneEdit_UnitY'). What am I doing wrong? Am I barking up the wrong LUA script? Using it incorrectly? Is there an easier way to do this? I can't find a solve for this anywhere I can think of.

Thanks in advance.
BDukes
Posts: 2450
Joined: Wed Dec 27, 2017 12:59 pm

Re: LUA for determining the shooter.

Post by BDukes »

You're missing the then I think

local unit = SceneEdit_UnitY()
print(unit.side)
if unit.side == "Good" then
return true
else
return false
end
Don't call it a comeback...
Exilemnord
Posts: 12
Joined: Tue May 31, 2022 4:33 pm

Re: LUA for determining the shooter.

Post by Exilemnord »

sorry. Have it. Just mistyped
Exilemnord
Posts: 12
Joined: Tue May 31, 2022 4:33 pm

Re: LUA for determining the shooter.

Post by Exilemnord »

Also wondering, as part of this question, if it isn't better to put all the LUA (the UnitY() stuff, if that's even the correct script) in the action and not have a condition. So NEUTRAL is hit, action would be if the attacker side is GOOD then Good loses points, if the attacker side is BAD then bad loses points. Conditions being Boolean seem both limited and an extra step?
BDukes
Posts: 2450
Joined: Wed Dec 27, 2017 12:59 pm

Re: LUA for determining the shooter.

Post by BDukes »

BDukes wrote: Wed Aug 16, 2023 3:45 pm You're missing the then I think

local unit = SceneEdit_UnitY()
print(unit.side)
if unit.side == "Good" then
return true
else
return false
end
Got it you've got an extra e in SceneEdit_UnitY()

Syntax is ScenEdit_UnitY()
Don't call it a comeback...
BDukes
Posts: 2450
Joined: Wed Dec 27, 2017 12:59 pm

Re: LUA for determining the shooter.

Post by BDukes »

Exilemnord wrote: Wed Aug 16, 2023 5:33 pm Also wondering, as part of this question, if it isn't better to put all the LUA (the UnitY() stuff, if that's even the correct script) in the action and not have a condition. So NEUTRAL is hit, action would be if the attacker side is GOOD then Good loses points, if the attacker side is BAD then bad loses points. Conditions being Boolean seem both limited and an extra step?
I haven't used many conditions so the jury hasn't even filed in on that one. An action works fine. That is what I tested the above with.

Mike
Don't call it a comeback...
Exilemnord
Posts: 12
Joined: Tue May 31, 2022 4:33 pm

Re: LUA for determining the shooter.

Post by Exilemnord »

So that Lua script ...ScenEdit_UnitY()...produced a side of shooter for you? I get nothing but Nil. Certainly ScenEdit_UnitX() will give me who was shot. But I just can't get the reverse.
BDukes
Posts: 2450
Joined: Wed Dec 27, 2017 12:59 pm

Re: LUA for determining the shooter.

Post by BDukes »

Exilemnord wrote: Wed Aug 16, 2023 10:26 pm So that Lua script ...ScenEdit_UnitY()...produced a side of shooter for you? I get nothing but Nil. Certainly ScenEdit_UnitX() will give me who was shot. But I just can't get the reverse.
No that's not what it does. You're just grabbing the unit info that triggered the event.

Further code will be required.

Mike
Don't call it a comeback...
Exilemnord
Posts: 12
Joined: Tue May 31, 2022 4:33 pm

Re: LUA for determining the shooter.

Post by Exilemnord »

ok yeah, it doesn't produce the side. I can get that. But for you it produced the unit info that triggered the event...i.e. the shooter?

from the docs:

ScenEdit_UnitY ()
(a) Detecting Unit ...from a Unit Detected event trigger.Otherwise,a nilis returned.
(b) Caused by unit .. from a damage/destroyed event.Otherwise,a nilis returned. The unit is the last one to actually cause damage during the event.

and

(b) Or the unit that caused the last damage

So I have a damaged /destroyed trigger. if I assign local unit to UnitY () and query unit I get Nil, which means there's nothing there. If I assign local unit to UnitX and query side I do get the unit who was shot's side. I'm unclear about what UnitY() is producing because I don't think it's producing, well anything. The Print (unit.side) produces Nil.

So back to the beginning. What can I use to determine the shooter?
BDukes
Posts: 2450
Joined: Wed Dec 27, 2017 12:59 pm

Re: LUA for determining the shooter.

Post by BDukes »

Exilemnord wrote: Thu Aug 17, 2023 12:59 am ok yeah, it doesn't produce the side. I can get that. But for you it produced the unit info that triggered the event...i.e. the shooter?

from the docs:

ScenEdit_UnitY ()
(a) Detecting Unit ...from a Unit Detected event trigger.Otherwise,a nilis returned.
(b) Caused by unit .. from a damage/destroyed event.Otherwise,a nilis returned. The unit is the last one to actually cause damage during the event.

and

(b) Or the unit that caused the last damage

So I have a damaged /destroyed trigger. if I assign local unit to UnitY () and query unit I get Nil, which means there's nothing there. If I assign local unit to UnitX and query side I do get the unit who was shot's side. I'm unclear about what UnitY() is producing because I don't think it's producing, well anything. The Print (unit.side) produces Nil.

So back to the beginning. What can I use to determine the shooter?
If this is still an issue when I have some time I'll try and tinker with it. I'm curious too.

Mike
Don't call it a comeback...
Exilemnord
Posts: 12
Joined: Tue May 31, 2022 4:33 pm

Re: LUA for determining the shooter.

Post by Exilemnord »

OK.Worked it out. To finish up.

Trigger is neutral hit.
Then set up two conditions (one of which will fail and one suceed)

local shooter = ScenEdit_UnitY()
local shooter_unit = nil
if shooter then
if shooter.unit then
shooter_unit = shooter.unit
end
end
if(shooter.unit.side) == 'Good' then
return true
else
return false
end

(obviously create another trigger for 'Bad")

Then create the subtract or add points action as normal. Trigger happens. Condition checks to see who fired and either activates the point or doesn't.

I played around in the action LUA with the same code (which worked) but was trying to add or subtract the points instead of returning true or false and couldn't find succinct LUA script that would modify a sides points. Sure it's out there and I just don't know it. If it is and could be attached it would allow for a single event. Neutral is hit - no condition - points are allocated in the action based on the above code identifying the shooter.
BDukes
Posts: 2450
Joined: Wed Dec 27, 2017 12:59 pm

Re: LUA for determining the shooter.

Post by BDukes »

Awesome job!

Mike
Don't call it a comeback...
Post Reply

Return to “Command: Modern Operations series”