Detecting when a guard has been eliminated.

I made an array for all of my guard spawners. I want to know when one from any of the spawners has been eliminated. Here is the code I have so far.

@editable
    Spawners : []guard_spawner_device = array{ }

OnBegin<override>()<suspends>:void=
        Spawners.EliminatedEvent.Subscribe(OnAIDeath)

The Error seems to display on “EliminatedEvent” with the discription of “Unknown Member”.

You need to write the function you are calling. Subscribe binds to a function called OnAIDeath, but there is no function by that name. The function should probably look something like this:

OnAIDeath(Agent:agent): void =
Print(“Guard has been eliminated.”)

In the above, you would put whatever expressions you wanted to happen after the guard is eliminated. In the Documentation, there is an elimination game example you can study to understand this more.

Hope that helps.

1 Like

@ok_studios post identified the issue exactly.

If you’d like another example of this in action you can look at the Stronghold Template from within the UEFN Feature Examples.
Here’s a snippet from the stronghold_bark_manager.verse script that shows handling the Eliminated Event and declaring the OnGuardEliminated which would be the same as your OnAIDeath function.

Many of your typical applicated can be found in this project and it’s various verse scripts.

Hope this helps.

#call this SubscribeToGuardSpawnerEvents function in your OnBegin function
 SubscribeToGuardSpawnerEvents(GuardSpawnerDevice:guard_spawner_device):void = 
        GuardSpawnerDevice.DamagedEvent.Subscribe(OnGuardDamaged)
        GuardSpawnerDevice.EliminatedEvent.Subscribe(OnGuardEliminated)
        GuardSpawnerDevice.EliminatingEvent.Subscribe(OnPlayerEliminated)

#an example of doing something when a guard is eliminated, like playing a sound effect device
 OnGuardEliminated(InteractionResult:device_ai_interaction_result):void=
        if (EliminatedGuard := InteractionResult.Target?):

            # Find closest alive guard to play this bark
            var ClosestGuard:?agent = false
            if:
                set ClosestGuard = option{StrongholdGameManager.AlertedGuards[0]}
                EliminatedGuardCharacter := EliminatedGuard.GetFortCharacter[]
            then:
                for (AlertedGuard : StrongholdGameManager.AlertedGuards, AlertedGuardCharacter := AlertedGuard.GetFortCharacter[]):
                    if:
                        not ClosestGuard? = AlertedGuard
                        ClosestGuardCharacter := ClosestGuard?.GetFortCharacter[]
                        DistanceSquaredToAlertedGuard := DistanceSquared(AlertedGuardCharacter.GetTransform().Translation, EliminatedGuardCharacter.GetTransform().Translation)
                        DistanceSquaredToClosestGuard := DistanceSquared(ClosestGuardCharacter.GetTransform().Translation, EliminatedGuardCharacter.GetTransform().Translation)
                        DistanceSquaredToAlertedGuard < DistanceSquaredToClosestGuard
                    then:
                        set ClosestGuard = option{AlertedGuard}

            if (Guard := ClosestGuard?):
                spawn {BarkNPCDown.PlayBark(Guard)}