+4 votes

I need to change the parent of a node while the game is running:

node.get_parent().remove_child(node)
new_parent.add_child(node)

However this technique disconnects all signals to this node, because well, the node has to leave the SceneTree before being added again...
This is a bit silly because my intention is not to destroy the node, just change its parent within the same scene.

In addition, the script that moves the node is not supposed to know which signals are connected, it just changes the hierarchy.

Is there a better way to do this without having to reconnect everything?

Edit: I also thought I could write a helper that fetches the list of connections and restores them afterwards, but I cannot find any get_connections() method...

in Engine by (29,118 points)
edited by

this always botter me... because of this i always connect signals via script, but i have no idea how solve your problem :/

there is Object.get_signal_connection_list(signal), but it seems that has bug for now.

The second method that would be needed for this is object.get_signal_list()

I thought so, but currently Object.get_signal_connection_list(signal) returns all connected signal whatever signal parameter.

Just to clarify: are we talking about outgoing connections (signals emitted by the moved node) or incoming signals (callback methods to be called on the moved node)?

In my case, the node I move handles signals coming from another node.

1 Answer

0 votes

After more testing I realized my problem was caused by _ready() being called again when re-entering the tree, so initialization code was called twice and broke the logic.
I changed the code so it's executed only once, and now my issue is solved.

by (29,118 points)

Ah, that's very good to know. On second thought it's to be expected, but I wouldn't have expected it either.

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.