skip to content

A town guard's state machine has grown to twenty states with tangled transitions; what causes this state explosion, and how would you restructure it?

level: seniorimportance: should knowfreq 56%

answer

  1. concerns multiplied, not added
  2. transitions grow with states squared
  3. split into concurrent machines
  4. parameters belong in data

basics

~20 s

State explosion comes from multiplying independent concerns, such as alertness, weapon and activity, into one flat machine whose transitions grow with the square of its states. Restructure: concurrent machines per concern, hierarchy, a stack for interruptions, data for variants.

solid answer

~50 s

A flat machine needs one state per **combination** of everything that matters: 3 alert levels times 2 weapon states times 4 activities is 24 combinations, and a town guard's machine at twenty states is usually that product, partly filled in. Transitions grow faster: twenty states allow up to 20 x 19 = 380 directed edges. To restructure: **split independent concerns into concurrent machines** (orthogonal regions), so the same town guard needs 3 + 2 + 4 = 9 states that read each other through shared data; use **hierarchy** where many states share exits such as death; use a **pushdown stack** for interruptions such as answering the player; and move **variants into data**, one `Patrol` reading a route instead of a state per route. Keep the machines table-driven so tools can draw them and log each transition; only if the behaviour still resists does another decision model earn a look.

go deeper

for a junior

Know that a flat machine needs one state per combination of everything that matters, which is why state counts grow fast.

for a middle

Count it: concerns multiply into states, and states allow transitions up to n times n minus one; then name concurrent machines, hierarchy, stacks and data parameters as remedies.

for a senior

Diagnose a real tangled machine by driver, restructure it in steps, and handle the new costs: update order between machines, forbidden combinations and logs across machines.

for a principal

Treat this as a structural fix inside the FSM model; choosing a different decision architecture for the studio is a separate decision made with planners and behavior trees in view.

## What state explosion looks like A town guard starts with `Patrol`, `Investigate` and `Fight`. Then design asks for night patrols with a torch, a sheathed weapon in town and a drawn one when alarmed, a suspicious phase before full alert, fleeing at low health, chatting with townsfolk. Each request adds states such as `SuspiciousPatrolWeaponDrawn`, and each new state needs transitions to and from many of the others. At twenty states the diagram no longer fits on a screen, and designers fear changing it. That is **state explosion**. ## Where it comes from **Concerns multiply.** A flat machine represents the NPC's whole situation as one state. If the town guard's situation is the combination of independent concerns: - alertness: `Calm`, `Suspicious`, `Alarmed` (3 values); - weapon: `Sheathed`, `Drawn` (2 values); - activity: `Posted`, `Patrolling`, `Investigating`, `Fleeing` (4 values); then a complete flat machine needs 3 x 2 x 4 = 24 states. Teams rarely author all of them, so they reach twenty and leave the rest as gaps that become bugs. **Transitions grow with the square.** With `n` states and any state able to reach any other, there are up to `n x (n - 1)` directed transitions. Twenty states allow 380. Even a sparse fraction of that is more than anyone can review. **Other drivers:** - The same exit, such as death or a stun, copied onto every state. - Variants encoded as states: `PatrolRouteA`, `PatrolRouteB`, `PatrolRouteC` that differ only in waypoints. - Interruptions encoded as return states: `TalkingThenBackToPatrol`, `TalkingThenBackToPost`. ## Restructuring, step by step 1. **Split independent concerns into concurrent machines.** Give the town guard three small machines, one per concern, all ticking together. The flat 24-combination space becomes 3 + 2 + 4 = 9 states. Where concerns interact, a transition guard in one machine reads another machine: the weapon machine's `Sheathed -> Drawn` fires when the alertness machine is `Alarmed`. 2. **Group shared exits under superstates.** Within each machine, use a hierarchy so death or stun is declared once on a parent. 3. **Put interruptions on a stack.** Talking to the player becomes a pushed state; popping it resumes whatever activity was underneath, with no return-state variants. 4. **Move variants into data.** One `Patrol` state reads a route asset; one `Investigate` state reads a point of interest. States describe behaviour; parameters describe instances. 5. **Make transitions data and draw them.** A table per machine with explicit priorities lets tools render the graph, flag unreachable states and log every firing. | Driver | Restructuring | Effect for the guard | |---|---|---| | Independent concerns multiplied | Concurrent machines | 24 combinations become 9 states | | Same exit on every state | Superstate transitions | Death declared once | | Return-to-X variants | Pushdown stack | One `Talking` state | | Per-instance variants | Parameters in data | One `Patrol` state | ## Costs of the restructured version Concurrent machines are not free: - **Coordination.** Concerns that depend on each other need explicit reads or messages between machines, and the update order between machines must be fixed, or one machine acts on the other's previous-tick state. - **Illegal combinations.** A flat machine simply lacks a `CalmFleeing` state; three machines can produce that combination unless a transition guard forbids it. List the forbidden combinations and assert on them. - **Debugging spans machines.** Logs must show all machines' states per tick, not one. ## When an FSM is the wrong model If, after restructuring, most of the remaining work is about **sequencing many small tasks with fallbacks** or **choosing among many options by weighing them**, the problem has outgrown state machines. Behavior trees, planners and utility scoring each address one of those shapes; choosing between them is a separate question. Restructuring first is still worth it: a clean set of small machines is often enough, and it shows exactly which part of the behaviour is resisting. ## Signs to watch before it explodes - A new feature request that needs a new state in more than one place. - State names that combine two or more concerns. - Copy-pasted transitions differing only in their source. - Designers asking a programmer to change a transition because the graph is unreadable.

  • How do concurrent machines for one NPC avoid acting on each other's stale state?
    Fix the update order per tick, for example alertness, then weapon, then activity, so later machines read the current tick's values from earlier ones. Where two machines depend on each other both ways, read a snapshot taken at the start of the tick instead, and accept one tick of delay in one direction.
  • How do you stop concurrent machines from producing a combination that should not exist?
    Write the forbidden combinations down, such as calm while fleeing, and enforce them where they arise: a transition guard on the activity machine that blocks `Fleeing` unless alertness is `Alarmed`, plus a debug assertion that checks the whole combination every tick and logs the transition that produced it.

  • Twenty states means twenty transitions, so the graph stays manageable.
  • The fix for state explosion is always to switch to a behavior tree.
  • A superstate hierarchy removes the explosion from multiplying independent concerns.
  • Concurrent machines need no coordination because each concern is independent.
  • Patrol routes and investigation points should each get their own state.