When an NPC using my custom npc_behaviour, which spawns a coroutine during OnBegin(), is eliminated, the spawned coroutine is cancelled.

Please select what you are reporting on:

Verse

What Type of Bug are you experiencing?

Verse

Summary

I have a Player Manager and a Custom NPC Behaviour file. I have a function in my Player Manager called Stun(), which I call using spawn{player_manager.Stun(PlayerCharacter)} from my Custom NPC Behaviour when the enemy damages the player. The Stun() puts the player into stasis and starts a countdown to release the player. If Stun() is called again while already active, the time remaining will be increased. If the NPC is eliminated after enough stun time has been accumulated for it to last beyond the time it takes the game to clean up the eliminated NPC, the spawned Stun() is cancelled alongside the eliminated NPC being removed from the game.

Steps to Reproduce

Create a custom npc_behavior class, assign it to a custom character and spawn it using an NPC spawner device. Have the NPC spawn a public function from the player-manager that contains a long loop with some output. Eliminate the NPC.

Greatly simplified, my code structure is like this:

player_manager.verse

player_manager<public>:= module: 

    LoopFunction<public>()<suspends>:void=

        loop:
            Print("loop running")
            Sleep(5.0)

custom_npc_behavior.verse

custom_npc_behavior := class(npc_behavior):

    OnBegin<override>()<suspends>:void =

        spawn{player_manager.LoopFunction()}

Expected Result

The spawned coroutine would continue forever or until complete.

Observed Result

The spawned coroutine is cancelled alongside the eliminated NPC being cleaned up.

Platform(s)

PC

Could anyone share a way for me to call this function by some action of my custom NPC while being fully disconnected so that the function doesn’t die along with the NPC?