Respawn killed NPC?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By JonTGN


I am fairly new to godot and gdscript but have plenty of background knowledge in python. One thing I can’t seem to figure out is how to handle killing off my npcs. I have my player character and right now it’s set up to run the kill() function in my zombie node. The kill function simply frees the queue after checking if there is in fact a node via weakref(Sprite). However as soon as I run the kill() method it is giving me an error stating "Attempt to call function ‘kill’ in base ‘previously freed instance’ on a null instance. This led me to believe it’s trying to remove all sprites or that it deletes the zombie sprite and still tries to remove them? Please correct me if I’m wrong but I have no clue how to solve this. Also how would I spawn a new zombie after the old one dies? In my physics_process function I have an if stating if the wr is false, then add_to_group(“zombies”) but I can’t tell if that’s correct or not since the game crashes as soon as I run queue_free(). Any help would be appreciated. Thanks :slight_smile:

:bust_in_silhouette: Reply From: FelipeRattu

Can you show the code from the kill() method? is kind of hard to guess like this. maybe there’s some syntax problem or the kill code is running before the node is added to the tree. i don’t think that’s the case but if you putting the queue_free() call in the same scope as the instance then the queue_free() can be running first.

to respawn the npc you just need to re-instance it’s scene or you can disable it instead of destroying from the tree and when you need it to respawn you just make it show up again and reset his variables (like health and stuff). this second option is pretty good if you know the maximum number of enemies your game is gonna be able to spawn at once (which i reccomend you to know), avoids the queue_free() problem and boost performance for not instantiating a lot of the same node over and over.