Reactive versus memory nodes
Whether a sequence or fallback starts again from its first child on every tick or picks up where it left off, what each choice costs, and when each is right.
A night watchman is walking to the beacon tower when the alarm bell rings. Should he notice? A behavior tree answers that question in how its sequences and fallbacks treat a child that is still Running (the status a node returns while its work takes more than one tick). There are two answers, and every runtime picks one or offers both:
- A reactive node starts again from its first child on every tick. Everything in front of the running child is checked again, so a change in the world is seen on the very next tick.
- A memory node remembers which child was running and, on the next tick, goes straight back to it. The children in front of it are not ticked again until the node has finished.
The building blocks page wrote its Sequence and Fallback the reactive way: “If a child is running, the sequence is running and picks up from the top on the next tick.” That is also how BeeTreeCraft runs every tree. This page shows what that buys you, what it costs, and how to write the memory version when you need it.
What reactive buys you
Reactive nodes make priorities work. In a fallback, the first child is the most important. If it is checked again on every tick, then the moment it can run, it wins, even if a lower branch was in the middle of something. The watchman drops the walk and runs to the gate the same tick the bell rings. The running walk is halted: its parent stops ticking it, and a good runtime tells it so, so it can stop the footsteps sound or release the path it reserved.
The same holds inside a sequence. A condition in front of a running action is a guard that is checked every tick: “keep walking while the path is clear” stops the walk as soon as the path is blocked.
What reactive costs
Every child in front of the running one is ticked again, every tick. A condition costs little to check again. An action that already succeeded is a different matter: if ring_bell comes before climb_stairs, a reactive sequence rings the bell again on every tick of the climb. Two rules keep a reactive tree honest:
- Put conditions, not actions with side effects, in front of a long-running action.
- Write actions so that ticking a finished one again is harmless.
walk_to_towerreturns Success at once when the watchman is already at the tower.
When memory is right
A memory node suits a list of steps that must each happen once, in order, where nothing should interrupt the middle: a scripted scene, a combo attack, “open the chest, take the gold, close the chest”. It also saves work in a large tree, since finished children are not ticked again. The price is that the node is blind while it waits: a memory sequence does not check its earlier conditions again until it finishes or fails. When a step needs a guard, wrap the memory sequence in a reactive one whose first child is the guard. The guard is checked every tick, and the steps behind it each run once.
| Reactive | Memory | |
|---|---|---|
| On each tick starts at | the first child | the child that was running |
| Sees a change in the world | on the next tick | when the node finishes |
| Children in front of the running one | ticked again, every tick | not ticked until the node finishes |
| Right for | priorities, guards, anything that must notice danger | scripted steps that each happen once |
| Found as | BehaviorTree.CPP ReactiveSequence, ReactiveFallback; BeeTreeCraft’s sequence and selector | BehaviorTree.CPP Sequence, Fallback; composites in Unreal Engine |
A tree you can run
The watchman answers the alarm first and lights the beacon otherwise. Lighting the beacon is three steps, and walking to the tower takes many ticks.
version: "1.0"
metadata:
name: night-watch
description: "Walks to the tower and lights the beacon, but drops everything when the alarm bell rings"
tags: [reactive, memory]
tree:
type: selector
name: watch_root
children:
- type: sequence
name: answer_alarm
children:
- {type: condition, name: alarm_ringing, node: AlarmRinging}
- {type: action, name: run_to_gate, node: RunToGate}
- type: sequence
name: light_the_beacon
children:
- {type: action, name: walk_to_tower, node: WalkToTower}
- {type: action, name: climb_stairs, node: ClimbStairs}
- {type: action, name: light_beacon, node: LightBeacon}
beetree simulate runs one tick, and --override sets what a leaf returns on it. Run three ticks by hand, one command each:
beetree simulate night-watch.beetree.yaml --override alarm_ringing=FAILURE --override walk_to_tower=RUNNING
beetree simulate night-watch.beetree.yaml --override alarm_ringing=SUCCESS --override run_to_gate=RUNNING
beetree simulate night-watch.beetree.yaml --override alarm_ringing=FAILURE --override climb_stairs=RUNNING
- No bell:
answer_alarmfails at its condition, and the tree is running atwalk_to_tower. - The bell rings: the root starts again from its first child,
alarm_ringingsucceeds, and the watchman runs to the gate.light_the_beaconis not ticked at all, so the walk is halted. A memory fallback would have gone straight back tolight_the_beaconand kept walking. - The alarm is over and he has reached the tower: the trace shows
walk_to_towerticked again beforeclimb_stairs. That is the cost of a reactive sequence, and whywalk_to_towermust return Success at once when he is already there.
In the web builder, paste the tree and use Step through: it pauses at each leaf so you choose its status, which plays one tick the same way. A run of many ticks in a row, with a status per tick, is not in the simulator yet.
The tree format has no memory variant of sequence or selector today, so a tree file always describes the reactive version. The code BeeTreeCraft generates for Unity and Godot is reactive too. For Unreal, the generated classes go into Unreal’s own behavior tree, whose composites remember their running child; to make a condition interrupt a lower branch there, set Observer aborts to Lower Priority on its decorator.
Engine code
A memory sequence needs one thing a reactive one does not: the index of the child it is waiting on. It moves past a child only when that child succeeds, and goes back to the first child when it finishes, either way. Each sample uses the Status type and tick() contract from Building blocks; compare it with the Sequence there, which is the reactive one.
Agnostic
MemorySequence(children):
current = 0 // the child to resume on the next tick
tick():
while current < length(children):
childStatus = children[current].tick()
if childStatus == Running: return Running // come back here next tick
if childStatus == Failure:
current = 0
return Failure
current = current + 1 // this child is done for good
current = 0 // finished: next time, start from the top
return SuccessUnity C#
public class MemorySequence : Node
{
readonly Node[] children;
int current; // the child to resume on the next tick
public MemorySequence(params Node[] children) { this.children = children; }
public override Status Tick()
{
while (current < children.Length)
{
var status = children[current].Tick();
if (status == Status.Running) return Status.Running; // come back here next tick
if (status == Status.Failure) { current = 0; return Status.Failure; }
current++; // this child is done for good
}
current = 0; // finished: next time, start from the top
return Status.Success;
}
}GDScript
class MemorySequence extends BTNode:
var children: Array[BTNode]
var current := 0 # the child to resume on the next tick
func _init(c: Array[BTNode]): children = c
func tick() -> Status:
while current < children.size():
var status := children[current].tick()
if status == Status.RUNNING:
return Status.RUNNING # come back here next tick
if status == Status.FAILURE:
current = 0
return Status.FAILURE
current += 1 # this child is done for good
current = 0 # finished: next time, start from the top
return Status.SUCCESSUnreal C++
class FMemorySequence : public FNode
{
public:
TArray<TSharedPtr<FNode>> Children;
int32 Current = 0; // the child to resume on the next tick
EStatus Tick() override
{
while (Current < Children.Num())
{
const EStatus Status = Children[Current]->Tick();
if (Status == EStatus::Running) return EStatus::Running; // come back here next tick
if (Status == EStatus::Failure) { Current = 0; return EStatus::Failure; }
++Current; // this child is done for good
}
Current = 0; // finished: next time, start from the top
return EStatus::Success;
}
};A memory fallback is the mirror image: it moves past a child only when that child fails. A full runtime also halts the children it stops ticking. When a reactive node switches to an earlier child, it tells the child that was running to stop; when a memory node is itself halted, it sets current back to 0. Without that reset, a memory sequence interrupted halfway resumes halfway the next time it runs, which is rarely what anyone wants.
Where it comes from
Colledanchise and Ögren treat reactiveness as one of the two reasons behavior trees work, and introduce memory nodes as the exception, in Behavior Trees in Robotics and AI (2018). The BehaviorTree.CPP tutorial Sequence versus ReactiveSequence runs the same tree both ways and prints the ticks. Colledanchise and Natale compare how libraries handle memory nodes in On the Implementation of Behavior Trees in Robotics (2021), and Epic’s Behavior Tree in Unreal Engine: Overview explains how Unreal gets reactiveness from Observer Aborts instead. More reading is on the Resources page.
Next, see how the format describes evaluation in the tree format’s Reactive evaluation section, or open the Build hub and put a guard in front of a long action of your own.