how exactly remove_child works?

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

Hey guys, i was having issues using “queue_free()”, it was crashing my game sometimes without any error mensage, so I tried to fix this bug in so many different ways and found this one,

get_parent.remove_child(self)

But i want to know how exactly it works, in documentation it says: “The node is NOT deleted and must be deleted manually.” I tested killing over 1000 enemies in my game expecting to reduce performance, but there is no impact on it, actually, the performance is better using it

If I have to delete the node manually and I didn’t, why it doesn’t affect the performance?

Why it doesn’t show anywhere in the “profiler” the removed enemies?

If I use change_scene() will the removed child be deleted as the rest of the previous scene?

Should I use queue_free() after remove child? It seems to work

My native language is not english, so forgive me if there are errors in my text.

:bust_in_silhouette: Reply From: jgodfrey

remove_child() simply remove the target node from the scene tree. It does not, however, delete the node or free up any memory used by it. You really should be using queue_free() to delete a node and free its memory.

If queue_free() really is the cause of a crash, that would seem to indicate an engine bug. As an additional guard prior to a queue_free() call, you could verify its validity via a is_instance_valid() check first (though it generally shouldn’t be necessary). Something like:

if is_instance_valid(player):
    player.queue_free()

Regarding performance when using only remove_child(), I’d expect that it will impact performance when it reaches some threshold - which you may not have reached yet.

Yeah, it seems to be a engine bug. By the way, do you know how I delete the node manually after removed? doing this maybe can solve the bug

carlospaiva | 2023-03-04 02:07