d0ppler Posted December 10, 2022 Posted December 10, 2022 I'm working on a single player mission in a campaign where I don't want the mission to be completed if Player has committed fratricide. So I'm trying to create a trigger when a friendly unit gets shot AND the shooter/instigator is the Player. Can it be done WITHOUT implementing the MOOSE framework? A-10C, AV-8B, Ka-50, F-14B, F-16C, F-5E, F/A-18C, L-39, Mi-8, MiG-21, MiG-29, SA34, Spitfire, Su-27, Su-33, UH-1H
cfrag Posted December 10, 2022 Posted December 10, 2022 Yes. Basically, all you need is to check when the kill event occurs if the initiator‘s faction is equal to to the target, and the initiator is the player. If I were home, I could whip out a small script for you, but unfortunately I‘m not. If no one else will beat me to it, I‘ll give it a shot come Wednesday.
d0ppler Posted December 10, 2022 Author Posted December 10, 2022 Thanks! I'll try myself first, but please feel free to help me out when you have any spare time I've barely touched lua scripting, but I see now that I must learn at least the basics. A-10C, AV-8B, Ka-50, F-14B, F-16C, F-5E, F/A-18C, L-39, Mi-8, MiG-21, MiG-29, SA34, Spitfire, Su-27, Su-33, UH-1H
d0ppler Posted December 10, 2022 Author Posted December 10, 2022 (edited) I've literally made my first LUA script from scratch! It works, and it looks like this (It's enough to hit a friendly guy to not get mission success, and S_EVENT_HIT served perfectly) fratricide = {} function fratricide:onEvent(event) local _initname = '' if event.id == world.event.S_EVENT_HIT then _initname = event.initiator:getName() if event.initiator:getCoalition() == event.target:getCoalition() then -- you have screwed up trigger.action.setUserFlag('9', true) -- this flag gets true if misconduct of ROE as well. Mission won't be completed trigger.action.outText(_initname .. ' has shot at one of his own', 20) end end end world.addEventHandler(fratricide) Edited December 11, 2022 by d0ppler A-10C, AV-8B, Ka-50, F-14B, F-16C, F-5E, F/A-18C, L-39, Mi-8, MiG-21, MiG-29, SA34, Spitfire, Su-27, Su-33, UH-1H
cfrag Posted December 11, 2022 Posted December 11, 2022 (edited) That looks like 99% there already, nicely done. You may want to add in some sanity checks to avoid ugly (but non-crashing) error messages that can arise from some edge cases. For example, if anyone accidentally hits a scenery object, you‘ll get an error message (just press ok to continue afterwards) because scenery objects don‘t have a coalition. Small stuff like that. Also, be advised that if the AI manages to shoot their own, the player will still get punished for that, so you may want to look into that as well (that‘s another edge case, your code should still be fine 99% of the time. But since you now know this exists, it will keep bothering you until you scratch that itch yeah, coding quickly becomes addictive) Edited December 11, 2022 by cfrag
TEMPEST.114 Posted December 11, 2022 Posted December 11, 2022 14 hours ago, d0ppler said: I've literally made my first LUA script from scratch! It works, and it looks like this (It's enough to hit a friendly guy to not get mission success, and S_EVENT_HIT served perfectly) fratricide = {} function fratricide:onEvent(event) local _initname = '' if event.id == world.event.S_EVENT_HIT then _initname = event.initiator:getName() if event.initiator:getCoalition() == event.target:getCoalition() then -- you have screwed up trigger.action.setUserFlag('9', true) -- this flag gets true if misconduct of ROE as well. Mission won't be completed trigger.action.outText(_initname .. ' has shot at one of his own', 20) end end end world.addEventHandler(fratricide) Lots of assumptions that it's only men flying eh? Kudos on writing your first script! Welcome to the Matrix.
cfrag Posted December 11, 2022 Posted December 11, 2022 2 hours ago, Elphaba said: Lots of assumptions that it's only men flying eh? So you assume that only men can identify as male???
TEMPEST.114 Posted December 11, 2022 Posted December 11, 2022 1 hour ago, cfrag said: So you assume that only men can identify as male??? Yes.
d0ppler Posted December 12, 2022 Author Posted December 12, 2022 (edited) On 12/11/2022 at 11:18 AM, cfrag said: That looks like 99% there already, nicely done. You may want to add in some sanity checks to avoid ugly (but non-crashing) error messages that can arise from some edge cases. For example, if anyone accidentally hits a scenery object, you‘ll get an error message (just press ok to continue afterwards) because scenery objects don‘t have a coalition. Small stuff like that. Also, be advised that if the AI manages to shoot their own, the player will still get punished for that, so you may want to look into that as well (that‘s another edge case, your code should still be fine 99% of the time. But since you now know this exists, it will keep bothering you until you scratch that itch yeah, coding quickly becomes addictive) Thank you for pointing that out! Here's what it looks like now, and seems to work. fratricide = {} function fratricide:onEvent(event) local _initname = '' if event.id == world.event.S_EVENT_HIT then _initname = event.initiator:getName() if event.target:getCoalition() and _initname == 'Player1' then if event.initiator:getCoalition() == event.target:getCoalition() then -- you have screwed up trigger.action.setUserFlag('505', true) end end end end world.addEventHandler(fratricide) Edited December 12, 2022 by d0ppler A-10C, AV-8B, Ka-50, F-14B, F-16C, F-5E, F/A-18C, L-39, Mi-8, MiG-21, MiG-29, SA34, Spitfire, Su-27, Su-33, UH-1H
cfrag Posted December 12, 2022 Posted December 12, 2022 (edited) That looks almost perfect. I only see a small possible improvement: Change if event.target:getCoalition() and _initname == 'Player1' then to if event.target.getCoalition and _initname == 'Player1' then (Change colon to dot, and remove the paratheses). Reason: we merely check if event.target has a member ‘getCoalition’ instead of invoking it (the latter can lead to errors if - like scenery objects - event.target has no getCoalition method). Edited December 12, 2022 by cfrag
d0ppler Posted December 16, 2022 Author Posted December 16, 2022 Thanks! A-10C, AV-8B, Ka-50, F-14B, F-16C, F-5E, F/A-18C, L-39, Mi-8, MiG-21, MiG-29, SA34, Spitfire, Su-27, Su-33, UH-1H
Recommended Posts