+7 votes

I'm trying to put a node under another parent from script, but surprisingly I got many errors.
First, I tried:
But the debugger says: "node already has a parent".
Ok, so I try to remove the node from its parent first:

node.get_parent().remove_child(node) # error here  

But again, error: This function can't be used during the in/out signal.
I seem to get the behaviour I want, but something went wrong, and I want to know how to change the parent properly?

in Engine by (28,744 points)
edited by

Do you happen to have some code we can look at?

I updated my post, the markdown was wrong on the second snippet.

Also, I just tested the second snippet on a blank project with simple Nodes, and it worked. But in my project it happens inside an Area2D.body_entered() callback. maybe it's related?
I can't extract the code easily right now, I need some time.

Also, any idea why the second error usually happens, and how to avoid it?

1 Answer

+5 votes

It looks like areas are locked during area_enter/exit. I think it should work if you use it with call deferred. Something like:


func reparent(node):
  node.get_parent().remove_child(node) # error here  

Or maybe it's better to just add the node to a var or array and do it in next _process.

by (66 points)
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.
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 webmaster@godotengine.org with your username.