How does a pushdown state machine let an NPC resume an interrupted state, such as a boss returning to Attack after a stun?
answer
- a stack of states
- top of stack is current
- push to interrupt, pop to resume
- resume must re-check the world
basics
~20 sA pushdown state machine keeps a stack of states and runs the top one. An interruption such as Stunned is pushed over Attack and popped when it ends, so Attack resumes with its data instead of restarting.
solid answer
~40 sA **pushdown state machine** adds a stack to a finite state machine: the state on top is current, and the states below are paused with their data kept. Its operations are `push` (interrupt: put `Stunned` on top of `Attack`), `pop` (finish: remove `Stunned`, and `Attack` resumes) and `replace` (an ordinary transition that swaps the top). Paused states usually get `pause` and `resume` hooks distinct from `enter` and `exit`, so `Attack` keeps its target and cooldown. The pitfalls are about stale and runaway stacks: a resumed state must re-check its world, since the target may have died; repeated pushes, such as a stun while stunned, need a policy so the stack cannot grow without bound; and `Dead` must clear the stack, or a later pop resurrects a paused living state.
code
pseudocode · 16 linesfunction push(npc, s):
if not npc.stack.isEmpty():
npc.stack.top().pause(npc)
npc.stack.push(s)
s.enter(npc)
function pop(npc):
finished = npc.stack.pop()
finished.exit(npc)
if not npc.stack.isEmpty():
npc.stack.top().resume(npc) # must re-check target, range, timers
function die(npc):
while not npc.stack.isEmpty():
npc.stack.pop().exit(npc) # clean up paused states too
push(npc, Dead)go deeper
Recall that a pushdown machine keeps a stack of states, runs the top one, pushes to interrupt and pops to go back.
Explain push, pop and replace with their hooks, and why paused states use pause and resume rather than exit and enter.
Show the production rules: re-validate on resume, a policy for repeated pushes and a depth cap, terminal states that clear the stack, and logging of every push and pop.
At lead level, decide where interruption lives across the codebase, a stack, history substates or both, and keep that choice consistent across NPC types.
## The problem a stack solves In a flat state machine, leaving a state forgets it. When a heavy hit stuns the boss mid-fight, `Attack` exits, `Stunned` enters, and when the stun ends the machine needs a transition back. Which state should it go to? The boss might have been in `Attack`, `Enraged` or `Idle`. A flat machine needs either a remembered variable or one `Stunned` variant per possible return state. A **pushdown state machine**, named after the pushdown automaton (a finite machine with a stack), answers this structurally: interruptions go on top of a stack, and finishing an interruption reveals what was underneath. ## Operations The current state is always the **top** of the stack. Three operations change it: | Operation | What it does | Boss example | Hooks that run | |---|---|---|---| | `push(s)` | Put `s` on top; the old top is paused | Stun hit: push `Stunned` over `Attack` | old top `pause`, `s.enter` | | `pop()` | Remove the top; the one beneath resumes | Stun timer ends: pop `Stunned` | top `exit`, new top `resume` | | `replace(s)` | Swap the top for `s`, an ordinary transition | `Attack` to `Enraged` | old top `exit`, `s.enter` | The paused state keeps its own data: the chosen target, the remaining attack cooldown, the step reached in a combo. That is why `resume` is usually a separate hook from `enter`: `enter` would reset all of it. ## Walking the boss through a stun 1. Stack: `[Attack]`. The boss is swinging, two hits into a three-hit combo. 2. A heavy hit arrives: `push(Stunned)`. `Attack.pause` stops the swing; `Stunned.enter` starts a three-second timer. Stack: `[Attack, Stunned]`. 3. `Stunned.update` counts down. `Attack.update` does not run while it is covered. 4. Timer ends: `pop()`. `Stunned.exit` runs; `Attack.resume` runs. Stack: `[Attack]`. 5. `Attack` continues with its stored target and combo step, after re-checking that both still make sense. ## Pitfalls and the rules that prevent them - **Stale resume.** While the boss was stunned, the player may have died, moved behind a wall or left the arena. `resume` must re-validate: is the target alive and in range, is the combo still possible? If not, it immediately transitions as its guards dictate. - **Unbounded growth.** If every stun pushes, a stun while stunned stacks `Stunned` on `Stunned`. Decide a policy per interrupt: refuse the push, restart the current one, or replace it. A small depth cap with a logged warning catches mistakes. - **Death and other terminal states.** Pushing `Dead` on top leaves living states underneath; any later pop resurrects one. Terminal transitions should clear the stack, running `exit` on every state on the way down, and then push `Dead`. - **Timers that should or should not pause.** A cooldown measured in elapsed game time keeps running while the state is covered; a counter decremented in `update` freezes. Choose per timer. - **Pops from the wrong place.** Let the interrupting state pop itself when it finishes. An outside system that pops blindly can remove a different state if a second interruption was pushed in the meantime. - **Two hook sets.** States must be written knowing they can be paused. A state that holds a reservation, such as an attack slot, should release it in `pause` if other NPCs need it while it is covered. ## Pushdown versus history A hierarchical machine's **history** substate also restores something after an interruption, but it is narrower: it remembers the last active substate of one superstate. A stack restores an arbitrary previous state and handles nested interruptions, such as a stun during a taunt during an attack. Many NPC machines combine both: a hierarchy for shared transitions, a stack for interruptions. ## Limits - A stack restores **one** suspended line of behaviour. Two independent concerns that interrupt each other, such as a town guard's alertness and their conversation, are better as separate concurrent machines. - The deeper the stack, the harder it is to debug; log every push and pop with the full stack contents.
- Why give paused states separate pause and resume hooks instead of reusing exit and enter?Exit and enter mean leaving and arriving for good: exit releases what the state holds and enter resets it. A paused state will come back, so it should keep its target, cooldown and combo step. Separate hooks let it stop work temporarily, then re-check the world on resume, without throwing its data away.
- What should happen when the boss is stunned again while already stunned?It is a design decision, made explicitly per interrupt. Common choices: ignore the new stun (stun immunity), restart the current stun timer without pushing, or replace the top with a fresh `Stunned`. Pushing a second `Stunned` is rarely intended, and without a depth cap repeated stuns can grow the stack without bound.
- Pushing a state runs the covered state's exit, so its data is reset.
- A resumed state can trust the target it stored before the interruption.
- Dying should push Dead on top of the stack like any other interruption.
- Every stun hit should push a new Stunned state.
- A pushdown machine replaces hierarchy entirely, so shared transitions need nothing else.