How to label a node as Non-Destroyable during runtime?

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

I can label a node as a singleton using the Auto load menu in the editor but how would I do so during runtime?

Not sure if I understood what you really want. Do you want to have global access of your node? Because if it is, you can add the node to an array variable on a Singleton you already have - but if you delete your node, the reference on the array will become null.

henriquelalves | 2017-03-16 04:22

are you trying to make a node like auto load menu in editor?

volzhs | 2017-03-16 05:27

:bust_in_silhouette: Reply From: avencherus

It’s not that the node is non-destroyable. It just exists at a level of the SceneTree that isn’t freed when scenes change. It’s the same concept as a variable outside the scope of a function.

The singletons and the active scene get added to the root viewport. To see this in action, create a singleton and do the following:

func _ready():
	for child in get_tree().get_root().get_children():
		printt(child, child.get_name())

So if you’d like to scope things in the same fashion in code you could manage the children manually by retrieving the root viewport. Then applying the methods add_child(), remove_child(), move_child(), etc.

var root = get_tree().get_root()
root.add_child(my_node)