Nodes, Scenes & the Scene Tree
Everything in a running Godot game is a node in one big tree. Nodes are small, single-purpose objects: a Sprite2D draws a texture, a CollisionShape2D gives a body its shape, a Timer counts down, an AudioStreamPlayer plays a sound. You build game objects by combining nodes, and you save a combination as a scene.
Nodes
Every node has:
- a name, unique among its siblings;
- a type (class), which decides its behaviour and its Inspector properties;
- children, which makes the tree;
- optionally a script, which extends its type with your code.
Node types inherit from each other. CharacterBody2D is a PhysicsBody2D, which is a CollisionObject2D, which is a Node2D (it has a position, rotation and scale), which is a CanvasItem (it can draw), which is a Node. Anything a Node2D can do, a CharacterBody2D can do too. The Create New Node dialog (Ctrl+A) shows this hierarchy.
| Family | Base class | Examples |
|---|---|---|
| 2D | Node2D |
Sprite2D, CharacterBody2D, Area2D, Camera2D, TileMapLayer |
| 3D | Node3D |
MeshInstance3D, CharacterBody3D, Camera3D, DirectionalLight3D |
| UI | Control |
Label, Button, VBoxContainer, TextureRect |
| Plain | Node |
Timer, AudioStreamPlayer, AnimationPlayer, your managers |
Scenes
A scene is a tree of nodes with one root, saved to a .tscn file. A player is a scene, so is a coin, a level, the main menu and a health bar. Scenes contain instances of other scenes, so a level scene contains a player instance and twenty coin instances (see Instancing Scenes).
Here is a small player scene as the editor saves it. You rarely edit these files by hand, but reading one makes clear that a scene is only data:
[gd_scene format=3]
[ext_resource type="Script" path="res://player/player.gd" id="1_k3r8p"]
[ext_resource type="Texture2D" path="res://player/player.png" id="2_q7w1z"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_mf4tn"]
size = Vector2(14, 22)
[node name="Player" type="CharacterBody2D"]
script = ExtResource("1_k3r8p")
[node name="Sprite" type="Sprite2D" parent="."]
position = Vector2(0, -11)
texture = ExtResource("2_q7w1z")
[node name="Hitbox" type="CollisionShape2D" parent="."]
position = Vector2(0, -11)
shape = SubResource("RectangleShape2D_mf4tn")ext_resourcelines point to other files.sub_resourceblocks are resources stored inside the scene (the hitbox's rectangle).parent="."means "child of the root". A grandchild would sayparent="Sprite".- Only properties that differ from the defaults are saved.
- The editor also writes
uid="uid://..."attributes, which keep references valid when files move.
The scene tree
When the game runs, the SceneTree owns a root Window (a viewport) called root. Your main scene is added under it, together with any autoloads. The runnable examples in this course add the script's node under root as Main, and you can build children in code just like the editor builds them from a .tscn:
extends Node2D
# frames: 0
func make(type_name: String, node_name: String, parent: Node) -> Node:
var n: Node = ClassDB.instantiate(type_name)
n.name = node_name
parent.add_child(n)
return n
func _ready():
var player := make("CharacterBody2D", "Player", self)
make("Sprite2D", "Sprite", player)
make("CollisionShape2D", "Hitbox", player)
var hud := make("CanvasLayer", "HUD", self)
make("Label", "ScoreLabel", hud)
print_tree_pretty()
for child in get_children():
print(child.name, " is a ", child.get_class()) ┖╴Main
┠╴Player
┃ ┠╴Sprite
┃ ┖╴Hitbox
┖╴HUD
┖╴ScoreLabel
Player is a CharacterBody2D
HUD is a CanvasLayer
In normal code you would write CharacterBody2D.new(); ClassDB.instantiate creates a node from a class name, which keeps this helper short. # frames: 0 tells the course's runner to stop right after _ready.
Finding nodes: paths
A node path is like a file path. Relative paths start from the node that asks, .. is the parent, and absolute paths start at /root. $Path is shorthand for get_node("Path"):
extends Node2D
# frames: 0
func _ready():
var player := CharacterBody2D.new()
player.name = "Player"
add_child(player)
var sprite := Sprite2D.new()
sprite.name = "Sprite"
player.add_child(sprite)
print(sprite.get_path())
print($Player/Sprite.get_class())
print(get_node("Player/Sprite") == sprite)
print(sprite.get_node("../..").name)
print(get_node_or_null("Player/Gun") == null)
player.position = Vector2(100, 50)
sprite.position = Vector2(0, -16)
print("local ", sprite.position, " global ", sprite.global_position)/root/Main/Player/Sprite
Sprite2D
true
Main
true
local (0.0, -16.0) global (100.0, 34.0)
The last line shows the most important property of the tree for 2D and 3D nodes: transforms are inherited. The sprite's position is relative to its parent, so moving the player moves the sprite. global_position gives the position in the world.
get_node on a path that doesn't exist prints an error and returns null; use get_node_or_null when a node is optional.
Scene-unique names (%)
Long paths break as soon as someone reorganises the scene. In the editor you can right-click a node and choose Access as Unique Name: its name gets a % badge, and any script in the same scene can reach it with %Name, wherever it sits:
@onready var name_tag: Label = %NameTag # instead of $Body/Sprite/Overlay/NameTagThis works for nodes owned by the same scene. Use it for the handful of nodes a script really needs, such as labels and timers.
Signals up, calls down
The tree also suggests how nodes should talk to each other:
- A parent may call methods on its children: it created them and knows they exist (
$Sprite.flip_h = true). - A child should not reach up into its parent's internals (
get_parent().get_parent().score += 1). It emits a signal instead, and whoever owns it decides what to do. That keeps the child reusable in other scenes.
You will see this rule again in Signals and in the field notes.
Test yourself
A Sprite2D is the child of a CharacterBody2D at (100, 50). The sprite's position is (0, -16). What is its global_position?
- (0, -16)
- (100, 50)
- (100, 34)
- (100, 66)
What does $Player/Sprite mean in a script?
- A global variable
get_node("Player/Sprite"): the Sprite child of this node's Player child- A path on disk
- A signal
In a .tscn file, what does a sub_resource block hold?
- A child scene
- A resource saved inside the scene file, such as a collision shape
- The script source code
- Editor settings