# 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:** 1\
**Showing post:** 3

<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)}

```

---

_[View the full topic](https://forums.unrealengine.com/t/detecting-when-a-guard-has-been-eliminated/1638197)._
