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.
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.
| Tick | What the guard does | Path through the tree | Nodes visited |
|---|---|---|---|
| 1–4 | walks the route | Root, Attack, Adjacent ✗, Chase, Sees ✗, Patrol | 6 |
| 5–6 | steps toward the player | … Sees ✓, Parallel: Step closer (RUNNING), Shout | 8 |
| 7 | swings the sword | Root, Attack, Adjacent ✓, Cooldown, Swing | 5 |
| 8–9 | holds its ground and shouts | … Cooldown ✗, Chase, Sees ✓, Parallel: Step (SUCCESS), Shout | 9 |
(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.
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.
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.