Home » SceneTree in Godot

SceneTree in Godot

by Online Tutorials Library

SceneTree in Godot

Introduction

There’s not much more depth than this. This is when things start getting abstract, but don’t panic.

In the last tutorial, everything revolved around the concept of nodes. A view is simply a collection of nodes. They become active as soon as they enter the visual tree.

This concept is worth going into a little more detail. The visual system is also not a core component of Godot because it is possible to skip and write a script (or C ++ code) that talks directly to the server, but creating a game in this way will do a lot of work.

MainLoop

This is how Godot works internally. The OS is the class, which is the only instance that runs at the beginning. Later, all drivers, servers, scripting language, visual systems, etc. are loaded.

When initialization is complete, the OS must be supplied with the main loop to run. Up to this point, all this is working interns (we can check the main / main.cpp file in the source code if we are ever interested to see how it works internally).

The game, user program, or game, starts in the main loop. Some methods for initialization in this class are passive (frame-synchronized callback), fixed (physics-synchronized callback), and input. Again, this is low-level, and while making the game in Godot, our Menloop writing rarely makes sense.

SceneTree

One of the way to explain how Godot works is that it is a high-level game engine on low-level middleware.

The visual system is the game engine, while the OS and server are low-level APIs.

In either case, the visual system provides its main loop to the OS, Scene Tree. It is set and set automatically while playing a scene; No additional work is required.

It is essential to know that this class exists because it has some essential uses:

  • It includes the root viewport, in which a view is added as a child when it is first opened to be part of the scene tree (next to that)
  • It contains information about groups, and they have the means to call all nodes in the group or get their list.
  • This includes some global state functionality, such as setting pause mode or skipping the process.

When a node is part of a scene tree, the syntry singleton can be obtained only by calling Node .get_tree ().

Root viewport

The root viewport is at the top of the scene. Form a node, and it can be obtained in two different ways:

This node contains the main viewport, whatever is the child of a viewport is drawn inside it by default, so it would make sense that all nodes always have a node of this type at the top; Otherwise, nothing will be seen!

While other viewports are created in the scene (split-screen effects and such), this is the only one that is never created by the user. It is automatically built inside SceneTree.

While other viewports are created in the scene (for split-screen effects and such), this is the only one that is never created by the user. It is created automatically inside SceneTree.

Scenetree

When a node is connected indirectly or directly to the root viewport, it will become part of the scene tree.

This means that as explained in previous tutorial, it will get the _enter_tree() and _ready() callbacks (as well as _exit_tree()).

SceneTree in Godot

When nodes enter the view tree, they become active. They get access to everything they need from the process, input, 2D and 3D displays, notifications, play sound, groups, etc. When they are removed from the other scene tree, they lose access.

Tree order

Most node operations in Godot, Like drawing 2D, processing, or getting notifications, are done in tree order. This means that parents are siblings with a smaller rank in the tree order will get notified before the current node.

SceneTree in Godot

“Becoming active” by entering the Scene Tree

  1. A scene is loaded from the disk or created by scripting.
  2. The root node of the scene is added as either a child of the “root” viewport or to any child and grandchild of it.
  3. Each node of the newly added view will receive an “Enter_tree” notification (_enter_tree () callback in GDScript) in the top-to-bottom order.
  4. A notification, “ready” (_ready() callback in GDScript), is provided for convenience when a node and all their children are inside the active scene.
  5. When a scene is removed, they receive the “exit scene” notification (_exit_tree() callback in GDScript) in bottom-to-top order

Changing the current scene

After the scene is loaded, it is desired to change this scene for another one. The simple way to do is to use the SceneTree.change_scene() function:

This is a fast and useful way to switch scenes, but the drawback is that the game will stop until the new scene is loaded and played. At some point in your game, it can be desired to create an appropriate loading screen with progress bars, animated indicators, or thread (background) loading. This should be done automatically using autoloads (see next chapter!) And background loading.


Next Topic#

You may also like