NPC Damage stuck on Infinite Loop

An unrecoverable Verse runtime error occurred! 6_15_2024 5_14_50 PM

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
1 Like

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

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.

    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
1 Like

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.

1 Like

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.

1 Like

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)
1 Like

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

    @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")

1 Like