How to move a node without loosing its connections?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Zylann
:warning: Old Version Published before Godot 3 was released.

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…

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

tiernich | 2016-06-20 22:09

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

volzhs | 2016-06-21 02:12

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

kubecz3k | 2016-06-21 07:45

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

volzhs | 2016-06-21 08:21

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)?

Warlaan | 2016-06-21 09:43

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

Zylann | 2016-06-21 12:42

:bust_in_silhouette: Reply From: Zylann

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.

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

Warlaan | 2016-06-21 14:10