Decorators, Running & Aborts
The Behavior Trees lesson built sequences, selectors, conditions and actions. Real trees need three more things: decorators that modify a single child, a clear rule for what happens to a node that was Running last tick, and aborts that stop a running action cleanly when something more important comes up.
Decorators
A decorator has exactly one child and changes how it runs or what it returns.
| Decorator | Behaviour | Typical use |
|---|---|---|
| Inverter | Success ↔ Failure, Running unchanged | "if NOT can see player" |
| Succeeder (Force Success) | always Success (after the child finishes) | optional steps in a sequence ("try to shout, then continue") |
| Repeat(n) | runs the child n times, Running in between | "fire 3 shots" |
| Retry(n) | re-runs a failing child up to n times | "try to open the door 3 times" |
| Cooldown(t) | fails without ticking the child if it finished less than t seconds ago | grenades, barks, special attacks |
| Timeout(t) | fails the child if it's been running longer than t | "give up reaching cover after 4 s" |
| Condition guard | ticks the child only if a condition holds | Unreal's Blackboard decorator works like this |
Here a grenade throw sits behind a 3-second cooldown, a "shoot" action is repeated 2 times, and an inverter turns "is reloading" into "is not reloading":
using System;
double now = 0;
bool reloading = false;
Node tree = new Selector(
new Cooldown("grenade", 3, () => now, new Act("throw grenade", () => Status.Success)),
new Sequence(
new Inverter(new Cond("is reloading", () => reloading)),
new Repeat(2, new Act("shoot", () => Status.Success))),
new Act("reload", () => Status.Success));
for (int t = 0; t < 7; t++)
{
now = t;
reloading = t == 5;
Console.WriteLine(quot;t={t}: {tree.Tick()} [{Log.Take()}]");
}
static class Log
{
static string text = "";
public static void Add(string s) => text += (text.Length > 0 ? ", " : "") + s;
public static string Take() { string s = text; text = ""; return s; }
}
enum Status { Success, Failure, Running }
abstract class Node { public abstract Status Tick(); }
class Act : Node
{
readonly string name; readonly Func<Status> f;
public Act(string name, Func<Status> f) { this.name = name; this.f = f; }
public override Status Tick() { Log.Add(name); return f(); }
}
class Cond : Node
{
readonly string name; readonly Func<bool> f;
public Cond(string name, Func<bool> f) { this.name = name; this.f = f; }
public override Status Tick() { bool r = f(); Log.Add(quot;{name}? {(r ? "yes" : "no")}"); return r ? Status.Success : Status.Failure; }
}
class Inverter : Node
{
readonly Node child; public Inverter(Node c) => child = c;
public override Status Tick() => child.Tick() switch
{ Status.Success => Status.Failure, Status.Failure => Status.Success, var s => s };
}
class Repeat : Node
{
readonly int times; readonly Node child; int done;
public Repeat(int times, Node child) { this.times = times; this.child = child; }
public override Status Tick()
{
Status s = child.Tick();
if (s == Status.Running) return s;
if (s == Status.Failure) { done = 0; return s; }
if (++done < times) return Status.Running;
done = 0;
return Status.Success;
}
}
class Cooldown : Node
{
readonly string name; readonly double seconds; readonly Func<double> clock; readonly Node child;
double readyAt = double.NegativeInfinity;
public Cooldown(string name, double seconds, Func<double> clock, Node child)
{ this.name = name; this.seconds = seconds; this.clock = clock; this.child = child; }
public override Status Tick()
{
if (clock() < readyAt) { Log.Add(quot;{name} cooling down"); return Status.Failure; }
Status s = child.Tick();
if (s != Status.Running) readyAt = clock() + seconds;
return s;
}
}
class Sequence : Node
{
readonly Node[] kids; public Sequence(params Node[] k) => kids = k;
public override Status Tick()
{
foreach (var k in kids) { var s = k.Tick(); if (s != Status.Success) return s; }
return Status.Success;
}
}
class Selector : Node
{
readonly Node[] kids; public Selector(params Node[] k) => kids = k;
public override Status Tick()
{
foreach (var k in kids) { var s = k.Tick(); if (s != Status.Failure) return s; }
return Status.Failure;
}
}t=0: Success [throw grenade]
t=1: Running [grenade cooling down, is reloading? no, shoot]
t=2: Success [grenade cooling down, is reloading? no, shoot]
t=3: Success [throw grenade]
t=4: Running [grenade cooling down, is reloading? no, shoot]
t=5: Success [grenade cooling down, is reloading? yes, reload]
t=6: Success [throw grenade]
The grenade is thrown at t=0 and t=3; in between, the cooldown fails without ticking its child, so the selector falls through to shooting. Repeat(2) returns Running after the first shot and Success after the second. At t=5 the inverted condition fails because the agent is reloading, so the sequence fails and the selector reaches reload.
What happens to a Running node?
When a sequence's second child returned Running last tick, what should the sequence do this tick? There are two common answers:
- Reactive (re-check from the start): tick child 1 again, then child 2. Conditions at the front are re-evaluated every tick, so the sequence notices when they stop being true. This is the "re-evaluate from the root" style from the previous lesson.
- With memory (resume): skip straight to the running child. Cheaper, and right for scripted steps that shouldn't be repeated ("play the alarm animation, then run to the button"), but the sequence won't notice that an earlier condition became false.
Libraries usually offer both. BehaviorTree.CPP, for example, has Sequence (remembers its position while a child is running), ReactiveSequence and SequenceWithMemory; Unreal's composites resume the running branch, and its decorators with observer aborts provide the reactivity.
Aborts: stopping a running action cleanly
If a lower-priority action is Running (patrolling, walking to cover) and a higher-priority branch becomes valid, the tree switches branches. The action that was running must be told so it can clean up: stop the move request, cancel the animation, release a reserved cover point. Engines call this abort or halt. Unreal decorators expose it as Observer aborts: Self (abort my own subtree when my condition becomes false), Lower Priority (abort branches to my right when my condition becomes true) or Both.
The reactive selector below remembers which child was running and calls Halt on it when another child takes over:
using System;
bool alarm = false;
var walk = new LongAction("walk patrol route", 4);
var hit = new LongAction("run to alarm", 2);
var root = new ReactiveSelector(
new Guarded("alarm raised", () => alarm, hit),
walk);
for (int t = 0; t < 6; t++)
{
alarm = t >= 2;
Console.WriteLine(quot;t={t}: root -> {root.Tick()}");
}
enum Status { Success, Failure, Running }
abstract class Node
{
public abstract Status Tick();
public virtual void Halt() { }
}
class LongAction : Node
{
readonly string name; readonly int length; int progress;
public LongAction(string name, int length) { this.name = name; this.length = length; }
public override Status Tick()
{
progress++;
Console.WriteLine(quot; {name}: step {progress}/{length}");
if (progress < length) return Status.Running;
progress = 0;
return Status.Success;
}
public override void Halt()
{
Console.WriteLine(quot; {name}: HALTED at step {progress} (stop moving, free resources)");
progress = 0;
}
}
class Guarded : Node
{
readonly string name; readonly Func<bool> cond; readonly Node child;
public Guarded(string name, Func<bool> cond, Node child) { this.name = name; this.cond = cond; this.child = child; }
public override Status Tick() => cond() ? child.Tick() : Status.Failure;
public override void Halt() => child.Halt();
}
class ReactiveSelector : Node
{
readonly Node[] kids; int running = -1;
public ReactiveSelector(params Node[] k) => kids = k;
public override Status Tick()
{
for (int i = 0; i < kids.Length; i++)
{
Status s = kids[i].Tick();
if (s == Status.Failure) continue;
if (running != -1 && running != i) kids[running].Halt(); // someone else took over
running = s == Status.Running ? i : -1;
return s;
}
return Status.Failure;
}
} walk patrol route: step 1/4
t=0: root -> Running
walk patrol route: step 2/4
t=1: root -> Running
run to alarm: step 1/2
walk patrol route: HALTED at step 2 (stop moving, free resources)
t=2: root -> Running
run to alarm: step 2/2
t=3: root -> Success
run to alarm: step 1/2
t=4: root -> Running
run to alarm: step 2/2
t=5: root -> Success
Without the Halt call the patrol action would keep its progress (and in an engine, its move request) and resume from step 3 later, from the wrong place. Forgetting to clean up on abort is one of the most common behavior tree bugs: agents that keep a reserved cover point forever, sounds that never stop, or a "walk" animation playing while standing still.
Tip: Give every long-running action three hooks: start, tick, halt. Unity's Behavior package actions have
OnStart,OnUpdateandOnEnd; Unreal'sUBTTaskNodehasExecuteTask,TickTaskandAbortTask. Put cleanup in the end/abort hook, never only at the end of a successful tick.
Test yourself
A Cooldown(3 s) decorator's child finished 1 second ago. What happens when the decorator is ticked?
- It ticks the child anyway
- It returns Failure without ticking the child
- It returns Running
- It resets the cooldown
What is the difference between a reactive sequence and a sequence with memory?
- There is none
- A reactive sequence re-checks earlier children every tick; one with memory resumes at the running child
- Only reactive sequences can return Running
- Sequences with memory are always faster and always better
Why must a running action be halted when a higher-priority branch takes over?
- To save memory
- So it can clean up: cancel moves, stop animations, release reserved resources
- Because Running nodes can't be ticked twice
- To print a log line