0 votes

Removing a child node n:


Will add error "Condition 'idx ==-1' is true." to Debugger Errors tab if the referenced node is not a child.

Could guard with:

if is_a_parent_of(n):

What if child was somehow removed in another thread between the guard check and the removal?

in Engine by (69 points)

1 Answer

+1 vote

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.

by (29,034 points)

Quality answer. Thanks. The only part that confused me is when you said that because the scene tree is not thread-safe, "there can't be any thread involved here". Is it more accurate to say that multiple threads could be involved here, but should NOT be?


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 Frequently asked questions and 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 [email protected] with your username.