game-design-patterns
Use this skill when implementing game programming patterns - state machines for character/AI behavior, object pooling for performance-critical spawning, event systems for decoupled game communication, or the command pattern for input handling, undo/redo, and replays. Triggers on game architecture, game loop design, entity management, finite state machines, object pools, observer/event bus, command queues, and gameplay programming patterns.
engineering game-devdesign-patternsstate-machineobject-poolevent-systemcommand-patternWhat is game-design-patterns?
Use this skill when implementing game programming patterns - state machines for character/AI behavior, object pooling for performance-critical spawning, event systems for decoupled game communication, or the command pattern for input handling, undo/redo, and replays. Triggers on game architecture, game loop design, entity management, finite state machines, object pools, observer/event bus, command queues, and gameplay programming patterns.
game-design-patterns
game-design-patterns is a production-ready AI agent skill for claude-code, gemini-cli, openai-codex, and 1 more. Implementing game programming patterns - state machines for character/AI behavior, object pooling for performance-critical spawning, event systems for decoupled game communication, or the command pattern for input handling, undo/redo, and replays.
Quick Facts
| Field | Value |
|---|---|
| Category | engineering |
| Version | 0.1.0 |
| Platforms | claude-code, gemini-cli, openai-codex, mcp |
| License | MIT |
How to Install
- Make sure you have Node.js installed on your machine.
- Run the following command in your terminal:
npx skills add AbsolutelySkilled/AbsolutelySkilled --skill game-design-patterns- The game-design-patterns skill is now available in your AI coding agent (Claude Code, Gemini CLI, OpenAI Codex, etc.).
Overview
Game design patterns solve recurring problems in game development where standard enterprise patterns fall short. Games face unique constraints: real-time frame budgets (16ms at 60fps), thousands of dynamic entities, complex state transitions for AI and player characters, and the need for deterministic replay and undo. This skill covers four foundational patterns - state machines, object pooling, event systems, and the command pattern - that form the backbone of well-architected gameplay code.
Tags
game-dev design-patterns state-machine object-pool event-system command-pattern
Platforms
- claude-code
- gemini-cli
- openai-codex
- mcp
Related Skills
Pair game-design-patterns with these complementary skills:
Frequently Asked Questions
What is game-design-patterns?
Use this skill when implementing game programming patterns - state machines for character/AI behavior, object pooling for performance-critical spawning, event systems for decoupled game communication, or the command pattern for input handling, undo/redo, and replays. Triggers on game architecture, game loop design, entity management, finite state machines, object pools, observer/event bus, command queues, and gameplay programming patterns.
How do I install game-design-patterns?
Run npx skills add AbsolutelySkilled/AbsolutelySkilled --skill game-design-patterns in your terminal. The skill will be immediately available in your AI coding agent.
What AI agents support game-design-patterns?
This skill works with claude-code, gemini-cli, openai-codex, mcp. Install it once and use it across any supported AI coding agent.
Maintainers
Generated from AbsolutelySkilled
SKILL.md
Game Design Patterns
Game design patterns solve recurring problems in game development where standard enterprise patterns fall short. Games face unique constraints: real-time frame budgets (16ms at 60fps), thousands of dynamic entities, complex state transitions for AI and player characters, and the need for deterministic replay and undo. This skill covers four foundational patterns - state machines, object pooling, event systems, and the command pattern - that form the backbone of well-architected gameplay code.
When to use this skill
Trigger this skill when the user:
- Needs to model character states, AI behavior, or game phases with a state machine
- Wants to implement object pooling for bullets, particles, enemies, or other frequently spawned entities
- Asks about event systems, message buses, or observer patterns in a game context
- Needs the command pattern for input handling, undo/redo, or action replays
- Is building a game loop and needs architectural guidance on entity management
- Wants to decouple game systems (audio, UI, physics) from gameplay logic
- Asks about managing game state transitions (menus, gameplay, pause, cutscenes)
Do NOT trigger this skill for:
- Rendering, shaders, or graphics programming (not a design pattern concern)
- General software design patterns unrelated to games (use clean-architecture instead)
Key principles
Frame budget is law - Every pattern choice must respect the ~16ms frame budget. Allocations during gameplay cause GC spikes. Indirection has cache costs. Always profile before adding abstraction.
Decouple, but not infinitely - Game systems should communicate through events and commands rather than direct references, but over-decoupling creates debugging nightmares. One level of indirection is usually enough.
State is explicit - Implicit state (nested boolean flags, mode integers) leads to impossible combinations and subtle bugs. Make every valid state a first-class object with defined transitions.
Pool what you spawn - Any entity created and destroyed more than once per second should be pooled. The cost of allocation is not the constructor - it is the garbage collector pause 3 seconds later.
Commands are data - When input actions are objects rather than direct method calls, you get undo, replay, networking, and AI "for free." The command pattern is the single highest-leverage pattern in gameplay code.
Core concepts
State machines model entities that have distinct behavioral modes. A character can be Idle, Running, Jumping, or Attacking - but never Jumping and Idle at the same time. Each state encapsulates its own update logic, entry/exit behavior, and valid transitions. Hierarchical state machines (HFSM) add nested sub-states for complex AI.
Object pooling pre-allocates a fixed set of objects and recycles them instead of creating and destroying instances at runtime. The pool maintains an "available" list and hands out pre-initialized objects on request, reclaiming them when they are "killed." This eliminates allocation pressure during gameplay.
Event systems (also called observer, pub/sub, or message bus) let game systems
communicate without direct references. When a player takes damage, the health system
fires a DamageTaken event. The UI, audio, camera shake, and analytics systems each
subscribe independently. Adding a new reaction requires zero changes to the damage code.
The command pattern encapsulates an action as an object with execute() and
optionally undo(). Player input becomes a stream of command objects. This enables
input rebinding, replay recording, undo/redo in editors, and sending commands over
the network for multiplayer.
Common tasks
Implement a finite state machine for character behavior
Each state is a class with enter(), update(), exit(), and a transition check.
The machine holds the current state and delegates to it.
interface State {
enter(): void;
update(dt: number): void;
exit(): void;
}
class IdleState implements State {
constructor(private character: Character) {}
enter() { this.character.playAnimation("idle"); }
update(dt: number) {
if (this.character.input.jump) {
this.character.fsm.transition(new JumpState(this.character));
}
}
exit() {}
}
class StateMachine {
private current: State;
transition(next: State) {
this.current.exit();
this.current = next;
this.current.enter();
}
update(dt: number) {
this.current.update(dt);
}
}Avoid string-based state names. Use typed state classes so the compiler catches invalid transitions.
Build an object pool
Pre-allocate objects at startup. acquire() returns a recycled instance; release()
returns it to the pool. Never allocate during gameplay.
class ObjectPool<T> {
private available: T[] = [];
private active: Set<T> = new Set();
constructor(
private factory: () => T,
private reset: (obj: T) => void,
initialSize: number
) {
for (let i = 0; i < initialSize; i++) {
this.available.push(this.factory());
}
}
acquire(): T | null {
if (this.available.length === 0) return null;
const obj = this.available.pop()!;
this.active.add(obj);
return obj;
}
release(obj: T): void {
if (!this.active.has(obj)) return;
this.active.delete(obj);
this.reset(obj);
this.available.push(obj);
}
}
// Usage: bullet pool
const bulletPool = new ObjectPool(
() => new Bullet(),
(b) => { b.active = false; b.position.set(0, 0); },
200
);Size the pool to your worst-case burst. If
acquire()returns null, either grow the pool (with a warning log) or skip the spawn - never allocate inline.
Set up a typed event system
Use a type-safe event bus so subscribers know exactly what payload to expect.
type EventMap = {
"damage-taken": { target: Entity; amount: number; source: Entity };
"enemy-killed": { enemy: Entity; killer: Entity; score: number };
"level-complete": { level: number; time: number };
};
class EventBus {
private listeners = new Map<string, Set<Function>>();
on<K extends keyof EventMap>(event: K, handler: (data: EventMap[K]) => void) {
if (!this.listeners.has(event)) this.listeners.set(event, new Set());
this.listeners.get(event)!.add(handler);
return () => this.listeners.get(event)!.delete(handler); // unsubscribe
}
emit<K extends keyof EventMap>(event: K, data: EventMap[K]) {
this.listeners.get(event)?.forEach(fn => fn(data));
}
}
// Usage
const bus = new EventBus();
const unsub = bus.on("damage-taken", ({ target, amount }) => {
healthBar.update(target.id, amount);
});Always return an unsubscribe function. Leaked subscriptions from destroyed entities are the #1 event system bug in games.
Implement the command pattern for input with undo
Each player action is a command object. Store a history stack for undo.
interface Command {
execute(): void;
undo(): void;
}
class MoveCommand implements Command {
private previousPosition: Vector2;
constructor(private entity: Entity, private direction: Vector2) {}
execute() {
this.previousPosition = this.entity.position.clone();
this.entity.position.add(this.direction);
}
undo() {
this.entity.position.copy(this.previousPosition);
}
}
class CommandHistory {
private history: Command[] = [];
private pointer = -1;
execute(cmd: Command) {
// Discard any redo history
this.history.length = this.pointer + 1;
cmd.execute();
this.history.push(cmd);
this.pointer++;
}
undo() {
if (this.pointer < 0) return;
this.history[this.pointer].undo();
this.pointer--;
}
redo() {
if (this.pointer >= this.history.length - 1) return;
this.pointer++;
this.history[this.pointer].execute();
}
}For replay systems, serialize commands with timestamps. Replay = feed the same command stream to a fresh game state.
Use a hierarchical state machine for complex AI
When a single FSM has too many states, use sub-states. A "Combat" state can contain "Attacking", "Flanking", and "Retreating" sub-states.
class HierarchicalState implements State {
protected subMachine: StateMachine;
enter() { this.subMachine.transition(this.getInitialSubState()); }
update(dt: number) { this.subMachine.update(dt); }
exit() { this.subMachine.currentState?.exit(); }
protected getInitialSubState(): State {
throw new Error("Override in subclass");
}
}
class CombatState extends HierarchicalState {
constructor(private ai: AIController) {
super();
this.subMachine = new StateMachine();
}
protected getInitialSubState(): State {
return new AttackingSubState(this.ai);
}
}Limit nesting to 2 levels. Three or more levels of hierarchy signals you need a behavior tree instead.
Implement command pattern for multiplayer input
Send commands over the network instead of state. Both clients execute the same command stream deterministically.
interface NetworkCommand extends Command {
serialize(): ArrayBuffer;
readonly playerId: string;
readonly frame: number;
}
class NetworkCommandBuffer {
private buffer: Map<number, NetworkCommand[]> = new Map();
addCommand(frame: number, cmd: NetworkCommand) {
if (!this.buffer.has(frame)) this.buffer.set(frame, []);
this.buffer.get(frame)!.push(cmd);
}
getCommandsForFrame(frame: number): NetworkCommand[] {
return this.buffer.get(frame) ?? [];
}
}Deterministic lockstep requires all clients to process the exact same commands in the exact same frame order. Floating-point differences across platforms will cause desync - use fixed-point math for critical state.
Anti-patterns / common mistakes
| Mistake | Why it's wrong | What to do instead |
|---|---|---|
| Boolean state flags | isJumping && !isAttacking && isDashing creates impossible-to-debug combinations |
Use an explicit state machine with typed states |
| Allocating in the hot loop | new Bullet() every frame causes GC pauses and frame drops |
Pool all frequently spawned objects |
| God event bus | Every system subscribes to everything on one global bus | Scope buses per domain (combat bus, UI bus) or use direct listeners for tight couplings |
| Commands without undo | Implementing execute() but skipping undo() for "simplicity" |
Always implement undo() even if unused now - replay and debugging need it |
| Stringly-typed events | Using raw strings like "dmg" instead of typed event names |
Use a typed EventMap (TypeScript) or enum-based keys so typos are compile errors |
| Unbounded command history | Storing every command forever leaks memory in long sessions | Cap history length or checkpoint + truncate periodically |
| Spaghetti transitions | Every state can transition to every other state | Define a transition table upfront. If a transition is not in the table, it is illegal |
Gotchas
Object pools sized for average load, not burst load, cause missed spawns - If you size a bullet pool for "average 50 bullets" but the boss fight fires 200 in 2 seconds,
acquire()returns null and bullets silently fail to spawn. Always size pools to the worst-case burst in your game, add pool expansion with a warning log, and test the burst scenario explicitly.State machine transitions that allocate new State objects cause GC pressure - If each
transition()call doesnew JumpState(character), you're allocating during gameplay, which triggers garbage collection pauses. Pre-allocate all state instances at startup and store them in a dictionary; transition by swapping references, not by creating new objects.Event bus subscriptions from destroyed entities cause null reference crashes - When a game object is destroyed without unsubscribing its event handlers, the next event dispatch calls a handler with a null
thiscontext and crashes or produces stale state. Always store and invoke the unsubscribe function returned byon()in the entity's destroy/cleanup path.Command history grows unbounded in long sessions - Storing every command since session start for an undo system will consume growing memory over hours of gameplay. Cap the command history to a maximum depth (e.g., 100 commands) or checkpoint-and-truncate periodically. For replay systems, commands older than the checkpoint can be dropped.
Deterministic lockstep breaks silently on floating-point operations - Two clients running the same command stream will desync if any physics or movement calculation uses floating-point math, because IEEE 754 results can differ across CPU architectures and compiler optimizations. Use fixed-point arithmetic for all game state that must be deterministic across clients.
References
For detailed content on specific patterns, read the relevant file from references/:
references/state-machines.md- Hierarchical FSMs, pushdown automata, behavior tree comparison, and transition table designreferences/object-pooling.md- Pool sizing strategies, warm-up patterns, thread safety, and language-specific GC considerationsreferences/event-systems.md- Event queue vs immediate dispatch, priority ordering, event filtering, and debugging leaked subscriptionsreferences/command-pattern.md- Serialization for replay/networking, macro recording, composite commands, and undo stack management
Only load a references file if the current task requires deep detail on that topic.
References
command-pattern.md
Command Pattern
The command pattern encapsulates actions as objects. Instead of calling
player.jump() directly, you create a JumpCommand object and execute it. This
single layer of indirection unlocks undo, redo, replay, networking, AI input,
input rebinding, and macro recording.
Core interface
Every command implements at minimum:
interface Command {
execute(): void;
undo(): void;
}For networking and replay, add serialization:
interface SerializableCommand extends Command {
readonly type: string;
serialize(): Record<string, unknown>;
static deserialize(data: Record<string, unknown>): SerializableCommand;
}Undo stack management
A command history tracks executed commands and supports undo/redo.
class CommandHistory {
private stack: Command[] = [];
private pointer = -1;
private maxSize: number;
constructor(maxSize = 100) {
this.maxSize = maxSize;
}
execute(cmd: Command) {
// Discard redo history when a new command is executed
this.stack.length = this.pointer + 1;
cmd.execute();
this.stack.push(cmd);
this.pointer++;
// Enforce max size to prevent memory leaks
if (this.stack.length > this.maxSize) {
this.stack.shift();
this.pointer--;
}
}
undo(): boolean {
if (this.pointer < 0) return false;
this.stack[this.pointer].undo();
this.pointer--;
return true;
}
redo(): boolean {
if (this.pointer >= this.stack.length - 1) return false;
this.pointer++;
this.stack[this.pointer].execute();
return true;
}
clear() {
this.stack.length = 0;
this.pointer = -1;
}
}Key decisions:
- Max size - Cap at 50-200 commands for gameplay, 1000+ for editors.
- Redo discard - When executing a new command after undoing, discard the redo branch. This is the standard UX expectation.
- Checkpoint - For long sessions, periodically snapshot the full state and truncate old commands. Undo only goes back to the last checkpoint.
Composite commands
Group multiple commands into one undoable unit. Essential for batch operations in editors (e.g., "move all selected objects").
class CompositeCommand implements Command {
constructor(private commands: Command[]) {}
execute() {
for (const cmd of this.commands) {
cmd.execute();
}
}
undo() {
// Undo in reverse order
for (let i = this.commands.length - 1; i >= 0; i--) {
this.commands[i].undo();
}
}
}
// Usage: move 5 selected tiles at once
const moveAll = new CompositeCommand(
selectedTiles.map(tile => new MoveCommand(tile, offset))
);
history.execute(moveAll); // Undo reverts ALL tiles in one stepReplay system
Record commands with timestamps during gameplay. Replay by feeding the same commands to a fresh game state at the same timings.
interface TimestampedCommand {
frame: number;
command: SerializableCommand;
}
class ReplayRecorder {
private recording: TimestampedCommand[] = [];
private currentFrame = 0;
record(cmd: SerializableCommand) {
this.recording.push({ frame: this.currentFrame, command: cmd });
}
tick() {
this.currentFrame++;
}
export(): string {
return JSON.stringify(
this.recording.map(r => ({
frame: r.frame,
type: r.command.type,
data: r.command.serialize(),
}))
);
}
}
class ReplayPlayer {
private recording: TimestampedCommand[];
private index = 0;
private currentFrame = 0;
constructor(data: string, private commandFactory: CommandFactory) {
const parsed = JSON.parse(data);
this.recording = parsed.map((r: any) => ({
frame: r.frame,
command: this.commandFactory.create(r.type, r.data),
}));
}
tick() {
while (
this.index < this.recording.length &&
this.recording[this.index].frame === this.currentFrame
) {
this.recording[this.index].command.execute();
this.index++;
}
this.currentFrame++;
}
get isComplete(): boolean {
return this.index >= this.recording.length;
}
}Critical requirement for replay: The game must be deterministic. Same commands on same frame must produce same result. This means:
- No
Math.random()- use a seeded PRNG and include the seed in the replay - No
Date.now()for gameplay logic - use frame count - Fixed timestep physics (not variable dt)
- Consistent floating-point behavior across platforms (use fixed-point if needed)
Networking with commands
In multiplayer, send commands over the network instead of state. This reduces bandwidth and leverages the same determinism required for replay.
Lockstep model
Both clients wait for the other's commands before advancing a frame.
Frame N:
1. Collect local input -> create commands
2. Send commands to server/peer
3. Wait for remote commands
4. Execute ALL commands (local + remote) in deterministic order
5. Advance to frame N+1Rollback model (GGPO-style)
Predict the remote player's input. If the prediction was wrong, roll back to the last confirmed frame and replay with correct commands.
class RollbackManager {
private confirmedFrame = 0;
private stateSnapshots = new Map<number, GameState>();
private commandHistory = new Map<number, Command[]>();
predict(frame: number, localCmd: Command, predictedRemoteCmd: Command) {
this.commandHistory.set(frame, [localCmd, predictedRemoteCmd]);
this.stateSnapshots.set(frame, this.gameState.snapshot());
localCmd.execute();
predictedRemoteCmd.execute();
}
correctPrediction(frame: number, actualRemoteCmd: Command) {
const predicted = this.commandHistory.get(frame)?.[1];
if (predicted && this.commandsEqual(predicted, actualRemoteCmd)) {
this.confirmedFrame = frame;
return; // Prediction was correct
}
// Rollback to the frame before the misprediction
this.gameState.restore(this.stateSnapshots.get(frame)!);
// Replay from that frame with correct commands
this.commandHistory.get(frame)![1] = actualRemoteCmd;
// ... re-execute all frames from here to current
}
}Rollback requires the ability to snapshot and restore game state quickly. Keep gameplay state compact and separate from visual state.
Input rebinding
When input is mapped through commands, rebinding is just changing which key maps to which command - no gameplay code changes.
class InputMapper {
private bindings = new Map<string, () => Command>();
bind(key: string, commandFactory: () => Command) {
this.bindings.set(key, commandFactory);
}
handleInput(key: string): Command | null {
const factory = this.bindings.get(key);
return factory ? factory() : null;
}
}
// Default bindings
const mapper = new InputMapper();
mapper.bind("Space", () => new JumpCommand(player));
mapper.bind("KeyZ", () => new AttackCommand(player));
// Player rebinds attack to KeyX
mapper.bind("KeyX", () => new AttackCommand(player));
mapper.bind("KeyZ", () => new DashCommand(player)); // Z is now dashMacro recording
Record a sequence of commands as a single reusable macro. Useful for strategy games, automation tools, and level editors.
class MacroRecorder {
private isRecording = false;
private commands: Command[] = [];
startRecording() {
this.commands = [];
this.isRecording = true;
}
record(cmd: Command) {
if (this.isRecording) this.commands.push(cmd);
}
stopRecording(): CompositeCommand {
this.isRecording = false;
return new CompositeCommand([...this.commands]);
}
}Common pitfalls
- Skipping undo() - "We'll add it later." You won't, and then replay breaks. Implement undo() for every command from day one.
- Mutable command state - A command that references mutable objects may produce different results on redo. Capture values at execute time, not construction time.
- Non-deterministic commands - Using
Math.random()or system time inside execute(). All randomness must come from a seeded PRNG passed to the command. - Unbounded history - Storing every command forever. Set a max size and/or use periodic checkpoints.
- Forgetting composite undo order - Composite commands must undo in reverse order. Forward undo creates inconsistent state.
event-systems.md
Event Systems
Event systems decouple game systems by letting them communicate through messages rather than direct references. The damage system does not need to know about the UI, audio, analytics, or camera shake - it just emits "damage dealt" and subscribers handle the rest.
Immediate dispatch vs event queue
Immediate dispatch
Handlers fire synchronously the moment emit() is called. Simple and predictable.
class ImmediateEventBus {
private handlers = new Map<string, Set<Function>>();
emit(event: string, data: any) {
this.handlers.get(event)?.forEach(fn => fn(data));
}
}Pros: Simple. Handlers see the event in the same frame. Stack trace is readable. Cons: A slow handler blocks the emitter. Handlers can emit more events, causing re-entrant cascades.
Event queue
Events are buffered and processed at a defined point in the frame (e.g., end of update, between fixed steps).
class QueuedEventBus {
private queue: Array<{ event: string; data: any }> = [];
private handlers = new Map<string, Set<Function>>();
emit(event: string, data: any) {
this.queue.push({ event, data });
}
processQueue() {
const current = this.queue;
this.queue = []; // Swap to avoid infinite loops from handlers emitting
for (const { event, data } of current) {
this.handlers.get(event)?.forEach(fn => fn(data));
}
}
}Pros: No re-entrancy issues. Can batch-process events. Easier to debug. Cons: One frame of latency. Harder to reason about ordering.
Recommendation: Use immediate dispatch for UI and simple gameplay. Use queued dispatch for physics callbacks, networking, and any system where re-entrancy is a risk.
Priority ordering
When multiple systems react to the same event, order can matter. A shield system should reduce damage before the health system applies it.
class PriorityEventBus {
private handlers = new Map<string, Array<{ priority: number; fn: Function }>>();
on(event: string, handler: Function, priority = 0) {
if (!this.handlers.has(event)) this.handlers.set(event, []);
const list = this.handlers.get(event)!;
list.push({ priority, fn: handler });
list.sort((a, b) => b.priority - a.priority); // Higher priority first
}
emit(event: string, data: any) {
this.handlers.get(event)?.forEach(({ fn }) => fn(data));
}
}
// Shield processes damage first (priority 100), then health (priority 0)
bus.on("damage-taken", shieldSystem.onDamage, 100);
bus.on("damage-taken", healthSystem.onDamage, 0);Use priority sparingly. If you have more than 3 priority levels for one event, the systems are too coupled and you should redesign the data flow.
Event filtering
Not every subscriber cares about every instance of an event. The UI only cares about damage to the player, not to every enemy.
Filter at subscribe time
bus.on("damage-taken", (data) => {
if (data.target !== player) return; // Early exit
healthBar.update(data.amount);
});Scoped event buses
Instead of one global bus, create buses per scope:
class Entity {
readonly events = new EventBus(); // Per-entity bus
}
// Subscribe to this specific enemy's events
boss.events.on("health-changed", updateBossHealthBar);Best practice: Use a global bus for system-level events (level-complete, pause, game-over). Use per-entity buses for entity-specific events (health-changed, state-changed).
Debugging leaked subscriptions
The #1 bug with event systems: a destroyed entity's handler is still subscribed, causing it to process events after "death."
Prevention: unsubscribe on destroy
class Enemy {
private unsubscribers: Array<() => void> = [];
init() {
this.unsubscribers.push(
bus.on("player-moved", this.onPlayerMoved.bind(this))
);
}
destroy() {
this.unsubscribers.forEach(unsub => unsub());
this.unsubscribers.length = 0;
}
}Detection: debug mode listener tracking
class DebugEventBus extends EventBus {
private subscriberSources = new Map<Function, string>();
on(event: string, handler: Function) {
this.subscriberSources.set(handler, new Error().stack ?? "unknown");
return super.on(event, handler);
}
debugListeners(event: string) {
console.log(`Listeners for "${event}":`);
this.handlers.get(event)?.forEach(fn => {
console.log(" -", this.subscriberSources.get(fn));
});
}
}In debug builds, log a warning when event handler count for any event exceeds a threshold (e.g., 50). This catches subscription leaks early.
Typed events pattern
Use TypeScript's type system to enforce correct event payloads at compile time.
// Define all events and their payloads in one place
interface GameEvents {
"player:damage": { amount: number; source: string; isCritical: boolean };
"player:death": { killer: string; position: Vector2 };
"enemy:spawn": { type: string; level: number; position: Vector2 };
"ui:notification": { message: string; duration: number };
"game:pause": {};
"game:resume": {};
}
class TypedEventBus {
private handlers = new Map<string, Set<Function>>();
on<K extends keyof GameEvents>(
event: K,
handler: (data: GameEvents[K]) => void
): () => void {
if (!this.handlers.has(event)) this.handlers.set(event, new Set());
this.handlers.get(event)!.add(handler);
return () => this.handlers.get(event)!.delete(handler);
}
emit<K extends keyof GameEvents>(event: K, data: GameEvents[K]) {
this.handlers.get(event)?.forEach(fn => fn(data));
}
}This catches typos in event names and wrong payload shapes at compile time.
Common pitfalls
- God bus - One global bus handles everything. Hard to trace which systems react to what. Split into domain-specific buses.
- Event storms - Handler A emits event B, handler for B emits event A, creating an infinite loop. Use queued dispatch or add re-entrancy guards.
- Order dependence - System correctness depends on handler execution order without making that order explicit. Use priority if order matters.
- Stale data in events - Event carries a reference to mutable data that changes before all handlers process it. Copy data into the event payload.
- Subscription leaks - Destroyed entities still subscribed. Always unsubscribe in the destroy/cleanup method.
object-pooling.md
Object Pooling
Object pooling eliminates runtime allocation by pre-creating objects and recycling them. In games, this is not an optimization - it is a requirement for any entity spawned more than once per second.
Why pooling matters in games
The problem is not allocation - it is deallocation. Creating an object is fast. But when the garbage collector eventually runs to clean up destroyed objects, it pauses your game for milliseconds. At 60fps, a 5ms GC pause means a visible stutter.
When to pool:
- Bullets, projectiles, particles (high spawn rate)
- Enemies that spawn in waves
- UI elements like damage numbers, floating text
- Audio source objects
- Visual effects (explosions, hit sparks)
When NOT to pool:
- Singleton systems (one instance, never destroyed)
- Level geometry (loaded once, persists all level)
- Anything created fewer than once per 10 seconds
Pool sizing strategies
Fixed pool
Pre-allocate an exact count. If the pool is empty, refuse the spawn.
const MAX_BULLETS = 200;
const bulletPool = new ObjectPool(() => new Bullet(), MAX_BULLETS);
function fireBullet(pos: Vector2, dir: Vector2) {
const bullet = bulletPool.acquire();
if (!bullet) return; // Pool exhausted - skip this bullet
bullet.init(pos, dir);
}Pros: Predictable memory. No allocations ever. Cons: Hard to tune. Too small = missed spawns. Too large = wasted memory.
Growing pool with warning
Start with an estimate. If exhausted, grow by allocating more - but log a warning so you can tune the initial size.
class GrowablePool<T> {
private available: T[] = [];
private growCount = 0;
constructor(
private factory: () => T,
private reset: (obj: T) => void,
initialSize: number,
private growSize: number = 10
) {
this.fill(initialSize);
}
private fill(count: number) {
for (let i = 0; i < count; i++) {
this.available.push(this.factory());
}
}
acquire(): T {
if (this.available.length === 0) {
this.growCount++;
console.warn(`Pool grew ${this.growCount} times. Consider increasing initial size.`);
this.fill(this.growSize);
}
return this.available.pop()!;
}
release(obj: T) {
this.reset(obj);
this.available.push(obj);
}
}Best practice: Run your heaviest gameplay scenario and check growCount. Set initial size to cover that peak with 20% headroom.
Double-buffer pool
For systems where objects are spawned and released in bulk (particle bursts), use two lists and swap them each frame to avoid iterator invalidation.
Reset contract
The most critical part of pooling is the reset function. When an object is released back to the pool, it MUST be returned to a pristine state. Stale data from a previous life is the #1 pooling bug.
function resetBullet(bullet: Bullet) {
bullet.active = false;
bullet.position.set(0, 0);
bullet.velocity.set(0, 0);
bullet.damage = 0;
bullet.owner = null;
bullet.lifetime = 0;
// Clear any event listeners attached during this life
bullet.removeAllListeners();
}Checklist for a reset function:
- Zero all physics state (position, velocity, acceleration)
- Reset all gameplay state (health, damage, owner, team)
- Deactivate rendering (hide sprite, disable mesh)
- Remove event subscriptions added during this object's life
- Cancel any active timers or coroutines
- Reset animation to default
Language-specific GC considerations
JavaScript / TypeScript
- No manual memory management. GC is unavoidable for unreferenced objects.
- Pool ALL frequently spawned objects. Avoid
newin update loops. - Use
TypedArray(Float32Array, etc.) for bulk numeric data instead of object arrays. - Avoid closures in hot paths - they allocate.
C# (Unity)
- Use
ObjectPool<T>from Unity 2021+ (UnityEngine.Pool). - For older Unity: implement your own with
Queue<T>. - Disable GameObjects with
SetActive(false)instead ofDestroy(). - Use
structfor small value types to avoid heap allocation.
C++
- Pooling is still valuable for cache locality even without GC.
- Use contiguous arrays (
std::vector<T>) for cache-friendly iteration. - Consider slot maps (sparse set) for stable handles with dense storage.
- Object pools in C++ also prevent memory fragmentation.
Rust
- No GC, but pooling still helps with allocation cost and cache performance.
- Use
Vec<T>as a free list.pop()to acquire,push()to release. - Consider
Arenaallocators for frame-scoped temporary objects.
Thread safety
If your game uses multithreaded job systems (Unity DOTS, Bevy ECS), pools need synchronization:
class ThreadSafePool<T> {
private available: T[] = [];
private lock = new Mutex();
acquire(): T | null {
this.lock.acquire();
try {
return this.available.pop() ?? null;
} finally {
this.lock.release();
}
}
release(obj: T) {
this.lock.acquire();
try {
this.available.push(obj);
} finally {
this.lock.release();
}
}
}Better approach: Use per-thread pools. Each thread has its own pool, eliminating contention entirely. Only rebalance between threads periodically.
Common pitfalls
- Forgetting to reset - Object retains state from previous use. Add assertions in debug builds that verify the reset contract.
- Double release - Releasing an object that is already in the pool. Guard with
an
activeflag or a set of active references. - Pool too small - Causes allocation during gameplay. Profile your peak and add headroom.
- Pool too large - Wastes memory on objects never used. Log high-water marks and trim during loading screens.
- Holding references to released objects - Other systems still point at a recycled object. Use handle/generation systems to detect stale references.
state-machines.md
State Machines
State machines are the most fundamental pattern for managing entity behavior in games. Every character, AI agent, UI screen, and game phase can be modeled as a state machine.
Finite State Machine (FSM)
The simplest form. An entity is always in exactly one state. Each state defines:
- enter() - Run once when entering the state (play animation, set flags)
- update(dt) - Run every frame while in the state
- exit() - Run once when leaving (cleanup, stop effects)
- transitions - Conditions that trigger a move to another state
Transition table design
Define valid transitions upfront rather than scattering them through state code. This makes the machine auditable and prevents spaghetti.
type TransitionTable = {
[fromState: string]: {
[condition: string]: string; // target state
};
};
const playerTransitions: TransitionTable = {
Idle: { jump: "Jumping", move: "Running", attack: "Attacking" },
Running: { stop: "Idle", jump: "Jumping", attack: "Attacking" },
Jumping: { land: "Idle", attack: "AirAttack" },
Attacking: { done: "Idle" },
AirAttack: { land: "Idle" },
};If a transition is not in the table, it is illegal. This catches bugs like "attacking while already attacking" at the architecture level.
Common FSM implementation patterns
Enum-based FSM - Simplest. Good for fewer than 5 states with minimal per-state logic.
enum PlayerState { Idle, Running, Jumping }
class Player {
state = PlayerState.Idle;
update(dt: number) {
switch (this.state) {
case PlayerState.Idle:
if (this.input.move) this.state = PlayerState.Running;
break;
case PlayerState.Running:
this.position.x += this.speed * dt;
if (!this.input.move) this.state = PlayerState.Idle;
break;
}
}
}Class-based FSM - Better for 5+ states or states with significant per-state logic. Each state is its own class implementing a common interface. This is the recommended approach for most game entities.
Data-driven FSM - States and transitions defined in external data (JSON, ScriptableObjects). Good for designer-facing tools where non-programmers need to tweak behavior.
Hierarchical State Machine (HFSM)
When a flat FSM has too many states, group related states into parent states. The parent handles shared behavior; children handle specifics.
CombatState (parent)
- AttackingState (child)
- DefendingState (child)
- FlankingState (child)
IdleState (parent)
- PatrolState (child)
- StandingState (child)Key rule: Transitions can target a parent state (enters its default child) or a specific child. A child can transition to a sibling without leaving the parent. Transitioning out of a child to a non-sibling exits the parent too.
When to use HFSM vs flat FSM
| Situation | Use |
|---|---|
| Fewer than 8 states, minimal shared behavior | Flat FSM |
| 8-20 states with clear groupings | HFSM (2 levels max) |
| States share entry/exit logic | HFSM - put shared logic in parent |
| Designer needs to visualize/edit states | HFSM with visual editor |
| 20+ states with complex transitions | Consider behavior trees instead |
Pushdown Automata
A stack-based state machine where new states push onto a stack and popping resumes the previous state. Perfect for interrupt-and-resume patterns.
Use cases:
- Pause menu pushes onto gameplay state; unpausing pops back
- Cutscene pushes onto exploration; cutscene ends, exploration resumes exactly
- Stun effect pushes onto any current state; stun ends, previous behavior resumes
class PushdownFSM {
private stack: State[] = [];
get current(): State | undefined {
return this.stack[this.stack.length - 1];
}
push(state: State) {
this.current?.pause?.();
this.stack.push(state);
state.enter();
}
pop() {
const old = this.stack.pop();
old?.exit();
this.current?.resume?.();
}
update(dt: number) {
this.current?.update(dt);
}
}The
pause()andresume()hooks are critical. Without them, a paused state's timers and animations continue running in the background.
FSM vs Behavior Trees
| Aspect | FSM | Behavior Tree |
|---|---|---|
| Complexity sweet spot | Simple, predictable behavior | Complex AI with priorities and fallbacks |
| Transitions | Explicit (state A -> state B) | Implicit (tree traversal determines next action) |
| Adding behavior | Requires new states and transitions | Add a new branch to the tree |
| Debugging | Easy - print current state | Harder - need tree visualization |
| Player characters | Excellent | Overkill for most cases |
| NPC AI | Good for simple enemies | Better for complex enemies with many behaviors |
| Game phases | Excellent | Not appropriate |
Rule of thumb: Start with an FSM. If you find yourself adding transitions between states that shouldn't know about each other, it is time to consider a behavior tree.
Common pitfalls
- State explosion - Too many states in a flat FSM. Solution: use HFSM or behavior trees.
- Transition spaghetti - Every state can go to every other state. Solution: define a transition table and enforce it.
- Duplicated exit logic - Multiple states need the same cleanup. Solution: use a parent state in an HFSM.
- Missing enter/exit hooks - Putting initialization in
update()behind afirstFrameflag instead of usingenter(). Always use the hooks. - Stateless state machine - Storing state data on the entity instead of in the state object. This leads to stale data when re-entering a state.
Frequently Asked Questions
What is game-design-patterns?
Use this skill when implementing game programming patterns - state machines for character/AI behavior, object pooling for performance-critical spawning, event systems for decoupled game communication, or the command pattern for input handling, undo/redo, and replays. Triggers on game architecture, game loop design, entity management, finite state machines, object pools, observer/event bus, command queues, and gameplay programming patterns.
How do I install game-design-patterns?
Run npx skills add AbsolutelySkilled/AbsolutelySkilled --skill game-design-patterns in your terminal. The skill will be immediately available in your AI coding agent.
What AI agents support game-design-patterns?
game-design-patterns works with claude-code, gemini-cli, openai-codex, mcp. Install it once and use it across any supported AI coding agent.