Unclear about singletons

GarromOrcShamanGarromOrcShaman Posts: 172Member

Hi. If i set script to autoload, engine creates a node with script attached to. However, what happens to singleton node when i replace root node with get_tree().change_scene("res://lets_say_there_is_scene_in_this_folder_with_this_name") ?
I wonder if singleton node get reparented or wiped out and replaced.I need to keep variables and some functions between menu and levels and i really want to know what happens to singleton node. I also want ask you if I can get multiple instances of singleton node by entring the scene where node already was what may happens in Unity with dontDestroyOnLoad flag (That does not mean i want it to happen. Vice versa, it was big problem in Unity).
Thank
-Garrom

Tagged:

Tags :

Answers

  • arthurarthur Posts: 34Member
    Answer ✓

    When you change_scene() or change_scene_to(), the singleton (or autoload) nodes are preserved. The function does not replace root node. It just changes active scene, without touching singleton nodes.

  • GarromOrcShamanGarromOrcShaman Posts: 172Member

    @arthur said:
    When you change_scene() or change_scene_to(), the singleton (or autoload) nodes are preserved. The function does not replace root node. It just changes active scene, without touching singleton nodes.

    So if is everything inside tree, the tree what get replaced, where is singleton node then ?

  • NeoDNeoD Posts: 141Member

    The editor can display the tree graph during runtime. Look at the debugger tabs in Godot 2.1, or in the scene dock in Godot 3.0 while the game is running.

  • arthurarthur Posts: 34Member
    Answer ✓

    @GarromOrcShaman said:

    @arthur said:
    When you change_scene() or change_scene_to(), the singleton (or autoload) nodes are preserved. The function does not replace root node. It just changes active scene, without touching singleton nodes.

    So if is everything inside tree, the tree what get replaced, where is singleton node then ?

    The engine is smart enough to just change node of current scene. It does not touch the root node or any of its children that is not the current scene node.
    Example:

    root
    |- singleton 1
    |- singleton 2
    L current scene node
    

    When you call change_scene(), the only node that will be changed is current scene node. The other nodes (root, singleton 1 and singleton 2) won't be affected at all.

Sign In or Register to comment.