anfieldroad Posted April 19, 2022 Posted April 19, 2022 Hi, I'm trying to set up a simple event handler to set a flag when the insurgents group either fires guns or MANPADs. The intent is that shortly after the hidden group goes active once you enter their trigger zone they will fire at you but then do the smart thing of hiding again in a hit and fade manner once a time frame has elapsed since the firing occurs. Problem is I get no message from the debug outText and the insurgents stick around constantly shooting once they go active. I get no errors from DCS on the lua itself. Handler = {} function Handler:onEvent(event) if event.id == world.event.S_EVENT_SHOOTING_START or event.id == world.event.S_EVENT_SHOT then local grp = event.initiator:getGroup() if grp.getName() == 'Insurgents' then trigger.action.outText("Insurgents firing", '10', 'true') trigger.action.setUserFlag('4', true) end end end world.addEventHandler(Handler) I also initially tried: env.setErrorMessageBoxEnabled(false) Handler = {} function Handler:onEvent(event) if event.id == (world.event.S_EVENT_SHOOTING_START or event.id == world.event.S_EVENT_SHOT) and event.initiator == group.getByName('Insurgents') then trigger.action.outText("Insurgents firing", '10', 'true') trigger.action.setUserFlag('4', true) end end world.addEventHandler(Handler) I also tried this with unit.getByName('Insurgents') Can anyone suggest better ways of doing this, it should be so simple.
cfrag Posted April 19, 2022 Posted April 19, 2022 24 minutes ago, anfieldroad said: if event.id == (world.event.S_EVENT_SHOOTING_START or event.id == world.event.S_EVENT_SHOT) That looks like a misplaced parentheses. You are comparing event.id to a boolean.
anfieldroad Posted April 19, 2022 Author Posted April 19, 2022 56 minutes ago, cfrag said: That looks like a misplaced parentheses. You are comparing event.id to a boolean. In most languages parentheses are used to group expressions in this case (A or B) AND C Is that not the case in LUA? 57 minutes ago, cfrag said: That looks like a misplaced parentheses. You are comparing event.id to a boolean. OHHH I see, you mean the ( should be before event.id
cfrag Posted April 19, 2022 Posted April 19, 2022 Just now, anfieldroad said: OHHH I see, you mean the ( should be before event.id sorry, was lazy.
cfrag Posted April 19, 2022 Posted April 19, 2022 Well now that I've looked a bit closer, some additional notes: 1 hour ago, anfieldroad said: if grp.getName() == 'Insurgents' then should probably be if grp:getName() == "Insurgents" then (colon instead of period -- should have thrown an error) Also, I recommend that you validate that event.initiator is non-nil before dereferencing it. Not required of course, but better safe than sorry
anfieldroad Posted April 19, 2022 Author Posted April 19, 2022 3 hours ago, cfrag said: Well now that I've looked a bit closer, some additional notes: should probably be if grp:getName() == "Insurgents" then (colon instead of period -- should have thrown an error) Also, I recommend that you validate that event.initiator is non-nil before dereferencing it. Not required of course, but better safe than sorry Ummm and how do I do that? Sorry I am still very new to this LUA stuff.
cfrag Posted April 19, 2022 Posted April 19, 2022 4 minutes ago, anfieldroad said: how do I do that? Simply replace the dot '.' with a colon ':' in grp:getName() The colon notation is syntactical sugar in Lua to make it more readable and look more OOP. "class.method(object, params)" can be written (if the class is defined correctly) as "object:method(params)" grp.getName() should be either Group.getName(grp) or grp:getName() [with "Group" being the Class name]] Lua is great, but a bit confusing at first, mainly because it's merely an OOP look-alike, not really OOP.
anfieldroad Posted April 19, 2022 Author Posted April 19, 2022 5 minutes ago, cfrag said: Simply replace the dot '.' with a colon ':' in grp:getName() The colon notation is syntactical sugar in Lua to make it more readable and look more OOP. "class.method(object, params)" can be written (if the class is defined correctly) as "object:method(params)" grp.getName() should be either Group.getName(grp) or grp:getName() [with "Group" being the Class name]] Lua is great, but a bit confusing at first, mainly because it's merely an OOP look-alike, not really OOP. Sorry I meant the last part sorry Quote "Also, I recommend that you validate that event.initiator is non-nil before dereferencing it."
cfrag Posted April 19, 2022 Posted April 19, 2022 Oh, sorry, I was being pompous. Check that the variable that you are accessing is non-nil (i.e. it exists) before you access it. Change local grp = event.initiator:getGroup() to if not event.initiator then return end -- initiator is nil, abort! local grp = event.initiator:getGroup()
toutenglisse Posted April 19, 2022 Posted April 19, 2022 6 hours ago, anfieldroad said: ...Can anyone suggest better ways of doing this, it should be so simple. In case it still helps, (cfrag already did it) if you want a script with conditions valid when 'Insurgents' units only shoot guns or sams to player only, with message telling what weapon is used, with a check that initiator and target both exists (to avoid any possible errors), you can use (it's your script with some changes): Spoiler Handler = {} function Handler:onEvent(event) if event.id == world.event.S_EVENT_SHOOTING_START and event.initiator and event.initiator:getGroup():getName() == 'Insurgents' and event.target and event.target:getPlayerName() then trigger.action.outText("Insurgents firing guns type " .. event.weapon_name, '10', 'true') trigger.action.setUserFlag('4', true) elseif event.id == world.event.S_EVENT_SHOT and event.initiator and event.initiator:getGroup():getName() == 'Insurgents' and event.weapon and event.weapon:getTarget() and event.weapon:getTarget():getPlayerName() then trigger.action.outText("Insurgents firing missile type " .. event.weapon:getTypeName(), '10', 'true') trigger.action.setUserFlag('4', true) end end world.addEventHandler(Handler) 1
anfieldroad Posted April 20, 2022 Author Posted April 20, 2022 22 hours ago, toutenglisse said: In case it still helps, (cfrag already did it) if you want a script with conditions valid when 'Insurgents' units only shoot guns or sams to player only, with message telling what weapon is used, with a check that initiator and target both exists (to avoid any possible errors), you can use (it's your script with some changes): Hide contents Handler = {} function Handler:onEvent(event) if event.id == world.event.S_EVENT_SHOOTING_START and event.initiator and event.initiator:getGroup():getName() == 'Insurgents' and event.target and event.target:getPlayerName() then trigger.action.outText("Insurgents firing guns type " .. event.weapon_name, '10', 'true') trigger.action.setUserFlag('4', true) elseif event.id == world.event.S_EVENT_SHOT and event.initiator and event.initiator:getGroup():getName() == 'Insurgents' and event.weapon and event.weapon:getTarget() and event.weapon:getTarget():getPlayerName() then trigger.action.outText("Insurgents firing missile type " .. event.weapon:getTypeName(), '10', 'true') trigger.action.setUserFlag('4', true) end end world.addEventHandler(Handler) Thanks Toutenglisse but that example is more complicated than needed, all I need really is to set a flag and I don't need to know what is being fired, only that something is being fired. The flag will trigger the units to fall back to cover. The intent is that AI units will move out of cover once units are within range, fire, then return back to cover in a hit and fade action. If the cover area is a building the AI units teleport out to a faraway place and then later will teleport back in if units are still in the zone. It's to make AI units far more realistic and difficult. They aren't going to stand there in a field waiting for your rockets, they'll run out of a building, find suitable nearby cover, start firing and then disappear. In certain missions the place they come out of may be hidden like a simulated tunnel entrance, they may teleport to a new location. So yeah all that verbosity isn't needed.
Recommended Posts