Decorators as policy
Keep what an agent does apart from when, how often and how long it may try: retries, cooldowns, timeouts and guards as decorators.
An action should say what the agent does: open a door, throw a grenade, call for help. It should not also decide how many times to try, how long to wait, or whether it is allowed right now. Those are rules about the action, and a decorator is the place for them. The building blocks page introduced decorators as nodes that wrap one child. This page treats them as policy: small, reusable rules you wrap around any action without changing the action itself.
Why bother? Put a retry counter inside OpenDoor and you will write it again inside PickLock, ClimbLadder and every other action that can fail for a moment. Each copy drifts a little. When the designer asks for “three tries, then give up”, you change ten files. With a Retry decorator, the action stays simple and the rule lives in the tree, where a designer can see it and change it.
The four common policies
| Policy | Decorator | What it does to the child |
|---|---|---|
| Try again | Retry(n) | Re-runs a failing child up to n times before it reports failure. |
| Not too often | Cooldown(seconds) | After the child finishes, fails at once until the time has passed. |
| Not too long | Timeout(seconds) | Fails the child if it is still running when the time runs out. |
| Only when allowed | Guard(condition) | Ticks the child only while a check holds; fails otherwise. |
Retry and Timeout pull in opposite directions, so order matters. Timeout(Retry(OpenDoor)) gives all the tries one shared deadline. Retry(Timeout(OpenDoor)) gives each try its own deadline. Read the tree from the outside in and say the rule out loud: “give up after five seconds, trying up to three times” is the first one.
A cooldown keeps an expensive or noisy action from firing every frame. A grenade thrower without one throws a grenade on every tick its conditions hold. A guard is the decorator form of a condition. It suits a check that belongs to one action, not to the whole branch.
A tree you can run
A guard that tries a locked door a few times within a deadline, falls back to calling for help (but not more than once every ten seconds), and only throws a grenade when it can see the target:
version: "1.0"
metadata:
name: door-breacher
description: "Retries a locked door under a deadline, with a cooldown on calling for help"
tags: [decorators, policy]
tree:
type: selector
name: get_through
children:
- type: decorator
name: give_up_after_5s
decorator: timeout
parameters: {seconds: 5}
children:
- type: decorator
name: try_three_times
decorator: retry
parameters: {max_attempts: 3}
children:
- {type: action, name: open_door, node: OpenDoor}
- type: sequence
name: grenade_if_seen
children:
- {type: condition, name: target_visible, node: TargetVisible}
- type: decorator
name: one_grenade_per_8s
decorator: cooldown
parameters: {seconds: 8}
children:
- {type: action, name: throw_grenade, node: ThrowGrenade}
- type: decorator
name: call_every_10s
decorator: cooldown
parameters: {seconds: 10}
children:
- {type: action, name: call_for_help, node: CallForHelp}
Save it as door-breacher.beetree.yaml, then check it and step through it:
beetree validate door-breacher.beetree.yaml
beetree simulate door-breacher.beetree.yaml --override open_door=FAILURE
The simulator runs one tick and has no clock. It passes Retry, Cooldown and Timeout straight through, so overriding open_door to FAILURE shows the fallback to the grenade branch the moment the door fails. The trace shows the shape of the tree; the counting and the timing are your engine’s job. beetree generate emits these decorators as hooks (in Unity, a pass-through rule) for you to fill in with code like the samples below.
The code behind each policy
Each sample uses the Status type and tick() contract from Building blocks, which also shows Retry. Time comes from the engine: Time.time in Unity, Time.get_ticks_msec() in Godot and FPlatformTime::Seconds() in Unreal.
Agnostic
Cooldown(child, seconds):
readyAt = 0
tick():
if now() < readyAt: return Failure // still cooling down
status = child.tick()
if status != Running: readyAt = now() + seconds
return status
Timeout(child, seconds):
startedAt = none
tick():
if startedAt == none: startedAt = now()
status = child.tick()
if status != Running: startedAt = none; return status
if now() - startedAt > seconds:
startedAt = none; return Failure // took too long: give up
return Running
Guard(child, check):
tick():
if not check(): return Failure // not allowed right now
return child.tick()Unity C#
public class Cooldown : Node
{
readonly Node child; readonly float seconds; float readyAt;
public Cooldown(Node child, float seconds) { this.child = child; this.seconds = seconds; }
public override Status Tick()
{
if (Time.time < readyAt) return Status.Failure; // still cooling down
var status = child.Tick();
if (status != Status.Running) readyAt = Time.time + seconds;
return status;
}
}
public class Timeout : Node
{
readonly Node child; readonly float seconds; float startedAt = -1f;
public Timeout(Node child, float seconds) { this.child = child; this.seconds = seconds; }
public override Status Tick()
{
if (startedAt < 0f) startedAt = Time.time;
var status = child.Tick();
if (status != Status.Running) { startedAt = -1f; return status; }
if (Time.time - startedAt > seconds) { startedAt = -1f; return Status.Failure; }
return Status.Running;
}
}
public class Guard : Node
{
readonly Node child; readonly System.Func<bool> check;
public Guard(Node child, System.Func<bool> check) { this.child = child; this.check = check; }
public override Status Tick() => check() ? child.Tick() : Status.Failure;
}GDScript
class Cooldown extends BTNode:
var child: BTNode
var seconds: float
var ready_at := 0.0
func _init(c: BTNode, s: float): child = c; seconds = s
func tick() -> Status:
var now := Time.get_ticks_msec() / 1000.0
if now < ready_at: return Status.FAILURE # still cooling down
var status := child.tick()
if status != Status.RUNNING: ready_at = now + seconds
return status
class Timeout extends BTNode:
var child: BTNode
var seconds: float
var started_at := -1.0
func _init(c: BTNode, s: float): child = c; seconds = s
func tick() -> Status:
var now := Time.get_ticks_msec() / 1000.0
if started_at < 0.0: started_at = now
var status := child.tick()
if status != Status.RUNNING:
started_at = -1.0
return status
if now - started_at > seconds:
started_at = -1.0
return Status.FAILURE # took too long: give up
return Status.RUNNING
class Guard extends BTNode:
var child: BTNode
var check: Callable
func _init(c: BTNode, f: Callable): child = c; check = f
func tick() -> Status:
return child.tick() if check.call() else Status.FAILUREUnreal C++
class FCooldown : public FNode
{
public:
TSharedPtr<FNode> Child;
double Seconds = 10.0;
double ReadyAt = 0.0;
EStatus Tick() override
{
const double Now = FPlatformTime::Seconds();
if (Now < ReadyAt) return EStatus::Failure; // still cooling down
const EStatus Status = Child->Tick();
if (Status != EStatus::Running) ReadyAt = Now + Seconds;
return Status;
}
};
class FTimeout : public FNode
{
public:
TSharedPtr<FNode> Child;
double Seconds = 5.0;
double StartedAt = -1.0;
EStatus Tick() override
{
const double Now = FPlatformTime::Seconds();
if (StartedAt < 0.0) StartedAt = Now;
const EStatus Status = Child->Tick();
if (Status != EStatus::Running) { StartedAt = -1.0; return Status; }
if (Now - StartedAt > Seconds) { StartedAt = -1.0; return EStatus::Failure; }
return EStatus::Running;
}
};
class FGuard : public FNode
{
public:
TSharedPtr<FNode> Child;
TFunction<bool()> Check;
EStatus Tick() override { return Check() ? Child->Tick() : EStatus::Failure; }
};Unreal’s own behavior trees ship these policies as built-in decorators (Cooldown, Time Limit, Blackboard-based conditions), so in a UE project you often wire the generated tree to those instead of writing your own.
Rules of thumb
- One policy per decorator. Stack them rather than build a
RetryWithCooldownAndTimeout. Small pieces compose; big ones do not. - Reset on finish. Every sample above clears its counter or clock when the child stops running. A retry count that never resets makes the second door unopenable.
- Fail, do not hang. A policy that blocks an action should return Failure, so the parent can try something else in the same tick.
- Name the rule.
one_grenade_per_8stells a designer what the node does without opening it.