0 votes

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

in Engine by (17 points)

1 Answer

0 votes
Best answer

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 printstraynodes () function tells you which nodes are orphaned, that is, no parent outside the scene tree. It only works in debugging mode.

by (2,058 points)
selected by

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]

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]

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

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.