Using SceneTree
Introduction
scene tree.
MainLoop
OS class, which is the only instance that runs at the beginning. Afterwards, all drivers, servers, scripting languages, scene system, etc are loaded. OS needs to be supplied a MainLoop to run. Up to this point, all this is internals working (you can check main/main.cpp file in the source code if you are ever interested to see how this works internally). The user program, or game, starts in the MainLoop. This class has a few methods, for initialization, idle (frame-synchronized callback), fixed (physics-synchronized callback), and input. Again, this is low level 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 level game engine over a low level middleware. OS and servers are the low level API. SceneTree. This is automatically instanced and set when running a scene, no need to do any extra work. It’s important to know that this class exists because it has a few important uses:
- Viewport, to which a scene is added as a child when it’s first opened to become part of the Scene Tree (more on that next).
- It contains information about the groups and has the means to call all nodes in a group or get a list of them.
- It contains some global state functionality, such as setting pause mode or quitting the process.
SceneTree singleton can be obtained by calling Node.get_tree().
Root viewport
Viewport is always at the top of the scene. From a node, it can be obtained in two different ways: GDScript C#get_tree().get_root() # Access via scene main loop.get_node("/root") # Access via absolute path.
Viewport is drawn inside of it by default, so it makes sense that the top of all nodes is always a node of this type otherwise nothing would be seen. While other viewports can be created in the scene (for split-screen effects and such), this one is the only one that is never created by the user. It’s created automatically inside SceneTree.GetTree().GetRoot(); // Access via scene main loop.GetNode("/root"); // Access via absolute path.
Scene tree
scene tree._enter_tree()and_ready()callbacks (as well as_exit_tree()). Scene Tree, they become active. They get access to everything they need to process, get input, display 2D and 3D visuals, receive and send notifications, play sounds, etc. When they are removed from the scene tree, they lose these abilities.Tree order
Most node operations in Godot, such as drawing 2D, processing, or getting notifications are done in tree order. This means that parents and siblings with a lower rank in the tree order will get notified before the current node.Scene Tree
- A scene is loaded from disk or created by scripting.
- The root node of that scene (only one root, remember?) is added as either a child of the “root” Viewport (from SceneTree), or to any child or grandchild of it.
- Every node of the newly added scene, will receive the “enter_tree” notification ( _enter_tree() callback in GDScript) in top-to-bottom order.
- An extra notification, “ready” ( _ready() callback in GDScript) is provided for convenience, when a node and all its children are inside the active scene.
- When a scene (or part of it) is removed, they receive the “exit scene” notification ( _exit_tree() callback in GDScript) in bottom-to-top order
Changing current scene
SceneTree.change_scene() function: GDScript C#func _my_level_was_completed(): get_tree().change_scene("res://levels/level2.tscn")
PackedScene resources using the equivalent function SceneTree.change_scene_to(PackedScene scene): GDScript C#public void _MyLevelWasCompleted(){ GetTree().ChangeScene("res://levels/level2.tscn");}
var next_scene = preload("res://levels/level2.tscn")func _my_level_was_completed(): get_tree().change_scene_to(next_scene)
Background loading.public void _MyLevelWasCompleted(){ var nextScene = (PackedScene)ResourceLoader.Load("res://levels/level2.tscn"); GetTree().ChangeSceneTo(nextScene);}
