skip to content

When several transition guards in an NPC state machine pass in the same tick, which transition should fire, and how do you make that explicit?

level: middleimportance: should knowfreq 55%

answer

  1. order is a decision
  2. first passing guard wins
  3. death outranks everything
  4. any-state transitions need exceptions
  5. table rows with priorities

basics

~20 s

Evaluation order decides, so make it a deliberate priority: test guards in a fixed, documented order, fire the first that passes and put overriding transitions such as death first. A transition table keeps that order visible and checkable in one place.

solid answer

~50 s

Simultaneous guards are normal: a killing blow can leave a boss at zero health, stunned and below half health in the same tick. Most machines use **first match**: guards are tested in a fixed order and the first that passes fires, so the order is the priority. Make it explicit: put global transitions such as `Dead` and `Stunned` ahead of each state's own, give each row a priority number, and keep guards free of side effects so evaluation order cannot change the world. A **table-driven** machine stores rows of `(from, guard, to, priority)` as data, which tools can sort, display and lint for unreachable states or tied priorities; **coded** transitions inside each state's update spread the order across code where nobody sees it whole. Any-state transitions also need an exception for their own target, or `Dead` re-enters itself every tick.

code

json · 6 lines
json
[
  {"from": "*", "exceptFrom": ["Dead"], "guard": "health <= 0", "to": "Dead", "priority": 0},
  {"from": "*", "exceptFrom": ["Dead"], "guard": "stunHitThisTick", "to": "Stunned", "priority": 10},
  {"from": "Attack", "guard": "health < 0.5 * maxHealth and not enragedYet", "to": "Enraged", "priority": 20},
  {"from": "Attack", "guard": "distanceToPlayer > leashRange", "to": "Idle", "priority": 30}
]

go deeper

for a junior

Know that when several guards pass, the evaluation order decides, and that death and stun are usually tested before ordinary transitions.

for a middle

Explain first-match evaluation, pure guards, one transition per tick and the any-state self-exclusion, and sketch a transition table with a priority column.

for a senior

Show how you would catch priority bugs before play: lint for ties and unreachable rows, log evaluated guards per tick, and choose when table-driven transitions earn their tooling cost.

for a principal

A lead's angle here is the shared contract: one priority rule and one table format across every NPC, so designers and tools read all machines the same way.

## Why guards pass together A **guard** is the condition on a transition. In a real fight several guards often become true in the same tick because one event changes several facts at once. Picture a boss in `Attack` with these outgoing transitions: - `health <= 0` leads to `Dead`. - `stunHitThisTick` leads to `Stunned`. - `health < 0.5 * maxHealth and not enragedYet` leads to `Enraged`. - `distanceToPlayer > leashRange` leads to `Idle`. A heavy finishing blow that also stuns makes the first three true together. The machine must pick exactly one, and that choice is the designer's, not an accident of how the code was typed. ## First match with an explicit order The usual rule is **first match**: 1. Collect the transitions that can leave the current state: the global ones first, then the state's own. 2. Sort them by priority (or keep them in a list whose order is the priority). 3. Evaluate guards in that order and stop at the first that passes. 4. Fire that one transition: exit, transition action, enter. With this rule, the order above is correct: death first, stun second, enrage third. If the enrage row had been listed first, a killing blow would play the enrage roar and the boss would die only on a later tick, which players read as a bug. Two supporting rules keep first match honest: - **Guards are pure.** A guard reads state and returns yes or no. Because testing stops at the first pass, a later guard with a side effect would run on some ticks and not others. - **One transition per tick, or a hard cap.** If the newly entered state's guards are re-tested in the same tick, a pair of states whose guards are each other's negation can loop indefinitely within one frame. ## Global and any-state transitions Transitions that apply from almost everywhere, such as death or stun, are often declared once as **any-state** (or global) transitions instead of being copied onto every state. They need two rules of their own: - **Exclude the target itself.** An any-state transition to `Dead` with guard `health <= 0` still passes while the boss is in `Dead`; without an exclusion it re-enters `Dead` every tick, replaying the death sequence. - **Decide whether they outrank local transitions.** Usually yes for death, and a design choice for the rest. When many states share the same outgoing transitions, a hierarchical machine with a common parent state is the structural answer; any-state rows are its flat cousin. ## Table-driven versus coded transitions | Aspect | Table-driven | Coded in each state | |---|---|---| | Where the order lives | One sorted list of rows | Scattered across state code | | Who can edit it | Designers, through data | Programmers, through code | | Tooling | Can draw the graph, list unreachable states, flag tied priorities | Needs reading the code | | Expressiveness | Guards limited to what the table language offers | Any condition the code can compute | | Debugging | Log the row index that fired | Log from inside each state | A **table-driven** machine stores rows as data, for example `from`, `guard`, `to` and `priority`. Guards are either small expressions or names of registered condition functions. The table makes priority a visible column, which is the point of this question. A **coded** machine has each state's update return the next state; that is quicker to write for a three-state creature and opaque for a twenty-state one, because nobody can see the whole order at once. ## Checks worth automating Once transitions are data, cheap checks catch most priority bugs before play: - Two rows from the same state with the same priority: the tie must be broken explicitly. - A row that can never fire because an earlier row's guard is strictly broader (a `health <= 0.5` row listed after a `health <= 0.9` row). - A state with no outgoing rows other than through global transitions, if that was not intended. - An any-state row without an exclusion for its own target. ## Debugging a wrong pick When a transition fires that should not have, log per tick the current state, every guard evaluated with its result, and the row that fired. A trace of a killing blow showing `Enraged` firing before `Dead` was tested points straight at the order, not at the health code.

  • Why not pick the most specific guard instead of a fixed order?
    "Most specific" has no general definition: is `health <= 0` more specific than `stunHitThisTick`? Any automatic rule ends up being a hidden order that designers cannot predict. An explicit priority column is boring, visible and testable, and that is what a behaviour model needs when a single hit changes several facts at once.
  • What goes wrong if guards are re-tested immediately after a transition, in the same tick?
    Chained transitions can loop: if state A leaves when a condition holds and state B leaves when it does not, the machine can bounce between them repeatedly inside one frame. Either allow one transition per tick, or allow chaining with a small hard cap and a warning when the cap is hit.

  • The machine automatically picks the most specific guard when several pass.
  • If two guards pass, both transitions fire and the later one wins.
  • A global transition to Dead needs no exception for the Dead state itself.
  • Guard order is an implementation detail designers never need to see.
  • Guards may update counters because each one runs exactly once per tick.