Menu

Godot Scene Tree: Nodes, get_node(), $ and add_child()

How nodes and the scene tree work in Godot 4 from GDScript: parents and children, finding nodes with get_node() and $, adding and removing nodes in code, groups, and the order Godot calls _ready().

This page includes runnable editors. Edit, run, and see output instantly.

A Godot game is a tree of nodes. Each node does one job (draw a sprite, play a sound, detect a collision), and nodes hold child nodes. This script builds a small tree in code and prints it:

 ┖╴Main
    ┖╴Level
       ┠╴Player
       ┠╴Coin0
       ┖╴Coin1

In the editor you build the same tree in the Scene dock, and save it as a scene file (.tscn). A scene can be placed inside other scenes, so a Player scene, an Enemy scene and a Coin scene become parts of a Level scene.

Finding Nodes: get_node() and $

get_node() takes a path of names, relative to the node the script is on. $ is a shorter way to write it:

Score: 10
/root/Main/UI/Score
UI
true
PathMeans
$Playerthe child named Player
$UI/ScoreScore, a child of UI
$".." or get_parent()the parent
$"../Enemy"a sibling named Enemy
%Playerthe node marked as a unique name in this scene
/root/Gamean absolute path from the tree root, such as an autoload

A path depends on the tree's layout, so moving a node breaks the paths to it. For nodes you use often, right click them in the Scene dock and choose Access as Unique Name, then use %Name: it keeps working wherever the node moves inside the scene.

Adding and Removing Nodes

add_child() puts a node in the tree. queue_free() removes and deletes it at the end of the frame:

const CoinScene = preload("res://coin.tscn")

func spawn_coin(at: Vector2) -> void:
	var coin := CoinScene.instantiate()
	coin.position = at
	add_child(coin)

func _on_coin_collected(coin: Node) -> void:
	coin.queue_free()
  • instantiate() creates a scene with all its children. Node2D.new() creates a single bare node.
  • remove_child() takes a node out of the tree without deleting it, so you can add it somewhere else. reparent() does both in one call.
  • Prefer queue_free() to free() for nodes in the tree: it waits until the frame is done, so code that still uses the node this frame does not crash.

Children and Groups

3
Enemy0
Enemy1
Enemy2
3 in the group
true

Groups tag nodes anywhere in the tree. get_tree().call_group("enemies", "freeze") calls a method on every member, which is easier than keeping your own list.

The Order of _ready()

Godot calls _ready() on children before their parent, so a parent's _ready() can always use its children. _enter_tree() runs the other way, parents first:

enter Parent
enter Child
ready Child
ready Parent

This is why get_node() is safe in _ready() and fails in _init(): during _init() the node has no children yet and is not in the tree. The @onready annotation is the usual way to store a child node in a variable.

get_tree()

get_tree() returns the SceneTree, the object that runs the game. Common uses:

get_tree().paused = true                           # pause the game
get_tree().reload_current_scene()                  # restart the level
get_tree().change_scene_to_file("res://menu.tscn") # load another scene
get_tree().quit()                                  # close the game
await get_tree().create_timer(1.0).timeout         # wait one second

Frequently Asked Questions

What is the scene tree in Godot?

The scene tree is every node of the running game, arranged as parents and children under one root. A scene you make in the editor is a branch of nodes saved to a file, and running the game adds it to the tree. Godot calls _ready(), _process() and the other callbacks on nodes that are in the tree.

What does $ mean in GDScript?

$ is shorthand for get_node(). $Player is get_node("Player"), and $UI/Score walks a path of child names. Use quotes when a name has spaces: $"Main Menu".

How do I add a node from code in Godot?

Create it with new() for a single node, or instantiate() for a scene, then call add_child(node) on its future parent. The node enters the tree and its _ready() runs.

Why does get_node() return null or fail?

The path does not match the tree: a typo, a renamed node, or a path relative to the wrong node. It also fails when you call it before the node is in the tree, for example in _init(). Read nodes in _ready() or with @onready, and use get_node_or_null() when a node may be missing.

Coddy programming languages illustration

Learn to code with Coddy

Get started