The idea: a tree you tick from the root

A game character needs a "brain" that decides what to do next. A behaviour tree is one way to write it. The leaves are small pieces of work: conditions ("is the player next to me?") and actions ("take one step"). The inner nodes decide which leaves run and in what order. Once per game tick the engine ticks the tree from the root. Each node it reaches returns one of three results to its parent: SUCCESS, FAILURE or RUNNING.

Four small trees with child results. Selector: children FAILURE, RUNNING, not ticked; it returns RUNNING. Sequence: children SUCCESS, FAILURE, not ticked; it returns FAILURE. Parallel: children RUNNING and SUCCESS, both ticked; it returns RUNNING. Cooldown decorator: during the cooldown it returns FAILURE and its child is not ticked.
A selector stops at the first child that does not fail, a sequence at the first child that does not succeed; a parallel ticks every child; a decorator can stop its child from running.

The canvas has four parts. The world is an 8 × 6 grid with the guard G, the player P, the patrol route (light blue cells; the four darker corners are its waypoints) and the hut. The tree colours each node by what it returned this tick; a white node was not ticked at all. The FSM is the same guard written as a finite state machine. The tick log keeps one line per tick. The player follows a fixed script, the same in every demo: it appears at tick 4, stands at (6,3) from tick 5, strikes the guard for 60 HP at tick 11 and leaves at tick 14.

SUCCESS, FAILURE, RUNNING

A condition answers at once: SUCCESS or FAILURE. An action that finishes in one tick (Swing sword, Shout) returns SUCCESS. An action that takes many ticks, such as walking somewhere, does one tick of work and returns RUNNING. Next tick the tree is ticked from the root again, and if nothing more important has come up, the same action gets its next tick. The guard's Patrol route never ends, so it always returns RUNNING.

TickWhat the guard doesPath through the treeNodes visited
1–4walks the routeRoot, Attack, Adjacent ✗, Chase, Sees ✗, Patrol6
5–6steps toward the player… Sees ✓, Parallel: Step closer (RUNNING), Shout8
7swings the swordRoot, Attack, Adjacent ✓, Cooldown, Swing5
8–9holds its ground and shouts… Cooldown ✗, Chase, Sees ✓, Parallel: Step (SUCCESS), Shout9

(Demo 1: patrol, nobody in sight; Demo 2: the guard sees the player and chases.)

Selector and sequence

A selector (?, also called a fallback) ticks its children left to right and returns the first result that is not FAILURE. Its children are a priority list: the guard's Root tries Attack, then Chase, then Patrol, and takes the first that works.

A sequence (→) ticks its children left to right and returns the first result that is not SUCCESS. It is "do these steps in order, and stop if one fails". So a sequence that starts with a condition is a guarded behaviour: Attack = "Adjacent ?" then "swing". When the condition fails, the children after it are not ticked at all.

Because the whole tree is ticked from the root every tick, a higher branch takes over the moment its condition becomes true. At tick 5 the player comes within 3 steps, and Chase wins over Patrol without anybody writing a "Patrol to Chase" rule.

Parallel and decorators

A parallel (⇉) ticks all its children in the same tick and combines their results with a policy. This one returns FAILURE if any child fails, SUCCESS if all succeed, and RUNNING otherwise. The guard steps toward the player and shouts "Halt!" in the same tick.

A decorator (◇) has one child and changes how it runs. Cooldown 2 passes its child's result up, but for 2 ticks after the child succeeded it returns FAILURE without ticking the child. So after a swing at tick 7, the Attack sequence fails at ticks 8 and 9, and the Root falls through to Chase: the guard is already adjacent, so Step closer returns SUCCESS and it just stands and shouts. It swings again at tick 10 (Demo 3: attack, then the Cooldown decorator). Other common decorators are Inverter (swap SUCCESS and FAILURE), Repeat, Retry and Timeout.

The same guard as a state machine

An FSM keeps one current state and a list of transitions out of it. To behave like the tree, the guard's FSM needs the states Patrol, Chase and Attack, and an edge for every pair, both ways: Patrol → Chase when it sees the player, Chase → Attack when adjacent, Attack → Chase when the player steps back, and so on. That is 3 × 2 = 6 transitions. The cooldown is a timer inside the Attack state.

Now add "flee to the hut when HP is below 50". In the tree it is one new subtree put first under the Root: → Flee with HP < 50 ? and Rest at hut. That is +3 nodes, and no old node changes. In the FSM it is a new state plus an edge into it from each of the 3 old states and an edge out of it to each of them: +6 transitions, 12 in all, and every old state has to be edited (Demo 5: add a flee branch). With n fully connected states an FSM has n(n − 1) transitions, so each new behaviour costs more than the last.

Left: the FSM before and after adding Flee. Three states with 6 grey edges become four states with 12 edges; the 6 new ones touching Flee are orange. Right: the tree with Root and children Attack, Chase, Patrol; the new Flee subtree with two leaves is added as the first child, drawn in orange.
Adding Flee: the FSM goes from 6 to 12 transitions and every old state changes; the tree gets one subtree of 3 nodes in one place.

