remove_child() and queue_free()

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

Hello, I have a small problem that I don’t know how to solve:
I have a parent node that I remove from the tree in the ready() function, I have assigned some of its children a code that causes them to be removed with queue_free(), the problem is that the code removes the child nodes correctly, but when adding the node back to the main tree with add_child gives me an error, node not found, which refers to those that were previously removed. In the application itself it does not give any problem and I imagine that it is because it eliminates the child nodes when it is not in the tree.
Is there a way to read the children’s script first and remove them and then pass to the parent’s script to detach it from the tree?

This is hard to imagine.
Please paste and format some of the code for better undestanding. Also explain why is it supposed to be dettached and queued free in this order in your project

Inces | 2022-04-14 20:17

:bust_in_silhouette: Reply From: 3dpishu

It is a game that I am making in 3d, where the dungeon is randomly generated rooms and instantiated to conform it. what I do is remove them from the tree once generated in order to improve performance and not lower the fps. these rooms go in and out of the tree as they are needed.
To generate them, what I do is that each room contains all the assets that make it up visually arranged as I intend them to appear and I randomly delete some assets so that the different instances of this same room are different in each one.
The problem is that if I remove the instantiated node from the tree after deleting those assets, when I add it back to the tree it gives me the following error trying to call the assets that no longer exist because they have been deleted.
If I don’t delete those assets, it doesn’t give any error.
This is the error:
E 0:00:26.136 get_node: (Node not found: “…/borra/piedraA” (relative to “/root/room/@cuevaC@11/tile/mapa/RocaA4”).)
<Error de C++>Condition “!node” is true. Returned: nullptr
<Fuente C++> scene/main/node.cpp:1325 @ get_node()
cueva.gd:25 @ _on_VisibilityNotifier_camera_entered_cueva()
I have tried to put the script that removes it from the tree at the beginning and at the end of the instantiated branch with the same result.

And this is the code that removes the assets that I need to delete, they are the other scripts that are seen in the previous image