GD

Scene Composition Pattern

Godot scene tree composition, signal-based decoupling, and autoload singleton patterns

Details

Language / Topic
gdscriptGDScript
Category
Architecture

Rules

balanced
- Favor composition over inheritance in Godot — build complex objects by nesting reusable scenes rather than deep `extends` hierarchies.
- Use signals for parent-to-sibling and child-to-parent communication — never call methods directly on parent nodes from child scripts.
- Keep autoloads (`Project Settings > Autoload`) minimal: use them only for truly global services (event bus, save manager, settings) — not for game-specific logic.
- Design scenes as self-contained units: a `Player.tscn` should work when instanced in any parent scene without external configuration requirements.
- Use an event bus autoload (`EventBus.gd`) with typed signals for decoupled cross-scene communication — emit events, never hold direct node references to distant scene tree nodes.
- Apply the State Pattern for complex entity behavior: a `StateMachine` node with `State` child nodes that each implement `enter()`, `exit()`, and `update()` methods.
- Use `Resource` subclasses for data objects (item stats, level configs) — they are serializable, inspectable, and shareable across scenes without code dependencies.