Loading...
Loading...
Structure a Godot 4.x project with the scene tree and node composition: build reusable scenes, instance PackedScenes at runtime, navigate the tree safely, and register autoload singletons. Use when designing .tscn scenes, deciding how to split nodes, spawning instances with instantiate(), wiring autoloads, or fixing "node not found"/freed-node errors in a Godot project.
npx skill4agent add gamedev-skills/awesome-gamedev-agent-skills godot-nodes-scenes.tscnPackedSceneget_node()$Pathnullgodot-gdscriptgodot-signals-groupsgodot-physics.tscn@exportPackedScenepreloadloadscene.instantiate()add_child(instance)@onready var x = $Path%Namequeue_free()is_instance_valid()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@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()# 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(...)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)$Pathget_node()null@onreadyget_node_or_null()$Path%NameAccess as Unique Name) so deep references survive renames and reparenting.
free()queue_free()is_instance_valid(node)add_child()_ready()@onreadyinstance()instantiate()preloadloadchange_scene_to_file()get_tree().current_scene_ready()ownerreferences/tree-and-instancing.mdgodot-gdscript@onreadygodot-signals-groupsgodot-resourcessave-systems