When would you choose a behavior tree over a finite state machine for an NPC's decision logic, and what does each make harder?
answer
- explicit versus implicit transitions
- edges grow with states
- priority order and reusable subtrees
- where the current mode lives
basics
~20 sChoose a behavior tree when an NPC has many prioritised reactions and reusable sub-behaviours; choose a state machine for a few clear modes with explicit transitions. Trees make remembered modes harder; state machines make adding reactions harder.
solid answer
~50 sA **finite state machine** names each mode and every transition between them, so the current state is explicit, cheap to evaluate and easy to reason about, and staying in a mode is free. Its cost is that transitions grow with the number of states, so a new reaction such as "flee when on fire" needs an edge from most states, and transitions name specific targets, which hinders reuse. A **behavior tree** makes transitions implicit: priority comes from child order and fallback from failure, so a new reaction is one guarded branch at the right priority, and subtrees such as "take cover" are reused across enemy types. Its costs: no single current state, so remembered modes need blackboard flags; reactivity costs evaluation; and "why did it pick that" needs tracing. Few modes with clear triggers favour a state machine; many prioritised reactions and reuse favour a tree.
go deeper
Recall that a state machine names states and explicit transitions, while a behavior tree gets priority from child order and fallback from failure.
Explain why transitions multiply as states grow, and how a tree adds a reaction locally but needs blackboard flags to hold a persistent mode.
Choose between the two for a concrete NPC from its number of modes, reactions, reuse and evaluation budget, and say where a hybrid is the better answer.
Weigh which model a team standardises on for which kinds of NPC, given designer tooling, debugging habits and the cost of mixing models.
## Two models of the same decision Both models answer "what should this NPC be doing now?", but they put the logic in different places. - A **finite state machine** (FSM) has named **states** (idle, attack, reload) and explicit **transitions** between them, each with a guard ("magazine empty"). Exactly one state is current; the logic lives on the edges. - A **behavior tree** (BT) has no current state in that sense. Each evaluation walks a tree of composites, decorators, conditions and actions; **priority** comes from child order in selectors, and **fallback** comes from failure. The logic lives in the structure. ## What a state machine is good at - **The current mode is explicit.** You can print one state name and know what the NPC is doing; designers and debuggers like that. - **Staying in a mode is free.** Once in "reload", the NPC stays until a transition fires, so modes that should persist need no extra bookkeeping. - **Evaluation is cheap.** Only the current state's outgoing transitions are tested. - **Small, mode-based behaviour reads well**: a turret that is idle, tracking, firing or overheated is four states and a handful of edges. What it makes hard: - **Transitions grow with states.** With `n` states there are up to `n * (n - 1)` ordered pairs of states that may each need a transition. A reaction that applies everywhere, such as "flee when on fire", needs an edge from most states, each with a guard to keep in sync. - **Reuse is limited.** A transition names a specific target state, so a combat block cannot be lifted into another NPC without rewiring its edges. - **Shared reactions and interrupt-and-return need extra machinery.** Hierarchical state machines and pushdown stacks exist for these; their design is a subject of its own. ## What a behavior tree is good at - **Adding a reaction is local.** "Escape grenade" is one guarded branch inserted at the right place under the root selector; no other branch changes. - **Priority is visible.** Left-to-right order under a selector is the priority list, and designers can reorder it. - **Subtrees are reusable.** A "take cover" subtree that reads its inputs from blackboard keys works for any soldier type. - **Failure is handled.** If "move to cover" fails at run time, the selector falls through to the next option without a dedicated transition. What it makes hard: - **There is no single current state.** Persistent modes ("I am in a search phase until 30 seconds pass") need blackboard flags or decorators, which is state the FSM had for free. - **Reactivity costs evaluation.** Re-walking from the root each tick re-tests many conditions; event-driven trees cut that cost but need aborts and observers. - **Debugging needs traces.** "Why did it choose that branch?" is answered by replaying which children failed on that tick. - **Flip-flopping between branches** can appear when two conditions hover near their thresholds, and needs a minimum hold time. ## Side by side | concern | finite state machine | behavior tree | |---|---|---| | where logic lives | explicit transitions | tree structure, order and failure | | current mode | one named state | implicit; persistent modes need flags | | adding a global reaction | edges from many states | one branch at the right priority | | reuse across NPCs | limited; edges name targets | subtrees reused with different keys | | evaluation cost | current state's edges only | many nodes per tick, or event-driven | | debugging | read the state and last transition | trace the tick's path through the tree | ## Choosing under constraints 1. **Count the modes and the reactions.** A handful of modes with clear triggers suits a state machine; many prioritised reactions that can fire from anywhere suit a tree. 2. **Ask who edits it.** If designers iterate on priorities and reuse behaviour across enemy types, a tree's locality pays off. 3. **Ask how long modes must persist.** Long-lived phases with entry and exit actions read naturally as states. 4. **Weigh the evaluation budget.** Many agents re-walking large trees every tick is a real cost; plan event-driven evaluation or lower tick rates. 5. **Consider a hybrid.** A tree whose actions run small state machines (a reload with its own steps), or a state machine whose states each run a tree, keeps each model where it is strongest. For a squad soldier who must react to grenades, gunfire and an empty magazine in a strict priority order, and whose combat blocks are shared by several enemy types, a behavior tree is the usual answer. For a turret with four modes, a state machine is shorter and clearer. Neither choice covers planners or utility scoring, which are a separate decision.
- How do you give a behavior tree a persistent mode, such as a search phase that lasts thirty seconds?Store the mode as data: write a search-phase flag and its start time to the blackboard when the phase begins, guard the search branch with a condition on that flag, and clear it when time runs out or the target is found. A decorator such as a time limit can also bound the branch.
- What does a hybrid of the two models look like in practice?Either a tree whose action leaves run small state machines, such as a reload with its own step states, or a state machine whose states each run a tree for their reactions. The aim is explicit modes where modes matter and prioritised reactions where reactions matter.
- Behavior trees are always better than state machines, which are obsolete for game AI.
- A state machine needs only one new transition to add a reaction that applies in every state.
- A behavior tree remembers its current mode the way a state machine does.
- Behavior trees cost nothing extra to evaluate compared with a state machine.
- A state machine cannot be used together with a behavior tree in one NPC.