Architecture for a 2D action game in Godot 4
godotarchitecturestate-machinecomponentsresourcesevent-busaddons
What a 2D action game in Godot 4 should look like structurally, from a five-agent research pass
(2026-03-27, 50+ sources; full report at ~/.claude/skills/research/2d-action-games-godot-advanced-engineering.md)
and from the technical debt that piled up in "Fight", a side-scrolling shooter that grew to 1,875 lines
of GDScript before any of it was applied.
Typical debt in a prototype that grew
- Monolith scripts: a 452-line player and a 535-line enemy base doing movement, combat, health and UI.
- Implicit state as booleans (
is_dead,is_attacking,invincible) instead of a state machine. - One autoload doubling as config store, event bus, score tracker, combo system and checkpoint manager.
- Weapon stats as dictionaries inside the player script.
- No scene management, audio, save system or fonts; a tileset that was never used (platforms were
ColorRects).
Target: three layers
- Visual layer: nodes and scenes, as stateless as possible.
- Logic layer: state machines and component nodes.
- Data layer:
Resource(.tres) files for all tunable data (WeaponStats, EnemyStats, PowerupConfig).
Patterns to adopt
- Node-based state machine: each state is a node script with
enter / exit / update / physics_update. An enum FSM is fine for a small enemy (IDLE, PATROL, CHASE, ATTACK, RETREAT, HURT, DEAD) but stops scaling. - Concurrent FSMs: separate movement and combat machines so "jumping while shooting" is not a combinatorial state.
- Component nodes: HealthComponent, HitboxComponent, HurtboxComponent, MovementComponent.
- Dedicated event bus autoload (
Events.gd) holding only cross-system signals. - Feature-based folders (
player/,enemies/,weapons/,ui/,world/) rather than by file type. - Input abstraction: the same interface for player input and AI input lets one FSM drive both.
- Scene manager with fade transitions; save/load early.
Anti-patterns the research flagged
Future-proofing, monolith scripts, editor-wired signal connections at scale, and object pooling by default (Godot's allocation is cheap enough; pool only after measuring).
Recommended addons (GitHub stars as of 2026-03)
LimboAI (about 2,600, behavior trees plus FSM in C++ with a visual debugger), Phantom Camera (about 3,200, follow/shake/zoom), GUT (about 2,400, unit testing), Aseprite Wizard (about 1,200), Input Helper (about 1,000, device detection), Sound Manager (about 740, audio pooling), GODOT-VFX-LIBRARY (about 217).
Enemy AI ideas that worked cheaply
Aggro memory (keep chasing 3 s after losing sight), alert propagation to nearby enemies, ranged enemies
that retreat to keep distance, aim prediction, staggered random attack delays, sentry vs patrol
variety, difficulty scaling by level position, knockback resistance per type, a wave system
(3 + 2 * wave enemies, mix weighted by wave).
Sprite sheet import tip
A single strip sheet with variable frame widths (2889 x 72 px, 40 frames) can be sliced with a short PIL
script that finds frame boundaries by empty columns, normalizes frames onto fixed canvases (64 x 48),
and writes SpriteFrames resources per animation.