Page 1 of 1

LUA for determining the shooter.

Posted: Wed Aug 16, 2023 3:19 pm
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.

Re: LUA for determining the shooter.

Posted: Wed Aug 16, 2023 3:45 pm
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

Re: LUA for determining the shooter.

Posted: Wed Aug 16, 2023 4:53 pm
by Exilemnord
sorry. Have it. Just mistyped

Re: LUA for determining the shooter.

Posted: Wed Aug 16, 2023 5:33 pm
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?

Re: LUA for determining the shooter.

Posted: Wed Aug 16, 2023 8:00 pm
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()

Re: LUA for determining the shooter.

Posted: Wed Aug 16, 2023 8:03 pm
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

Re: LUA for determining the shooter.

Posted: Wed Aug 16, 2023 10:26 pm
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.

Re: LUA for determining the shooter.

Posted: Wed Aug 16, 2023 11:18 pm
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

Re: LUA for determining the shooter.

Posted: Thu Aug 17, 2023 12:59 am
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?

Re: LUA for determining the shooter.

Posted: Thu Aug 17, 2023 3:08 pm
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

Re: LUA for determining the shooter.

Posted: Thu Aug 17, 2023 6:19 pm
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.

Re: LUA for determining the shooter.

Posted: Fri Aug 18, 2023 1:23 pm
by BDukes
Awesome job!

Mike