0 votes

Removing a child node n:

remove_child(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):
    remove_child(n)

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

asked Jul 5, 2019 in Engine by goshot (61 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.

answered Jul 5, 2019 by Zylann (21,823 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?

https://docs.godotengine.org/en/3.1/tutorials/threads/thread_safe_apis.html

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.