The best way to avoid remove_child
error is to not remove a child that is not in the tree, or was never added to the tree to begin with. Also, the scene tree is not thread-safe, so there can't be any thread involved here. If you use threads, avoid modifying the tree from it. Use call_deferred
or other synchronization methods to schedule calls on the main thread.
If you don't use threads, the node is added to the tree and you still get that error, it means two parts of your game want to remove the same child, which means those two parts think they have ownership on that node: either structure your code so that this case can't happen, or add a check like you proposed.