skip to content

Behavior Trees

How behavior trees tick: sequences and selectors as priority logic, decorators, the running status, blackboards and aborts. Interviews probe running-state subtleties and BT-vs-FSM choices.

on this pageshow

explore

questions

5

In a behavior tree, how do sequence and selector nodes decide what to run, and what roles do decorators and leaf nodes play?

level: juniorimportance: must knowfreq 75%

answer

  1. three statuses flow up
  2. AND versus OR over children
  3. left-to-right order is priority
  4. one child, altered result

basics

~20 s

A sequence runs its children left to right and stops at the first failure; a selector stops at the first success, so child order sets priority. Decorators wrap one child to change its result or timing; leaves are conditions and actions.

solid answer

~50 s

Each tick, a node returns `success`, `failure` or `running`. A **sequence** ticks its children left to right: it returns failure as soon as one fails, running if one is running, and success only when all succeed, so it is an AND over the steps of one task ("magazine empty?", then "reload"). A **selector** (fallback) ticks left to right and returns success at the first child that succeeds, running at the first that is running, and failure only if all fail, so it is an OR whose order is the priority list: escape a grenade, else take cover, else reload, else advance. **Decorators** have one child and change its result or when it may run: an inverter swaps success and failure, a cooldown fails while its timer is pending, a repeater re-runs the child. **Leaves** do the work: conditions test the world and answer at once; actions change it and may stay running across ticks.

code

pseudocode · 13 lines
pseudocode
function tickSelector(node):
    for each child in node.children, left to right:
        status = tick(child)
        if status == SUCCESS or status == RUNNING:
            return status
    return FAILURE

function tickSequence(node):
    for each child in node.children, left to right:
        status = tick(child)
        if status == FAILURE or status == RUNNING:
            return status
    return SUCCESS

go deeper

for a junior

Recall the three statuses and the rule for each composite: a sequence stops at the first failure, a selector at the first success, and child order in a selector is priority.

for a middle

Walk one tick of a real tree aloud, including a child returning running, and explain why a failing action, not only a false condition, makes a selector fall through.

for a senior

Show that you design trees around priority order and failure: where guards sit, which actions can fail at run time, and what a parallel's policy should be for the behaviour you want.

for a principal

Be ready to say which node vocabulary a team should standardise on, such as which decorators exist and what a parallel's policy means, so trees read the same across designers.

