Using SceneTree

Introduction

In previous tutorials, everything revolved around the concept ofnodes. Scenes are collections of nodes. They become active oncethey enter thescene tree.

MainLoop

The way Godot works internally is as follows. There is theOS class,which is the only instance that runs at the beginning. Afterwards, alldrivers, servers, scripting languages, scene system, etc are loaded.

When initialization is complete,OS needs to besupplied aMainLoopto run. Up to this point, all this is internals working (you can checkmain/main.cpp file in the source code if you are ever interested tosee how this works internally).

The user program, or game, starts in the MainLoop. This class has a fewmethods, for initialization, idle (frame-synchronized callback), fixed(physics-synchronized callback), and input. Again, this is lowlevel and when making games in Godot, writing your own MainLoop seldom makes sense.

SceneTree

One of the ways to explain how Godot works is that it's a high-levelgame engine over a low-level middleware.

The scene system is the game engine, while theOSand servers are the low-level API.

The scene system provides its own main loop to OS,SceneTree.This is automatically instanced and set when running a scene, no needto do any extra work.

It's important to know that this class exists because it has a fewimportant uses:

  • It contains the rootViewport, to which ascene is added as a child when it's first opened to becomepart of theScene Tree (more on that next).

  • It contains information about the groups and has the means to call allnodes in a group or get a list of them.

  • It contains some global state functionality, such as setting pausemode or quitting the process.

When a node is part of the Scene Tree, theSceneTreesingleton can be obtained by callingNode.get_tree().

Root viewport

The rootViewportis always at the top of the scene. From a node, it can be obtained intwo different ways:

get_tree().root# Access via scene main loop.get_node("/root")# Access via absolute path.

This node contains the main viewport. Anything that is a child of aViewportis drawn inside of it by default, so it makes sense that the top of allnodes is always a node of this type otherwise nothing would be seen.

While other viewports can be created in the scene (for split-screeneffects and such), this one is the only one that is never created by theuser. It's created automatically inside SceneTree.

Scene tree

When a node is connected, directly or indirectly, to the rootviewport, it becomes part of thescene tree.

This means that as explained in previous tutorials, it will get the_enter_tree() and_ready() callbacks (as well as_exit_tree()).

../../_images/activescene.webp

When nodes enter theScene Tree, they become active. They get accessto everything they need to process, get input, display 2D and 3D visuals,receive and send notifications, play sounds, etc. When they are removed from thescene tree, they lose these abilities.

Tree order

Most node operations in Godot, such as drawing 2D, processing, or gettingnotifications are done intree order, or top to bottom as seen in theeditor (also known as pre-order traversal):

../../_images/toptobottom.webp

For example, the top node in a scene has its_process() functioncalled first, then the node below it has its_process() function called,then the node below that and so on.

An important exception is the_ready() function: each parent node has its_ready() function called only after all its child nodes have their_ready() functions called, so that the parent knows its children arecompletely ready to be accessed. This is also known as post-order traversal.In the above image,NameLabel would be notified first (but only after itschildren, if it had any!), followed byName, etc., andPanel would benotified last.

The order of operations can also be overridden using theprocess_prioritynode property. Nodes with a lower number are called first. For example, nodeswith the priorities "0, 1, 2, 3" would be called in that order from left to right.

"Becoming active" by entering theScene Tree

  1. A scene is loaded from disk or created by scripting.

  2. The root node of that scene (only one root, remember?) is added aseither a child of the "root" Viewport (from SceneTree), or to anyof its descendants.

  3. Every node of the newly added scene will receive the "enter_tree"notification (_enter_tree() callback in GDScript) intop-to-bottom order (pre-order traversal).

  4. Every node will receive the "ready" notification (_ready()callback in GDScript) for convenience, once all its children havereceived the "ready" notification (post-order traversal).

  5. When a scene (or part of it) is removed, they receive the "exitscene" notification (_exit_tree() callback in GDScript) inbottom-to-top order (the exact reverse of top-to-bottom order).

Changing current scene

After a scene is loaded, you may want to change this scene foranother one. One way to do this is to use theSceneTree.change_scene_to_file()function:

func_my_level_was_completed():get_tree().change_scene_to_file("res://levels/level2.tscn")

Rather than using file paths, one can also use ready-madePackedScene resources using the equivalentfunctionSceneTree.change_scene_to_packed(PackedScene scene):

varnext_scene=preload("res://levels/level2.tscn")func_my_level_was_completed():get_tree().change_scene_to_packed(next_scene)

These are quick and useful ways to switch scenes but have the drawbackthat the game will stall until the new scene is loaded and running. Atsome point in the development of your game, it may be preferable to create proper loadingscreens with progress bar, animated indicators or threaded (background)loading. This must be done manually usingSingletons (Autoload)andBackground loading.


User-contributed notes

Please read theUser-contributed notes policy before submitting a comment.