TExcelTAD
Home › Godot 4 with GDScript › Field Notes: Scene Architecture

Field Notes: Scene Architecture

Godot gives you very few rules about how to organise a game. That freedom is pleasant for a week and painful by month three, when a coin scene breaks because someone moved it under a new parent, or every script calls a Global autoload with forty variables. These notes are the habits that keep a Godot project easy to change. They are short on purpose: come back to them when a project starts to feel stiff.

1. Signals up, calls down

A node may call methods on its children (it created them or owns them, so it knows they exist). A node should not reach up to its parent or across to siblings; it emits a signal and lets whoever owns it decide what to do.

text
Player (CharacterBody2D)          ← connects child signals, calls child methods
├── Health (Node)                 ← emits changed / died, knows nothing about Player or HUD
├── Sprite (AnimatedSprite2D)     ← Player calls sprite.play("hurt")
└── Hurtbox (Area2D)              ← emits area_entered

The same idea in code: a reusable health component and a HUD, wired together by their parent.

gdscript · try it yourself
extends Node

# frames: 0
# A component: knows nothing about who owns it. It only signals up.
class HealthComponent extends Node:
	signal changed(current: int, maximum: int)
	signal died
	@export var max_health := 3
	var current := 0

	func _ready() -> void:
		current = max_health

	func damage(amount: int) -> void:
		if current <= 0:
			return
		current = maxi(current - amount, 0)
		changed.emit(current, max_health)
		if current == 0:
			died.emit()

class Hud extends Node:
	func show_health(current: int, maximum: int) -> void:
		print("  HUD: ", "#".repeat(current) + "-".repeat(maximum - current))

# The parent wires its children together: calls down, listens to signals coming up.
var health := HealthComponent.new()
var hud := Hud.new()

func _ready() -> void:
	health.name = "Health"
	add_child(health)
	add_child(hud)
	health.changed.connect(hud.show_health)
	health.died.connect(_on_died)
	for hit in [1, 1, 5]:
		print("hit for ", hit)
		health.damage(hit)    # call down

func _on_died() -> void:
	print("  Player: died, playing death animation")
hit for 1
  HUD: ##-
hit for 1
  HUD: #--
hit for 5
  HUD: ---
  Player: died, playing death animation

HealthComponent can now go on an enemy, a crate or a boss without changes. Signs you broke the rule: get_parent().get_parent(), get_node("../../HUD"), or owner.score += 1 inside a child. Each one silently assumes a particular tree shape.

Tip: Exceptions exist. A child may call methods on its parent when the scene is designed as one unit (a State node calling state_machine.transition_to() in A State Machine in GDScript). Make the dependency explicit with a typed @export var or a typed owner check, so a wrong setup fails loudly.

2. Compose scenes; inherit sparingly

Godot has two ways to reuse a scene:

Composition (instance a scene inside another) Scene inheritance (Scene > New Inherited Scene)
Reuse unit small scenes and components: Health, Hitbox, Interactable a base scene: Enemy.tscn → Slime.tscn, Bat.tscn
Change the base each user picks what it needs every inherited scene changes, sometimes in surprising ways
Typical depth wide and shallow best kept to one level

Prefer composition. Use inheritance for real variants that share almost the whole tree (enemy skins with different stats), and keep it one level deep. A deep chain of inherited scenes is hard to edit, because changes to inherited nodes show up as overrides that are easy to lose.

Scripts follow the same rule: one level of extends on top of an engine class is common (class_name Enemy extends CharacterBody2D, then extends Enemy); five levels is a smell. Put shared behaviour into child nodes instead.

3. Keep autoloads few and boring

Autoloads (Project Settings > Globals > Autoload) are nodes added under the root before your main scene, reachable by name from every script. They are convenient, which is the problem: every autoload is a global variable.

Good autoloads:

  • An event bus with signals only (signal coin_collected(value: int)), for events that cross scene boundaries, such as achievements, analytics or a HUD in another branch of the tree.
  • Services with one job: SaveManager, AudioManager (music that survives scene changes), SceneLoader (fade + change_scene_to_file).
  • Settings loaded once at startup.

Bad autoloads:

  • Global.gd with player, score, current_level, enemies_alive and twenty helper functions. Every scene now depends on every other through it, and state from the last level leaks into the next.
  • Autoloads holding references to nodes in the current scene. After change_scene_to_file() those nodes are freed and the reference is dangling (check with is_instance_valid).
  • Logic that only one scene uses.

Rules of thumb: fewer than about five autoloads in a small game; signals and services, not shared mutable state; and if a value belongs to a level, it lives in the level scene. When you need game-wide state (score across levels), give it one owner with a reset() method that a new game calls.

4. Find things by reference, not by path

  • Use @onready var with %UniqueName (right-click a node > Access as Unique Name) for nodes inside your own scene: @onready var label: Label = %ScoreLabel. The path survives reorganising the scene.
  • Use @export var target: Node2D for references the designer sets in the inspector. Typed exports refuse wrong node types.
  • Use groups for "all enemies" style queries, not paths (see Groups & Tree Queries).
  • Avoid get_tree().root.get_node("Main/Level/Player"): it encodes the whole game's tree.

5. One scene, one job, runnable alone

Every scene should run on its own with F6 (Run Current Scene). An enemy scene that crashes unless a Player autoload exists is coupled too tightly. Give scenes sensible defaults, guard optional dependencies with if target:, and keep a test_*.tscn sandbox scene per mechanic. You will debug far faster in a sandbox than by playing to level 3.

6. Make data, not code, vary

When ten enemies differ only in numbers, write one Enemy scene and ten EnemyStats resources (.tres), not ten scripts. Designers can edit resources in the inspector, and you can add an enemy without touching code. Remember that resources are shared, as shown in Field Notes: Typing, Physics & Memory.

7. Name things the Godot way

Thing Convention Example
Files and folders snake_case player.gd, player.tscn, enemies/slime/
Classes, nodes, class_name PascalCase class_name PlayerController, node HealthBar
Functions, variables, signals snake_case; signals in past tense take_damage(), signal health_changed
Constants and enum values CONSTANT_CASE const MAX_SPEED := 300.0
Private members leading underscore var _velocity_cache
Signal handlers _on_<node>_<signal> _on_hurtbox_area_entered

Keep each scene and its script together in a feature folder (player/player.tscn, player/player.gd) rather than splitting into scenes/ and scripts/ folders; features are easier to move and delete that way. The Unity course has a similar discussion for prefabs.

Test yourself

A coin needs to add to the score when collected. Which design follows 'signals up, calls down'?

  1. The coin calls get_parent().get_parent().score += 1
  2. The coin emits collected(value); the level (or an event bus) connects to it and updates the score
  3. The coin edits a Global.score variable and the HUD polls it every frame
  4. The coin finds the HUD by path and changes its label

Which is a good use of an autoload?

  1. Storing a reference to the current level's player node
  2. A signal-only event bus or a single-purpose service such as a save manager
  3. Every helper function in the project
  4. Level-specific enemy counts

Why prefer composition (small instanced scenes) over deep scene inheritance?

  1. Inheritance is not supported in Godot 4
  2. Small components can be mixed freely, while changes to a deep base scene ripple into every inherited scene
  3. Composition uses less memory in every case
  4. Inherited scenes can't have scripts