# Detecting when a guard has been eliminated.

**URL:** <https://forums.unrealengine.com/t/detecting-when-a-guard-has-been-eliminated/1638197>\
**Category:** Verse\
**Tags:** unreal-editor-for-fortnite, fortnite-creative, question, fortnite\
**Created:** [January 24, 2024, 2:42am UTC](https://forums.unrealengine.com/t/detecting-when-a-guard-has-been-eliminated/1638197 "2024-01-24T02:42:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Overseeing\_Industry](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/overseeing_industry/32/584362_2.png) [@Overseeing\_Industry](https://forums.unrealengine.com/u/Overseeing_Industry)\
**Post date:** [January 24, 2024, 2:42am UTC](https://forums.unrealengine.com/t/detecting-when-a-guard-has-been-eliminated/1638197/1 "2024-01-24T02:42:57Z")

</div>

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.

```auto
@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”.

---

<div class="post-metadata">

**Author:** ![ok\_studios](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ok\_studios](https://forums.unrealengine.com/u/ok_studios)\
**Post date:** [January 25, 2024, 4:03am UTC](https://forums.unrealengine.com/t/detecting-when-a-guard-has-been-eliminated/1638197/2 "2024-01-25T04:03:12Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![JKingpin](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jkingpin/32/924238_2.png) [@JKingpin](https://forums.unrealengine.com/u/JKingpin)\
**Post date:** [January 25, 2024, 7:36pm UTC](https://forums.unrealengine.com/t/detecting-when-a-guard-has-been-eliminated/1638197/3 "2024-01-25T19:36:48Z")

</div>

@ok_studios post identified the issue exactly.

If you’d like another example of this in action you can look at the [Stronghold Template](https://forums.unrealengine.com/t/stronghold-template/1208383) 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.

```auto
#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)}

```
