Topic was automatically imported from the old Question2Answer platform.
Both use to remove children of parent
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.
Magso | 2019-07-31 21:25
They remove children as well.
I also do not understand the difference well.
bgegg | 2019-08-02 00:31
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)
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.
Does it mean that the node removed by remove_child can be reused?
bgegg | 2019-08-01 07:52
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.
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.”
thanks for comment.
Can not be reused if deleted by queuefree (deleted from memory)
Child removed by removechild can be reused
is this Correct?
bgegg | 2019-08-01 06:50
Yes, that seems to be correct
Calinou | 2019-08-01 12:43
I still need to understand
bgegg | 2019-08-02 00:32
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.
Magso | 2019-08-02 13:42
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.