Godot Nodes & Scenes (4.x)
Compose games from nodes and scenes, instance them at runtime, and access the tree
without crashing on freed or missing nodes. Targets Godot 4.7.
When to use
- Use when structuring
.tscn scenes, choosing how to break a feature into nodes,
instancing a PackedScene (bullets, enemies, UI), or setting up autoload singletons.
- Use when debugging
get_node() / $Path returning null, or "Attempt to call on a
previously freed instance".
When not to use: GDScript language/syntax → godot-gdscript; signal-based
decoupling → godot-signals-groups; physics bodies/collisions → godot-physics.
Core workflow
- Model with composition. A scene is a tree of nodes saved as
.tscn. Build small,
single-purpose scenes (Player, Bullet, Enemy) and compose larger scenes from them.
Favor adding child nodes over deep inheritance.
- Make a scene reusable by giving its root a script and exposing
@export config.
Save it; it becomes a PackedScene you can instance many times.
- Instance at runtime with
preload/load → scene.instantiate() →
add_child(instance). Set position/state after adding (or before, both work).
- Access nodes safely. Use
@onready var x = $Path for fixed children; use unique
names (%Name) for nodes deep in the tree; never assume a node still exists.
- Use autoloads for global state/services (game state, audio, scene switching) —
registered in Project Settings > Globals (Autoload), accessible by name everywhere.
- Free nodes with
queue_free() and guard later access with is_instance_valid().
- Check references before you finish. After creating, moving or renaming scenes,
resources or scripts, run
python scripts/check_godot_project.py <project-dir> (no
Godot install needed; --json for machine-readable output). Fix every ERROR (missing
or wrong-case res:// paths, undeclared ExtResource/SubResource ids, bad parent=
paths, missing main scene or autoload) and re-run until it exits 0. Warnings such as a
stale path= that Godot still finds by uid are safe but worth fixing. Run the script
path relative to this skill directory, or resolve the installed skill path first. It
needs Python 3.10+ and nothing else, and it does not replace opening the project in Godot.
Patterns
1. Instance a scene at runtime
extends Node2D
const BULLET := preload("res://bullet.tscn") # preload: loaded at compile time
func shoot(at: Vector2, dir: Vector2) -> void:
var bullet := BULLET.instantiate() # create an instance of the scene
bullet.global_position = at
bullet.direction = dir # set exported/public state
add_child(bullet) # now it's in the tree and runs
2. Safe node access: $, get_node_or_null, and unique names
@onready var label: Label = $UI/Label # $ is sugar for get_node("UI/Label")
@onready var health_bar: ProgressBar = %HealthBar # % = scene-unique name (rename-proof)
func update() -> void:
var optional := get_node_or_null("Maybe/Missing") # returns null instead of erroring
if optional:
optional.queue_free()
3. An autoload singleton (global game state)
# game_state.gd — add in Project Settings > Globals > Autoload as "GameState".
extends Node
var score := 0
signal score_changed(value: int)
func add_score(points: int) -> void:
score += points
score_changed.emit(score) # any scene can: GameState.score_changed.connect(...)
4. Change the running scene
func go_to_level_2() -> void:
# Swaps the current scene for another. Frees the old scene tree.
get_tree().change_scene_to_file("res://levels/level_2.tscn")
# Or, with a preloaded PackedScene:
# get_tree().change_scene_to_packed(LEVEL_2)
Pitfalls
$Path / get_node() return null or error when the path is wrong or the node
isn't in the tree yet. Use @onready, verify the path matches the scene, or use
get_node_or_null() for optional nodes.
- Renaming a node breaks
$Path. Use unique names (%Name, set via right-click
Access as Unique Name) so deep references survive renames and reparenting.
free() mid-frame can crash other code still using the node. Prefer
queue_free() (deletes at end of frame) and check is_instance_valid(node).
- Setting child state before
add_child() is fine, but _ready() on the child only
runs after it enters the tree — don't expect its @onready vars before that.
- Autoload order matters: autoloads are added before the main scene, in list order.
An autoload can't depend on the main scene existing yet.
instance() was renamed to instantiate() in Godot 4. preload runs at parse
time (path must be constant); load runs at runtime (path can be a variable).
change_scene_to_file() is deferred, and the old scene leaves the tree at once. The
outgoing scene is removed from the tree immediately (its get_tree() now returns null,
and get_tree().current_scene is null), then at the end of the frame it is freed and the
new scene is added. Code after the call must not touch get_tree() from the old scene. To
act on the new scene, do it from the new scene's _ready(), or from an autoload with
await get_tree().scene_changed (Godot 4.5+). change_scene_to_node(node) (4.6+) swaps
in a node you have already instanced and configured.
References
- For scene inheritance,
owner/ownership when saving scenes from code, groups vs
unique names, and node-path edge cases, read references/tree-and-instancing.md.
scripts/check_godot_project.py — read-only reference checker for a Godot 4.x project;
--help lists every check and the exit codes.
Related skills
godot-gdscript — language, lifecycle, and @onready.
godot-signals-groups — decouple instanced scenes from their spawner.
godot-resources — share data between instances without duplicating it.
save-systems — persist scene/game state across runs.
1---2name: godot-nodes-scenes3description: >4 Structure a Godot 4.7 project with the scene tree and node composition: build5 reusable scenes, instance PackedScenes at runtime, navigate the tree safely,6 and register autoload singletons. Use when designing .tscn scenes, deciding how7 to split nodes, spawning instances with instantiate(), wiring autoloads, or8 fixing "node not found"/freed-node errors in a Godot project.9metadata:10 source: https://skillmd.com/skills/junmystery/godot-nodes-scenes11---1213# Godot Nodes & Scenes (4.x)1415Compose games from nodes and scenes, instance them at runtime, and access the tree16without crashing on freed or missing nodes. Targets **Godot 4.7**.1718## When to use1920- Use when structuring `.tscn` scenes, choosing how to break a feature into nodes,21 instancing a `PackedScene` (bullets, enemies, UI), or setting up autoload singletons.22- Use when debugging `get_node()` / `$Path` returning `null`, or "Attempt to call on a23 previously freed instance".2425**When _not_ to use:** GDScript language/syntax → `godot-gdscript`; signal-based26decoupling → `godot-signals-groups`; physics bodies/collisions → `godot-physics`.2728## Core workflow29301. **Model with composition.** A scene is a tree of nodes saved as `.tscn`. Build small,31 single-purpose scenes (Player, Bullet, Enemy) and compose larger scenes from them.32 Favor adding child nodes over deep inheritance.332. **Make a scene reusable** by giving its root a script and exposing `@export` config.34 Save it; it becomes a `PackedScene` you can instance many times.353. **Instance at runtime** with `preload`/`load` → `scene.instantiate()` →36 `add_child(instance)`. Set position/state _after_ adding (or before, both work).374. **Access nodes safely.** Use `@onready var x = $Path` for fixed children; use unique38 names (`%Name`) for nodes deep in the tree; never assume a node still exists.395. **Use autoloads for global state/services** (game state, audio, scene switching) —40 registered in Project Settings > Globals (Autoload), accessible by name everywhere.416. **Free nodes with `queue_free()`** and guard later access with `is_instance_valid()`.427. **Check references before you finish.** After creating, moving or renaming scenes,43 resources or scripts, run `python scripts/check_godot_project.py <project-dir>` (no44 Godot install needed; `--json` for machine-readable output). Fix every `ERROR` (missing45 or wrong-case `res://` paths, undeclared `ExtResource`/`SubResource` ids, bad `parent=`46 paths, missing main scene or autoload) and re-run until it exits 0. Warnings such as a47 stale `path=` that Godot still finds by uid are safe but worth fixing. Run the script48 path relative to this skill directory, or resolve the installed skill path first. It49 needs Python 3.10+ and nothing else, and it does not replace opening the project in Godot.5051## Patterns5253### 1. Instance a scene at runtime5455```gdscript56extends Node2D5758const BULLET := preload("res://bullet.tscn") # preload: loaded at compile time5960func shoot(at: Vector2, dir: Vector2) -> void:61 var bullet := BULLET.instantiate() # create an instance of the scene62 bullet.global_position = at63 bullet.direction = dir # set exported/public state64 add_child(bullet) # now it's in the tree and runs65```6667### 2. Safe node access: $, get_node_or_null, and unique names6869```gdscript70@onready var label: Label = $UI/Label # $ is sugar for get_node("UI/Label")71@onready var health_bar: ProgressBar = %HealthBar # % = scene-unique name (rename-proof)7273func update() -> void:74 var optional := get_node_or_null("Maybe/Missing") # returns null instead of erroring75 if optional:76 optional.queue_free()77```7879### 3. An autoload singleton (global game state)8081```gdscript82# game_state.gd — add in Project Settings > Globals > Autoload as "GameState".83extends Node8485var score := 086signal score_changed(value: int)8788func add_score(points: int) -> void:89 score += points90 score_changed.emit(score) # any scene can: GameState.score_changed.connect(...)91```9293### 4. Change the running scene9495```gdscript96func go_to_level_2() -> void:97 # Swaps the current scene for another. Frees the old scene tree.98 get_tree().change_scene_to_file("res://levels/level_2.tscn")99 # Or, with a preloaded PackedScene:100 # get_tree().change_scene_to_packed(LEVEL_2)101```102103## Pitfalls104105- **`$Path` / `get_node()` return `null` or error** when the path is wrong or the node106 isn't in the tree yet. Use `@onready`, verify the path matches the scene, or use107 `get_node_or_null()` for optional nodes.108- **Renaming a node breaks `$Path`.** Use **unique names** (`%Name`, set via right-click109 > Access as Unique Name) so deep references survive renames and reparenting.110- **`free()` mid-frame can crash** other code still using the node. Prefer111 `queue_free()` (deletes at end of frame) and check `is_instance_valid(node)`.112- **Setting child state before `add_child()`** is fine, but `_ready()` on the child only113 runs _after_ it enters the tree — don't expect its `@onready` vars before that.114- **Autoload order matters**: autoloads are added before the main scene, in list order.115 An autoload can't depend on the main scene existing yet.116- **`instance()` was renamed** to `instantiate()` in Godot 4. `preload` runs at parse117 time (path must be constant); `load` runs at runtime (path can be a variable).118- **`change_scene_to_file()` is deferred, and the old scene leaves the tree at once.** The119 outgoing scene is removed from the tree immediately (its `get_tree()` now returns `null`,120 and `get_tree().current_scene` is `null`), then at the end of the frame it is freed and the121 new scene is added. Code after the call must not touch `get_tree()` from the old scene. To122 act on the new scene, do it from the new scene's `_ready()`, or from an autoload with123 `await get_tree().scene_changed` (Godot 4.5+). `change_scene_to_node(node)` (4.6+) swaps124 in a node you have already instanced and configured.125126## References127128- For scene inheritance, `owner`/ownership when saving scenes from code, groups vs129 unique names, and node-path edge cases, read `references/tree-and-instancing.md`.130- `scripts/check_godot_project.py` — read-only reference checker for a Godot 4.x project;131 `--help` lists every check and the exit codes.132133## Related skills134135- `godot-gdscript` — language, lifecycle, and `@onready`.136- `godot-signals-groups` — decouple instanced scenes from their spawner.137- `godot-resources` — share data between instances without duplicating it.138- `save-systems` — persist scene/game state across runs.