queue_free deleting non-children nodes

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

My node can shoot bullets, which are added to it as children. When the node dies I do not want all its bullets to die. I have a function that calls remove_child and add_child on all the bullets, removing them from the parent and adding them to '/root'. Something like this:

func kill_entity:
    remove_all_bullets_and_add_to_world() #works fine according to remote inspector
    queue_free() #still deletes the bullets!

I have seen that this works in the remote inspector, a breakpoint after doing this shows the bullet nodes are on the root object. However, calling queue_free afterwards still deletes all the bullets as if they were still children. They are removed from the screen and from the remote inspector (so they’re not just being repositioned). Any ideas why this might be?

I’d have to take a look at your project, if you could upload it. However, what you’re trying to do smells a bit like bad architecture. Why not gathering the bullets in a seperate container in the first place? You can set their positions easily like this from any node:

var gun = get_node("../../Gun")
var bullet = get_node("../BulletContainer").get_child(0)
bullet.global_position = gun.global_position

Footurist | 2018-03-20 07:43