Jump to content

Trigger for when a specific unit shoots down another specific unit?


Recommended Posts

Posted

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

Posted

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. 😎

Posted

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

Posted (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 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

Posted (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 by cfrag
Posted
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.

Posted (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 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

Posted (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 by cfrag
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...