Change scenes manually

    GDScript   C#

    To complete the cycle and swap out the new scene with the old one, developers have a choice to make. Many strategies exist for removing a scene from view of the Viewport. The tradeoffs involve balancing operation speed and memory consumption as well as balancing data access and integrity.

    1. We can delete the existing scene. and SceneTree.change_scene_to() will delete the current scene immediately. Developers can also delete the main scene though. Assuming the root node’s name is “Main”, one could do to delete the whole scene.

    2. We can hide the existing scene. By changing the visibility or collision detection of the nodes, we can hide the entire node sub-tree from the player’s perspective.

      • Memory still exists.

      • Processing continues.

        • Pro: Nodes are still members of groups (since groups belong to the ).

    3. We can remove the existing scene from the tree. Assign a variable to the existing scene’s root node. Then use Node.remove_child(Node) to detach the entire scene from the tree.

    There are also cases where one may wish to have many scenes present at the same time. Perhaps one is adding their own singleton at runtime, or preserving a a scene’s data between scene changes (adding the scene to the root node).

    GDScript   C#

    Perhaps instead they wish to display multiple scenes at the same time using . This is optimal in cases where the intent is to render different content in different parts of the screen. Minimaps and split-screen multiplayer are good examples.

    Each option will have cases where it is best appropriate, so one must examine the effects of each and determine what path best fits their unique situation.