What You’ll Learn in This Hour:
What Godot’s Scene System entails
What nodes are and how to use them
How nodes and scenes relate to each other
How resources are managed inGodot
How to explore the possibilities of the scene system
One special part of Godot and what makes it different from other engines is its Scene System. Godot is based upon nodes and how they
compose a scene. In this hour, you’ll learn how this system works and what can be done with it. While this chapter may be more abstract, it is
the foundation upon whichGodot is built, and will be helpful to design and develop all of your games.
Nodes and Scenes
All Godot games are based on nodes and scenes. Nodes are atoms of game functionality, and are put together to make scenes. These
scenes can then be combined to make bigger and more complex scenes. This is what you have to take into account when designing your
games.
Node
A Node is the most fundamental element of the game-building process. It is a basic block of functionality that has a name, properties, and a
special function. A node can be extended via inheritance in the OOP (object-oriented programming) sense, which is done inside the engine
itself or via modules. For your game functionality, the node can be extended with a script to have custom and specific actions such as
decreasing the life of the player when hit by a bullet.
When you click on the
Add Node button of the Scene dock, you are greeted with a large list of nodes from which to choose. While it may
seem daunting to learn what all these nodes do, you don’t need to do it all at once. The nodes have descriptive names that are easy to
remember and are organized by their inheritance tree: the nodes down the tree are specializations of their parent. Also in that dialog, you can
either click a node to select it and see its description on the box below or simply hover your mouse cursor over any node to see a tooltip with
the description.
There are three main types of nodes:
Node2D,
Spatial, and
Control. These are the bases for most of the node types and relates
to their specific functions (see Figure 2.1). Node2D is the base for all nodes in 2D games, as Spatial is for all 3D nodes. There is some
correspondence between 2D and 3D nodes, e.g., Camera (derived from Spatial) versus Camera2D (derived from Node2D). This relationship
makes it easy to alternate between 2D and 3D games with maintained familiarity. The Control node is the base for all GUI, which can be used
in both 2D and 3D games.
FIGURE 2.1
Create New Node dialog, which contains a list of all nodes you can add to a scene.
Every node can have a callback to process within the game loop. This makes sure, for instance, that the AnimationPlayer node has a chance
to update the Animation each frame and a Particles node can process in the next batch. This also applies to custom scripts, so you can check
if the user has pressed a button that’ll move the player character.
NOTE
Game Loop
A commonplace element in game development is the game loop. This is a code that gets executed every frame to update the game world,
check user input, and draw the result to the screen. Godot has two types of game loops: Idle and Fixed. The Idle process is so called because
every frame is drawn as fast as possible. The Fixed processing has a fixed timestep that is synchronized with the physics loop, with a default
of 60 FPS (Frames Per Second).
Scene
A node by itself cannot do much. To explore its full potential, it needs to be combined with other nodes to expand functionality. That’s why we
make Scenes. A scene is nothing more than a node with other nodes as children, forming a tree structure. It is important to notice that the
scene structure is formed by the fact that nodes can have multiple children but only a single parent. A scene starts with one node (called the
root), which can have any number of children and grandchildren (see Figure 2.2).
FIGURE 2.2
Scene Tree of Godot’s Platformer 3D demo project
111111111
22222222This tree structure guarantees how the nodes interface with its parent and children. Here’s a breakdown of the tree properties:
Nodes are processed in tree order. The root node will receive the process callback before its children.
Nodes are drawn in tree order. The parents are drawn before the children, which means that children cover their parents (this does not
apply to 3D nodes).
Nodes inherit the transform of their parents. This means that if a node changes position, rotation, or scale, it’ll be applied to all of its
children. Children’s transforms are relative to their parent.
Resources
Another special type of object inside Godot is the Resource. While nodes represent behaviors (physics interaction or UI controls, for
example), Resources represent data. As an example, the
AnimationPlayer node is responsible for playing and stopping Animations. To
know what it needs to do, it needs the data that comes from the
Animation Resource. An Animation contains all of the properties of
nodes that need to be animated, and the AnimationPlayer reads this data to make the needed changes in the scene.
There are a lot of core Resource types in the engine, but you rarely need to create the manually. When you set the texture property of a Sprite,
Godot creates a Resource from the PNG file that you set. The same happens when you create an Animation inside the AnimationPlayer inside
the editor. Most of the time, Resources are created with specific contextual editors or by import plugins.
If you need to create a Resource manually, you can do so by clicking the
New Resource button in the Inspector dock. A dialog much like
the one you see when you add nodes will show up, but this one shows the Resource types instead. It is interesting to take a look at this list and
see all types of Resources provided byGodot, but you rarely (if ever) need to create Resources this way. Resources can also be created in the
Inspector property dropdown if the property requires a Resource type.
NOTE
Custom Nodes and Resources
It is possible to create your own node types by creating new editor plugins. Custom nodes and Resources appear in the dialog list, so they
behave like core types. How to do this won’t be covered in this book, but it can be found inGodot engine’s official documentation.
Inspecting and Editing Resources
Resources can be edited in the Inspector-like nodes. While not all Resource types have meaningful exposed properties in the editor, some
types greatly benefit from the Inspector. If a property requires a Resource, you can open it by clicking the property itself, and it’ll open in the
Inspector dock. You can go back to the previously edited object by pressing the
Back button or by clicking the
History button.
Some Resource types have custom editors. For instance, if you create
a
Theme Resource, the proper contextual editor will appear where
you can edit the theme options. This happens if you click a property that contains a theme but also if you double-click the file in the FileSystem
dock.
Resource Locations and Uniqueness
Resources inGodot can either be saved inside the scene file or as its own separate file. There are advantages and disadvantages in both
workflows, and it’s usually best decided case by case. In general, if you reuse the Resource across scenes, it is best to save as a separate file
so it can be load in multiple places.
You need to understand that Resources are shared by default. If you duplicate a node in the tree, the Resources they use will be shared, and if
you edit one, it’ll affect all instances that use it. Usually, this behavior is desired (Godot ensures that the Resource is loaded only once from the
disk, increasing performance), but if you want to have two different copies of the Resource, you need to make them unique. This can be done
by clicking on the
vertical ellipsis besides the property on the Inspector and selecting
Make Unique on the dropdown menu (see
Figure 2.3). You can also make all sub-Resources of a node unique by selecting the corresponding option in the Inspector
Tools menu.
FIGURE 2.3
Resource dropdown menu on the Inspector dock.
Combining Scenes
While a scene by itself can be quite powerful, it is easy to make it too big and difficult to maintain. A game is composed of many scenes. You
don’t need to see a scene as simply a “level” or an “area” of the game, but also as a collection of small composition blocks that can be
combined into larger scenes. For instance, your player can be a scene that is then put into the larger level scene.
Inheriting Scenes
There are two ways in which a scene can be extended. They can be either Inherited or Instanced. Inheritance is a way to create derived scenes
from a same base. You may have a default structure with nodes and properties set to a character. Then your player is a character that can
benefit from the structure by tweaking a few values with an Inherited Scene. The enemy is also a character and can be inherited of the same
scene. If for some reason the way characters are structured changes, you can simply change the base scene.
Inheritance works in scenes similar to how it works in general OOP. You have a base scene with some general structure and the child scene
that inherits from it, specializing the structure by adding other nodes to the tree and changing the base properties. Inheriting scenes is the
same as instancing themas the root node for a new scene.
111111111
22222222You can create one by clicking on the New Inherited Scene option under the Scene menu. The editor will open a dialog where you can select
the base scene.
Instancing Scenes
When you are designing your level, you need to put several enemies in place. These enemies all look and behave alike, so you can simply
copy and paste the nodes to put them in various places. However, if later you need to change how they look, you’ll need to find all the copies to
change them. That’s where Scene Instancing can help.
Instead of making your enemies directly in the level scene, you can create a new scene with just the enemy. You can customize it until you’re
satisfied, then make an instance of it in the level scene. An instance means there’s a reference to another scene inside the tree. Since scenes
always have a single root node, they can behave like a node, which you can place anywhere in the tree.
When you change the original scene on the disk, all instances will be updated to reflect the new values. Note that if you change a property of an
instanced scene, it won’t affect the original, and if the original ever changes, this property won’t be updated. To revert the property to its original
value, you need to click the
Revert button beside the property in the Inspector dock.
TRY IT YOURSELF
Instancing Scenes
There are four ways to instance a scene in the editor. Let’s try each of them by following these steps:
1. Make a simple scene that can be instanced. It can contain just the root node. Save this scene as “subscene.tscn”.
2. Create a new scene and add a
Node as the root.
3. Save the scene as “main-scene.tscn”.
4. Click on the
Instance Scene button in the Scene dock.
5. In the file dialog that appears, select “subscene.tscn” and click on the Open button.
6. You can see on the Scene dock a new child node with a special
Subscene icon. This means this node comes from another
scene.
7. Now locate the “subscene.tscn” in the FileSystem dock.
8. Drag the “subscene.tscn” file from the FileSystem dock and drop it over the root Node on the Scene dock. That’s the second
way to instance scenes.
9. Select the root Node in the Scene dock.
10. Drag the “subscene.tscn” file from the FileSystem to the main editor viewport. You can see a preview of how it’ll look.
11. Drop the scene anywhere in the Viewport. It’ll be placed there as a child of the selected node. This is the third way to instance a
scene.
12. On the Scene dock, select the root Node.
13. Back on the FileSystem dock, right-click “subscene3.tscn” the file and select Instance on the menu that appears (see Figure
2.4). That is the last way to instance a scene in the editor.
FIGURE 2.4
Instanced scenes shown in the tree.
TIP
When to Create a New Scene
It’s tempting to start a new scene and add nodes until you’re satisfied. But it’s important to have a design idea before making the scenes of the
game. You can think of each isolated component as a scene. A “Player Character” likely will be its own scene. The protagonist’s house is
bound to be its own scene, too, and maybe each piece of furniture is a scene, especially if they are reused in other houses.
Some game engines have a distinction between “scenes” and “prefabs.” Godot does not make such a distinction. If you are used to make
prefabs in other engines, make it a scene when using Godot. This makes the workflow much more flexible, and you can make design
diagrams thinking about how the Scene Tree will be constructed by concrete objects.
Managing Subscenes
When you break down your game in small, single-function scenes, it’s much easier to deal with the content. However, you need to know how to
maintain the larger scenes that are composed of the smaller ones. This section shows a few tips on how to manage your scenes and ease the
way you edit them.
Opening Instanced Subscenes
Sometimes, you need to tweak something in the original scene to reflect everywhere it’s instanced. You don’t need to fiddle around the file
system to find it. You just need to click the
Open Subscene button that appears beside the node in the Scene dock. When you click this
icon, the original scene that was instanced will open in another tab inside the editor. If you make changes and save the subscene, then go back
111111111
22222222to the where it was instanced, the editor will update the view to reflect the changes.
Editing Subscene’s Children
By default, only the root node of the instanced scene is shown. This helps keep the Scene dock clear of clutter and encapsulates the
functionality of subscenes. However, sometimes you may want to change a property of a child of the subscene’s root. This is possible if you
enable the Editable Children option. You can do that by right-clicking the node in the Scene dock and enabling the option in the context menu.
Note that any property of the instanced that you edit will save in the main scene. So, if you change the original subscene later, the changes
won’t reflect in the main scene, because they were overridden there. This is the case even if you disable the Editable Children option later. You
need to revert the properties that you don’t want to override.
Unlinking an Inherited Scene
It may be the case that you edited the subscene so much that it’s simply easier to remove the link to the original scene. Or you simply want to
copy a subscene because the structure is similar, but it is unrelated to the original. In these cases, you can remove the instanced status of the
subscene by clicking on the Discard Instancing option in the right-click context menu of the node.
When the instancing is discarded, the subscene tree will integrate in the main scene tree and become part of it. This is like making the
subscene part of the main scene from the beginning.
Splitting a Scene
There are cases when you want to do the opposite: the scene has become too big and certain elements can be made into a subscene to be
reused. Godot also offers the possibility to save a branch of the tree as a subscene. If you right-click the node in the Scene dock, you can
select the Save Branch as Scene option. This will make a file dialog appear so you can save the subscene. After saving it, the branch will
turn into an instanced scene right way.
NOTE
Cyclic Dependencies
Godot will detect and stop you from making cyclic dependencies. This means that if scene A instances scene B, which then instances scene
C, Godot won’t let you instance scene A inside scene C. Because A depends on B, which depends on C, adding A as a subscene will make
the scenes themselves children, which is an undefined behavior.
Summary
In this hour, you were introduced to Godot’s Scene System. You learned about Scenes, Nodes, and Resources. You saw how a Scene is
composed as a tree of nodes and learned that nodes represent behav
io
rs, while resources contain data. You learned how to split and join
scenes to help you compose your game.
Q&A
Q. Can a node have more than one parent?
A. No, a node can have only one parent. This ensures the tree structure.
Q. What is best for performance: a big monolithic scene or a lot of instanced subscenes?
A. This depends on the project structure, but usually there’s no noticeable performance impact on instancing subscenes. In fact, if these
subscenes are reused, it’s likely more performant to instance them.
Q. What happens if I delete a scene or resource fromthe disk that is used somewhere?
A. When you open the scene inside the editor, a dialog will appear so you can fix the missing dependencies. When deleting from the
FileSystem dock, the editor will warn you if the file is used elsewhere.
Workshop
Here are a few questions for you to review the contents of this second hour.
Quiz
1. What is the fundamental element of a game?
2. True or False: Resources are shared between nodes by default.
3. How are subscenes indicated as such?
4. In which order are the nodes processed?
5. True or False: Resources are loaded only once from the disk.
Answers
1. The node is the base element to create games.
2. True. When a Node that contains a Resource is duplicated, or when the Resource is copied using the copy/paste function of the Inspector,
the resources are shared by default unless explicitly made unique.
111111111
222222223. There’s a special icon in the Scene dock indicating that the node is an instanced scene.
4. They’re processed in tree order, and the parent is processed before its children.
5. True. If the game requests a resource that is already in its memory, Godot won’t load it again from the disk.
Exercises
This hour is more conceptual than practical. You should take some time doing these exercises to help you visualize the concepts inside the
editor and see how everything is affected.
1. Create a new scene and add a
Node as the root node.
2. Rename the root node “MasterScene” and save the scene as “master-scene.tscn”.
3. Create another scene and add a
Sprite node.
4. Rename the node “SubScene” and save the scene as “sub-scene.tscn”.
5. Instance the “sub-scene.tscn” inside the “main-scene.tscn”.
6. Go to the “sub-scene.tscn” in editor and change the position of the only node there. Save the scene.
7. Go back to the “main-scene.tscn” and observe how the subscene is updated.
8. Change the position of the subscene inside the master scene and save it.
9. Go to the subscene in editor and once again change the position of the root. Save the scene.
10. When you go back to the master scene, notice how the Sprite’s position isn’t updated anymore.
11. Select the SubScene node on the master scene.
12. In the Inspector dock, find the Transform > Position property. Click on the
Revert button.
13. Observe how the node changes position to the place saved in the subscene.