## The tick and the three statuses A **behavior tree** is a tree of nodes that an NPC's decision logic walks, or **ticks**, at a rate the game chooses: every frame, a few times a second, or on demand. Ticking a node runs its logic for that moment and returns one of three statuses to its parent: - **`success`**: the node did what it is for. The condition held, or the action completed. - **`failure`**: it could not. The condition was false, or the action was impossible or gave up. - **`running`**: an action has started and needs more ticks to finish, such as walking to cover or playing a reload. The parent reads that status and decides what to tick next. That one small contract is what lets small, reusable nodes compose into a large behaviour without any node knowing about the others. ## Three families of node - **Composites** have several ordered children and decide which of them to tick: the sequence, the selector and the parallel. - **Decorators** have exactly one child and change its result, or decide whether and how often it may run. - **Leaves** do the work. **Conditions** test the world and answer at once with success or failure. **Actions** change the world and may return running for many ticks. ## Sequence and selector A **sequence** is a logical AND over the steps of one task. On each tick it walks its children from left to right: 1. If a child returns failure, the sequence returns failure at once and ticks no later child. 2. If a child returns running, the sequence returns running and stops for this tick. 3. If a child returns success, the sequence moves on to the next child. 4. When the last child succeeds, the sequence returns success. A **selector**, also called a fallback or priority node, is a logical OR over alternatives. It walks its children from left to right and returns success at the first child that succeeds, running at the first that is running, and failure only when every child has failed. Because it stops at the first child that does not fail, **the left-to-right order is the priority order**: the leftmost option that can work wins. | composite | stops early at | returns success when | typical use | |---|---|---|---| | sequence | the first child that fails or runs | every child succeeds | the steps of one task: check, move, act | | selector | the first child that succeeds or runs | any child succeeds | prioritised alternatives and fallbacks | | parallel | whatever its policy says | its success policy is met | two activities at once | A **parallel** node ticks all of its children on the same tick and resolves by a stated policy, for example "succeed when all succeed, fail as soon as one fails" or "succeed when any one succeeds". Implementations differ on the default, so the policy should always be stated, and when a parallel resolves, any child still running has to be stopped. ## Decorators and leaves Common decorators include: - **Inverter**: swaps success and failure; running passes through unchanged. - **Succeeder** (force success): reports success once its child finishes, whatever it returned, so an optional step cannot fail its sequence. - **Repeater**: re-runs its child a fixed number of times, or until the child fails. - **Cooldown**: typically fails without ticking the child if the child finished less than a set time ago. - **Guard** (a conditional decorator): lets the child run only while a condition holds. Conditions should be cheap and instant; a check that needs several ticks is really an action. Actions keep their own progress between ticks, so that the second tick of a reload continues it instead of starting it again. ## Putting it together: a squad soldier A soldier should get away from a grenade, take cover when shot at, reload when the magazine is empty, and otherwise advance. A root selector lists those as prioritised options, each a sequence that starts with its own condition: ```pseudocode selector (root) sequence: grenade nearby? then move away from grenade sequence: under fire? then pick cover point then move to cover sequence: magazine empty? then reload advance toward objective ``` On a tick where the soldier is under fire and no grenade is near, the first sequence fails at its condition, so the selector tries the second. Its condition succeeds, a cover point is found and "move to cover" returns running, so that sequence and the root both return running. If no cover point exists, "pick cover point" fails, the second sequence fails, and the selector falls through to reloading or advancing. That last case is the difference between a selector and an if-else chain. An if-else commits to a branch once its test passes; a selector also falls through when an **action** fails at run time, so a plan that turns out to be impossible gives way to the next option automatically.

  • How is a selector different from an if-else chain?
    An if-else commits to a branch once its test passes. A selector also falls through when an action fails at run time: if "pick cover point" finds no cover, that sequence fails and the selector tries the next option, such as reloading. Failure is a first-class result, not only a false condition.
  • What does a parallel node do, and why must its policy be stated?
    It ticks all its children on the same tick, for example "move to cover" alongside "fire at target". Whether it succeeds when one child succeeds or only when all do, and whether one failure fails it, varies between implementations. State the policy, and stop any child still running once the parallel resolves.

A selector is like getting into a locked-out house: try the front door, and if that fails try the back door, then a window. A sequence is the steps of one attempt, such as find the key, unlock, open, where any failed step ends that attempt.

  • A selector picks the child with the highest score.
  • A sequence keeps ticking the remaining children after one of them fails.
  • Running is an error status that the parent should treat as failure.
  • Child order inside a composite is cosmetic; priority comes from separate weights.
  • A decorator can wrap several children, the way a composite does.
open as a page

In a behavior tree, what does the running status mean, and how does the tree resume a running action on the next tick?

level: middleimportance: must knowfreq 62%

basics

~20 s

Running means an action has started but needs more ticks. Next tick, a reactive tree re-checks earlier children before reaching the action again, while a composite with memory skips straight to it; a preempted action must be halted.

open as a page

When would you choose a behavior tree over a finite state machine for an NPC's decision logic, and what does each make harder?

level: seniorimportance: must knowfreq 58%

basics

~20 s

Choose 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.

open as a page

What is a blackboard in a behavior tree, and what problems appear when many nodes read and write it?

level: middleimportance: should knowfreq 50%

basics

~20 s

A blackboard is a key-value store a tree's nodes share: sensors write facts such as the target or ammo, conditions and actions read them. It decouples nodes but hides data flow and invites stale values, key mismatches and write conflicts.

open as a page

A squad soldier's behavior tree keeps finishing a reload while a grenade lands beside him; how do you make the tree interrupt the running reload?

level: seniorimportance: should knowfreq 45%

basics

~20 s

Place the grenade branch above the reload and make sure something re-checks its guard while the reload runs: a reactive root re-walked each tick, or, in an event-driven tree, a lower-priority abort observing the grenade key.

open as a page