0 votes

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

in Engine by (270 points)

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.

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

1 Answer

0 votes

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)
by (5,240 points)
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.