How to return removed child?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By GIN

Sorry for my English. But I don’t know how to return removed child. When i trying use add_child() for return, script works fine, but node isn’t appear. I look at this node with print() and it printed me “[Object:null]”
Part of the code:
if Scenes.has(key_for_comparison): remove_child(get_node("%s" % str(GlobalScript.cur_scene))) add_child(get_node("%s" % str(Scenes[key_for_comparison].Scene))) print(get_node("%s" % str(GlobalScript.cur_scene)))

:bust_in_silhouette: Reply From: estebanmolca

If you save a reference to the node before deleting it, would it work?

func _ready ():
print_stray_nodes ()
var node_for_delete = $ Sprite
remove_child ($ Sprite)
print_stray_nodes ()
add_child (node_for_delete)
print_stray_nodes ()

I’m not very clear about how everything happens when a node is removed from the scene, apart from the fact that there are several properties that are not very clear to me what they do, such as establishing an owner for a node. By the way, the print_stray_nodes () function tells you which nodes are orphaned, that is, no parent outside the scene tree. It only works in debugging mode.

Hello, thanks for answer, but it don’t work…
When I save reference and print() i have a result.
After performing the deletion, the link becomes empty.
Until deleting
[Node2D:1745]
After deleting
[Object:null]

GIN | 2021-03-09 19:16

in your original example, it does not work because you are using get_node(path) after the node is removed, but since the node is not in the tree there is no path to it.
however the code from estebanmolca should work, can you write down the exact code you used that gives the below as result?

Until deleting
[Node2D:1745]
After deleting
[Object:null]

Andrea | 2021-03-09 21:38

Oh, sorry, guys it’s my fault. That work fine!!! Thank you very much!

GIN | 2021-03-10 13:29