+5 votes

Both use to remove children of parent

in Engine by (185 points)

Honestly, I don't know why remove_child exists. It removes the child and it's children but doesn't delete them(?), so you might as well use queue_free which is an out right deletion of the node. There must be some reason for it but it's beyond my perception.

They remove children as well.
I also do not understand the difference well.

like someone said, they're just different

however no-one has pointed out... you can use just remove_child.... and the resources WILL be freed at some point by Godot (with the reference counting)

they will be freed if you have no longer any references to them (but be careful about circular references, that's where the reference leads back to itself, eg ObjectA has a ref to Object B , ObjectB has a ref to ObjectA)

2 Answers

+18 votes

queue_free and remove_child are some of the common methods you need to know when beginning with Godot, and they do two specific things. They are both mentionned in tutorials, as well as in the doc:



queue_free() is essentially a shortcut for destroying a node safely at the end of the frame and also removes it from tree. It does not happen immediately to avoid some tricky situations that would otherwise cause the game to crash. For example, your script receives a message, you destroy the node, but... what if the code that called your script in the first place might have been the node itself, or is using that node somewhere up the call hierarchy. And if you destroy it immediately... the caller will access garbage memory. So to keep things simple, it's good practice to use queue_free() for nodes in the tree. "queue" stands for putting the node in a list of "things to delete when the game finished processing at this frame".
In fact, free() exists if you want to destroy the node immediately. But be careful about what I said above if you decide to use it, just know what you are doing. It can be useful if you have a node already outside the tree for example.

remove_child is different: it's only a subset of what queue_free does. It just removes a node (or branch of nodes) out of the tree. This effectively "removes them from the world", without actually loosing their data, and this can be useful in some scenarios. For example, you might want to enter a house, but remember the state of the outside map: remove the map from the tree, add the house, but without destroying either of them. You can then swap nodes in and out.
Adding and removing nodes from the tree is not useless at all, it's just the way Godot handles the scene tree and there are use cases for them. If you think you don't need it, just don't use it.

by (27,574 points)
edited by

thanks comment.
Does it mean that the node removed by remove_child can be reused?

It can be added back to the tree if you want to, yeah. It will then be in the same state it was from the time you removed it. The only advice is to keep a variable with the node if you want to do this, so that the instance is not lost.
If you don't add it back to the tree before the game quits, you may have to free it.

thanks!i understand a little bit!

+6 votes

remove_child() is the opposite of add_child(). It removes the node from the scene tree. It does not delete the node, so you can then add it as the child of another node, for example.

free() is the method for deleting a node from memory. However, it's not always safe to do so. If you delete a node while other nodes may still be processing it, you'll crash. Therefore, it's safer to call queue_free() which tells the engine "Delete this node as soon as it's safe to do so."

by (19,175 points)

thanks for comment.

Can not be reused if deleted by queuefree (deleted from memory)
Child removed by removechild can be reused
is this Correct?

Yes, that seems to be correct :)

I still need to understand

remove_child() is the opposite of add_child()

That's true but it could infer that


would work when it doesn't. As far as I know remove_child() can be used on any child node whereas add_child() needs a preloaded instance.

This is incorrect. Either one needs a node reference. The problem with your code above is that you're using $node which is shorthand for get_node("node"). You can't get a node reference to a node that's not in the tree.

This works fine:

mynode = $node

So queue_free() would cause it to loose the variable reference and remove_child() would keep it. I take my first comment back.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.