# NPC Damage stuck on Infinite Loop

**URL:** <https://forums.unrealengine.com/t/npc-damage-stuck-on-infinite-loop/1906986>\
**Category:** Verse\
**Tags:** unreal-editor-for-fortnite, editor, fortnite\
**Created:** [June 17, 2024, 10:44pm UTC](https://forums.unrealengine.com/t/npc-damage-stuck-on-infinite-loop/1906986 "2024-06-17T22:44:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Xonxo9000](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/xonxo9000/32/1221348_2.png) [@Xonxo9000](https://forums.unrealengine.com/u/Xonxo9000)\
**Post date:** [June 17, 2024, 10:44pm UTC](https://forums.unrealengine.com/t/npc-damage-stuck-on-infinite-loop/1906986/1 "2024-06-17T22:44:57Z")

</div>

![An unrecoverable Verse runtime error occurred! 6_15_2024 5_14_50 PM](https://d3kjluh73b9h9o.cloudfront.net/original/4X/4/8/d/48d10cc03d0595f604cafbaeaa4473c6c4a2eebe.png)

Infinite loop keeps happening when NPC dies. Damage works fine until death. HandlePlayerHit makes it so the NPC deals extra damage when it is hit. I need this to stop having errors.

using { /Fortnite.com/Devices }  
using { /Verse.org/Simulation }  
using { /UnrealEngine.com/Temporary/Diagnostics }  
using { /Fortnite.com/Characters }  
using { /UnrealEngine.com/Temporary/SpatialMath }  
using { /Fortnite.com/AI }  
using { /Verse.org/Random }  
using { /Fortnite.com/Game }

MovementState := enum{Idle, Walking, Attacking}  
enemy\_info\_class := class:  
@editable Health : float = 1.0  
@editable Damage : float = 0.0

custom\_enemies := class(creative\_device):  
@editable Spawners : npc\_spawner\_device = array{}  
@editable EnableTrigger : trigger\_device = trigger\_device{}  
@editable DisableTrigger : trigger\_device = trigger\_device{}  
@editable var Enabled : logic = true  
@editable var EnemyArray : enemy\_info\_class = array{}

```
OnBegin<override>()<suspends>:void=
    EnableTrigger.TriggeredEvent.Subscribe(Enable)
    DisableTrigger.TriggeredEvent.Subscribe(Disable)
    for (Spawner : Spawners):
        Spawner.SpawnedEvent.Subscribe(CreatureSpawned)  

DamageTracker(FortChar : fort_character,MaxHealth : float)<suspends>: void =
    var Health : float = MaxHealth
    loop:
        DamageStruct := FortChar.DamagedEvent().Await()
        FortChar.DamagedEvent().Subscribe(HandlePlayerHit)
        set Health -= DamageStruct.Amount

HandlePlayerHit(DamageResult : damage_result) : void =
    Target := DamageResult.Target
    Amount := DamageResult.Amount

    if (FortCharacterWhoWasHit := fort_character[Target]):
        Print("Was Hit for {Amount}")
            if(not DamageResult.Source?):
                FortCharacterWhoWasHit.Damage(12.0) 

CreatureSpawned(Agent : agent): void =
    if (Enabled = true):
        if (FortChar := Agent.GetFortCharacter[],Enemy := EnemyArray[GetRandomInt(0,EnemyArray.Length-1)]):
            FortChar.SetMaxHealth(Enemy.Health)
            spawn{EnemyLoop(FortChar,Enemy)}

EnemyLoop(FortChar : fort_character,EnemyInfo : enemy_info_class)<suspends>: void =
    spawn{DamageTracker(FortChar,EnemyInfo.Health)}
    var LastTransform : transform = FortChar.GetTransform()

Enable(MaybeAgent : ?agent): void =
    set Enabled = true

Disable(MaybeAgent : ?agent): void =
    set Enabled = false

```

---

<div class="post-metadata">

**Author:** ![Mineblo](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/mineblo/32/1203575_2.png) [@Mineblo](https://forums.unrealengine.com/u/Mineblo)\
**Post date:** [June 18, 2024, 2:48am UTC](https://forums.unrealengine.com/t/npc-damage-stuck-on-infinite-loop/1906986/2 "2024-06-18T02:48:13Z")

</div>

What is the code supposed to do exactly? Could you explain what the end result should be?

Also in infinite Loops you need to have a Sleep() inside

---

<div class="post-metadata">

**Author:** ![Uxibeo](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Uxibeo](https://forums.unrealengine.com/u/Uxibeo)\
**Post date:** [June 18, 2024, 4:22am UTC](https://forums.unrealengine.com/t/npc-damage-stuck-on-infinite-loop/1906986/3 "2024-06-18T04:22:48Z")

</div>

I’ve added a break condition to halt the loop when `FortChar` is `Eliminated` , which is a straightforward fix to prevent an infinite loop. Additionally, I moved the `subscribe` on `DamagedEvent` outside of the loop since it only needs to be subscribed to once. However, I believe there is room for improvement in the overall code structure. It would be beneficial to refactor the Npc Spawner and its related components into a separate class, enabling us to utilize subscriptions instead of the current `await` and loop approach, leading to more efficient and cleaner code.

```auto
    DamageTracker(FortChar : fort_character,MaxHealth : float)<suspends>: void =
        var Health : float = MaxHealth
        FortChar.DamagedEvent().Subscribe(HandlePlayerHit)
        loop:
            LoopResult := race:
                block:
                    FortChar.EliminatedEvent().Await()
                    1
                block:
                    DamageStruct := FortChar.DamagedEvent().Await()
                    set Health -= DamageStruct.Amount 
                    2
            if (LoopResult = 1):
                break

```

---

<div class="post-metadata">

**Author:** ![Xonxo9000](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/xonxo9000/32/1221348_2.png) [@Xonxo9000](https://forums.unrealengine.com/u/Xonxo9000)\
**Post date:** [June 18, 2024, 8:10pm UTC](https://forums.unrealengine.com/t/npc-damage-stuck-on-infinite-loop/1906986/4 "2024-06-18T20:10:37Z")

</div>

It is supposed to make it so when you select a npc spawner it makes all npcs on that spawner have the extra damage dealt. But when I eliminate the npc the verse breaks and gives an infinite loop error making it not be able to do extra damage anymore.

---

<div class="post-metadata">

**Author:** ![Xonxo9000](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/xonxo9000/32/1221348_2.png) [@Xonxo9000](https://forums.unrealengine.com/u/Xonxo9000)\
**Post date:** [June 18, 2024, 8:11pm UTC](https://forums.unrealengine.com/t/npc-damage-stuck-on-infinite-loop/1906986/5 "2024-06-18T20:11:28Z")

</div>

It still gives an error when the npc dies. It says it has something to do with my HandleHit event but that is not a loop and it keeps saying it is. I really dont know how to fix it.

---

<div class="post-metadata">

**Author:** ![Xonxo9000](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/xonxo9000/32/1221348_2.png) [@Xonxo9000](https://forums.unrealengine.com/u/Xonxo9000)\
**Post date:** [June 18, 2024, 8:13pm UTC](https://forums.unrealengine.com/t/npc-damage-stuck-on-infinite-loop/1906986/6 "2024-06-18T20:13:02Z")

</div>

Here is my modified script. But still the same error.

using { /Fortnite.com/Devices }  
using { /Verse.org/Simulation }  
using { /UnrealEngine.com/Temporary/Diagnostics }  
using { /Fortnite.com/Characters }  
using { /UnrealEngine.com/Temporary/SpatialMath }  
using { /Fortnite.com/AI }  
using { /Verse.org/Random }  
using { /Fortnite.com/Game }

MovementState := enum{Idle, Walking, Attacking}  
enemy\_info\_class := class:

custom\_enemies := class(creative\_device):  
@editable Spawners : npc\_spawner\_device = array{}  
@editable EnableTrigger : trigger\_device = trigger\_device{}  
@editable DisableTrigger : trigger\_device = trigger\_device{}  
@editable var Enabled : logic = true  
@editable var EnemyArray : enemy\_info\_class = array{}

```
OnBegin<override>()<suspends>:void=
    EnableTrigger.TriggeredEvent.Subscribe(Enable)
    DisableTrigger.TriggeredEvent.Subscribe(Disable)
    for (Spawner : Spawners):
        Spawner.SpawnedEvent.Subscribe(CreatureSpawned)

Enable(MaybeAgent : ?agent): void =
    set Enabled = true

Disable(MaybeAgent : ?agent): void =
    set Enabled = false

CreatureSpawned(Agent : agent): void =
    if (Enabled = true):
        if (FortChar := Agent.GetFortCharacter[],Enemy := EnemyArray[GetRandomInt(0,EnemyArray.Length-1)]):
            FortChar.DamagedEvent().Subscribe(HandlePlayerHit)

HandlePlayerHit(DamageResult : damage_result) : void =
    Target := DamageResult.Target
    Amount := DamageResult.Amount

    if (FortCharacterWhoWasHit := fort_character[Target]):
        Print("Was Hit for {Amount}")
            if(not DamageResult.Source?):
                FortCharacterWhoWasHit.Damage(12.0)

```

---

<div class="post-metadata">

**Author:** ![Mineblo](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/mineblo/32/1203575_2.png) [@Mineblo](https://forums.unrealengine.com/u/Mineblo)\
**Post date:** [June 18, 2024, 11:23pm UTC](https://forums.unrealengine.com/t/npc-damage-stuck-on-infinite-loop/1906986/7 "2024-06-18T23:23:23Z")

</div>

Here’s a quick script I made/edited for guard spawners (might need some fine tuning for npc spawner) where it grabs their fort\_character at their spawn and places it into an array then and only then when a player is damaged if the instigator is the guard it will do extra 40 damage, and I have also made it where if a non-arrayed fort\_character who is not the same as the target it will do extra 4 dmg (can be removed).

As you can see in this video whenever the bot hits me for 21 dmg it deals an additional 40 and when I hit the player aside from the normal 30 the scar does it will also deal 5 more additional damage

[![](https://d3kjluh73b9h9o.cloudfront.net/original/4X/6/1/e/61ecf620ec599436bb468f8e944a1f71d591bd31.jpeg "Video for Dev Forums UEFN") ](https://www.youtube.com/watch?v=kD7KQBZn_bw)

```auto
    @editable Spawners : []guard_spawner_device = array{}
    @editable EnableTrigger : trigger_device = trigger_device{}
    @editable DisableTrigger : trigger_device = trigger_device{}
    @editable Enabled : logic = true
    var CreatureArray : []fort_character = array{}
    OnBegin<override>()<suspends>:void=

        for (Spawner : Spawners):
            Spawner.SpawnedEvent.Subscribe(CreatureSpawned)
            Spawner.EliminatedEvent.Subscribe(CreatureDeSpawned)
        for(Player:GetPlayspace().GetPlayers()):
            if(FC:=Player.GetFortCharacter[]):
                FC.DamagedEvent().Subscribe(HandlePlayerHit)
    

    
    HandlePlayerHit(DamageResult : damage_result) : void =
        Target := DamageResult.Target
        Amount := DamageResult.Amount
    
        if (FortCharacterWhoWasHit := fort_character[Target],Amount>=1.0):
            Print("Was Hit for {Amount}")
                if(TargetFC:=fort_character[DamageResult.Target],InstigFC:=fort_character[DamageResult.Instigator?],not TargetFC=InstigFC):
                    if(CreatureArray.Find[InstigFC]):
                        Print("MONSTER DAMAGE")
                        FortCharacterWhoWasHit.Damage(40.0)
                    else:
                        Print("HOOMAN DAMAGE")
                        FortCharacterWhoWasHit.Damage(5.0)
    
    
    CreatureSpawned(Agent : agent): void =
        if (Enabled = true):
            if (FortChar := Agent.GetFortCharacter[]):
                set CreatureArray += array{FortChar}
    CreatureDeSpawned(Result : device_ai_interaction_result): void =
        if (Target:=Result.Target?):
            if (FortChar := Target.GetFortCharacter[]):
                set CreatureArray = CreatureArray.RemoveAllElements(FortChar)
                Print("CREATURE REMOVED")

```