Without the Flee branch, the hit at tick 11 changes nothing: HP drops to 40 and the guard keeps fighting, swings a third time at tick 13 and goes back to its route at tick 14 (Demo 4: hurt, but the tree has no flee branch). With it, the guard walks (7,2), (7,1), reaches the hut at tick 13, rests to HP 60 at tick 14, and at tick 15 HP < 50 ? fails, so the Root falls through to Patrol again (Demo 6: retreat, rest, back to patrol).

What re-ticking costs

The tree pays for being reactive: it re-checks its conditions on every tick. Here that is 5 to 9 nodes per tick, and 2 more with the Flee branch, because → Flee and HP < 50 ? are ticked first every time. The FSM only tests its current state's out-edges (2, or 3 with Flee). Over the 16 scripted ticks the tree visits 109 nodes without the Flee branch and 120 with it (Demo 7: the whole script with the flee branch). For one guard that is nothing; for thousands of NPCs, engines cut it with event-driven trees that only re-check a condition when the data it reads has changed.

Two rows of 16 tick boxes. Without Flee: Patrol at ticks 1 to 4, Chase at 5 and 6, Attack at 7, Chase at 8 and 9, Attack at 10, Chase at 11 and 12, Attack at 13, Patrol at 14 to 16, total 109 nodes visited. With Flee: the same to tick 10, then Flee at ticks 11 to 14 and Patrol at 15 and 16, total 120.
The branch that ran on each of the 16 ticks and the nodes visited. The Flee branch adds 2 visits to every calm tick but only 4 visits on a hurt tick.

In real engines

Unreal Engine's behaviour trees keep their data in a blackboard (a key–value store the conditions read) and are event-driven: a decorator that "observes" a blackboard key aborts the running branch when the key changes, instead of re-ticking from the root. Unreal also has services, nodes that update the blackboard at a fixed rate. Godot and Unity have behaviour trees as add-ons, and the same idea is used in robotics (the BehaviorTree.CPP library and ROS 2 Nav2).

A tree is a good fit when there are many behaviours ranked by priority. An FSM is still simpler when there are a few states with clear, rare changes, such as a door or an animation. Many games use both: a tree picks the behaviour and a small state machine runs the animation.

See also Finite State Machines for state machines in hardware, The State Pattern for one object per state in code, the game loop and its fixed timestep that supplies the ticks, steering behaviours and boids for how an action like "move toward the player" can steer smoothly, and grid pathfinding for walking around walls instead of one straight step.

See also Multiplayer Netcode for how the server keeps every player's view of these characters in step.

What the page leaves out

The guard walks in straight steps on an empty grid; real games use pathfinding. The tree has no memory: every composite restarts from its first child on each tick (a "reactive" tree). Many libraries also offer sequences with memory, which resume the RUNNING child without re-checking the earlier ones. Halting a RUNNING node when a higher branch takes over (the "abort" or "halt" call) is only implied. The blackboard, services, utility scoring and GOAP planners are left out. Several NPCs sharing one tree, and the network side of multiplayer games, are not shown.

On the mind map

Open the AI: Search, Games & Reasoning mind mapAI: Search, Games& ReasoningGame search: best move against an opponentGame searchConstraint satisfaction: assign values, no rule brokenConstraint satisfactionLocal search: improve one solution step by stepLocal searchSequential decisions: states, actions, rewardsSequential decisionsProbabilistic reasoning: belief from uncertain evidenceProbabilistic reasoningLogic & knowledge: rules and inferenceLogic & knowledgeState-space search: uninformed searchState-space searchSimulated annealingSimulated annealingaccept worse moves with e^(Δ/T)TSP heuristicsTSP heuristicsnearest neighbour, then 2-opt swapsGenetic algorithmGenetic algorithmselect, crossover, mutate a populationParticle Swarm OptimizationParticle Swarm Optimizationinertia + own best + swarm bestBoids flockingBoids flockingsteering = desired − v, clampedBehaviour treesTick from root: SUCCESS, FAILURE, RUNNINGAnt Colony OptimizationAnt Colony OptimizationShort tours leave more pheromoneOpen the AI: Search, Games & Reasoning mind mapAI: Search, Games & Reasoningimprove one solution step by stepLocal searchSimulated annealingSimulated annealingaccept worse moves with e^(Δ/T)TSP heuristicsTSP heuristicsnearest neighbour, then 2-opt swapsGenetic algorithmGenetic algorithmselect, crossover, mutate a populationParticle Swarm OptimizationParticle Swarm Optimizationinertia + own best + swarm bestBoids flockingBoids flockingsteering = desired − v, clampedBehaviour treesTick from root: SUCCESS, FAILURE, RUNNINGAnt Colony OptimizationAnt Colony OptimizationShort tours leave more pheromone
This page on the AI: Search, Games & Reasoning mind map · 25 pages in 7 branches · open it to see the whole map and test yourself in recall mode.