Concurrency
Doing two things at once with a parallel node: how its success and failure policies decide the result, what happens to the children still running, and how to keep a condition watching while an action works.
A soldier is running to cover while firing to keep the enemy’s heads down. Neither job waits for the other: the legs and the rifle work in the same moments. A behavior tree says “at the same time” with a parallel node, which the building blocks page introduced: a composite that ticks all of its children on every tick, instead of one after another.
“At the same time” in a behavior tree does not mean threads. On one tick the parallel ticks its first child, then its second, then its third, on the same thread, and each returns a status (Success, Failure or Running, the status of a node whose work takes more than one tick). What makes it concurrent is that the parallel does not stop at a running child: every child gets its tick, every tick, so several actions are under way together. That leaves three questions, and this page takes them in turn:
- When several children are under way, which results make the parallel succeed or fail? That is its policy.
- When the parallel has its answer while some children are still running, what happens to them? They are halted.
- A child that has finished while its siblings run on: is it ticked again? That is the difference between a plain parallel and one that remembers.
Policies decide the result
A parallel’s success policy says how many children must succeed for it to succeed, and its failure policy says how many failures make it fail. The building blocks page wrote this as a threshold M out of N children. Two values cover most trees:
| Policy | Success policy means | Failure policy means |
|---|---|---|
| require all | succeed when every child has succeeded | fail when every child has failed |
| require one | succeed as soon as one child succeeds | fail as soon as one child fails |
The usual pair is “succeed when all succeed, fail when one fails”: the soldier is done when he is in cover and the burst is fired, and one failure (out of ammunition, the path blocked) ends the whole move. “Succeed when one succeeds” is a race: play the reload animation and wait for the reload timer, and continue with whichever finishes first. With policies, a parallel can also end with every child finished and neither policy met (two of three succeeded under “require all” success and “require all” failure); a runtime has to pick an answer for that case, and most say Failure.
BeeTreeCraft’s tree format writes the two policies as success_policy and failure_policy on a parallel node, with the values require_all and require_one. Today only the generated Godot runtime acts on them. beetree simulate, the web builder and the generated Unity runtime run every parallel with the usual pair, whatever the fields say, and the Unreal output turns a parallel into Unreal’s own Simple Parallel (see the end of this page), so the tree below leaves the fields out. Making every surface honour them is queued work; until it ships, read the policy table above as the meaning, and the usual pair as what you will see.
Halting the children that are still running
When the parallel has its answer, some children may still be running. The soldier runs out of ammunition while still running to cover: the parallel fails, and its parent moves on to something else. The run to cover must not carry on by itself. A runtime halts a running child it stops ticking: it tells the child to stop, so the child can cancel its path, stop its animation and free what it reserved. A parallel halts its running children whenever it finishes, either way, and when its own parent halts it.
Halting is the part of concurrency most often forgotten in a hand-written runtime, and the bugs it leaves are the strange ones: a character who keeps walking toward cover after it has turned to flee, or a sound that never stops.
Watching while working
The most common use of a parallel in a game tree has nothing to do with two actions. It is a monitor: a condition placed beside a long action, so that the condition is checked on every tick while the action runs. In the tree below, has_ammo sits beside the move and the burst. The tick it returns Failure, the parallel fails, the move and the burst are halted, and the soldier falls back. A sequence could not do this, because a sequence ticks the condition before the action and then waits on the action. A reactive sequence (see Reactive versus memory nodes) can, by checking the condition again in front of the action; the parallel does it for several actions at once.
A tree you can run
version: "1.0"
metadata:
name: advance-under-fire
description: "Runs to cover while laying down suppressing fire, and falls back the moment the ammunition runs out"
tags: [concurrency, parallel]
tree:
type: selector
name: soldier_root
children:
- type: parallel
name: advance_under_fire
children:
- {type: action, name: move_to_cover, node: MoveToCover}
- {type: action, name: suppress_enemy, node: SuppressEnemy}
- {type: condition, name: has_ammo, node: HasAmmo}
- {type: action, name: fall_back, node: FallBack}
beetree simulate runs one tick, and --override sets what a leaf returns on it. Play three moments of the advance, one command each:
beetree simulate advance-under-fire.beetree.yaml --override move_to_cover=RUNNING --override suppress_enemy=RUNNING
beetree simulate advance-under-fire.beetree.yaml --override move_to_cover=SUCCESS --override suppress_enemy=RUNNING
beetree simulate advance-under-fire.beetree.yaml --override move_to_cover=RUNNING --override suppress_enemy=RUNNING --override has_ammo=FAILURE
- Both actions are under way and there is ammunition: every child is ticked, two are running, and the parallel and the tree are Running.
fall_backis not ticked. - The soldier has reached cover but the burst is still firing: under “succeed when all succeed” the parallel waits, Running. Under “succeed when one succeeds” it would have finished here.
- The ammunition runs out mid-move:
has_ammofails, so the parallel fails although two of its children are still running. Those two are halted, and the selector moves on tofall_back, which succeeds.
What the simulator shows today
The trace of the third command shows the parallel failing while two of its children report Running, and then fall_back. It does not show the halt: the simulator has no step that tells a running node to stop, so on this page the halt is something you read from the trace, not something it prints. beetree simulate runs one tick, so the second moment cannot follow the first in a single run; play the moments with one command each, as above, or paste the tree into the web builder and use Step through, which pauses at each leaf so you choose its status. A run of many ticks with a status per tick, and the policies, are on their way and not there yet.
Engine code
The parallel on the building blocks page ticks every child on every tick, including a child that already finished. For conditions that is right, since a monitor must be checked again. For an action with an effect it is wrong: once move_to_cover has succeeded, ticking it again would start a new move. The parallel below remembers each child’s result once it has finished and does not tick that child again, until the parallel itself finishes and starts over. It takes both thresholds, so “require all” and “require one” are its two extremes. Each sample uses the Status type and tick() contract from Building blocks.
Agnostic
ResumingParallel(children, successThreshold, failureThreshold):
results = [Running] * N // Running: not finished yet, tick it
tick():
successes = 0
failures = 0
for i in 0 .. N - 1:
if results[i] == Running:
results[i] = children[i].tick() // a finished child keeps its result
if results[i] == Success: successes += 1
if results[i] == Failure: failures += 1
if successes >= successThreshold: return finish(Success)
if failures >= failureThreshold: return finish(Failure)
if successes + failures == N: return finish(Failure) // all done, no policy met
return Running
finish(status):
results = [Running] * N // halt the running children here, then start over
return statusUnity C#
public class ResumingParallel : Node
{
readonly Node[] children;
readonly Status[] results; // Running: not finished yet, tick it
readonly int successThreshold, failureThreshold;
public ResumingParallel(int successThreshold, int failureThreshold, params Node[] children)
{
this.children = children;
this.successThreshold = successThreshold;
this.failureThreshold = failureThreshold;
results = new Status[children.Length];
System.Array.Fill(results, Status.Running);
}
public override Status Tick()
{
int successes = 0, failures = 0;
for (int i = 0; i < children.Length; i++)
{
if (results[i] == Status.Running)
results[i] = children[i].Tick(); // a finished child keeps its result
if (results[i] == Status.Success) successes++;
if (results[i] == Status.Failure) failures++;
}
if (successes >= successThreshold) return Finish(Status.Success);
if (failures >= failureThreshold) return Finish(Status.Failure);
if (successes + failures == children.Length) return Finish(Status.Failure); // all done, no policy met
return Status.Running;
}
Status Finish(Status status)
{
System.Array.Fill(results, Status.Running); // halt the running children here, then start over
return status;
}
}GDScript
class ResumingParallel extends BTNode:
var children: Array[BTNode]
var results: Array[Status] = [] # RUNNING: not finished yet, tick it
var success_threshold: int
var failure_threshold: int
func _init(s: int, f: int, c: Array[BTNode]):
success_threshold = s
failure_threshold = f
children = c
results.resize(c.size())
results.fill(Status.RUNNING)
func tick() -> Status:
var successes := 0
var failures := 0
for i in children.size():
if results[i] == Status.RUNNING:
results[i] = children[i].tick() # a finished child keeps its result
if results[i] == Status.SUCCESS:
successes += 1
elif results[i] == Status.FAILURE:
failures += 1
if successes >= success_threshold:
return finish(Status.SUCCESS)
if failures >= failure_threshold:
return finish(Status.FAILURE)
if successes + failures == children.size():
return finish(Status.FAILURE) # all done, no policy met
return Status.RUNNING
func finish(status: Status) -> Status:
results.fill(Status.RUNNING) # halt the running children here, then start over
return statusUnreal C++
class FResumingParallel : public FNode
{
public:
TArray<TSharedPtr<FNode>> Children;
TArray<EStatus> Results; // Running: not finished yet, tick it
int32 SuccessThreshold = 1;
int32 FailureThreshold = 1;
EStatus Tick() override
{
while (Results.Num() < Children.Num()) Results.Add(EStatus::Running);
int32 Successes = 0, Failures = 0;
for (int32 i = 0; i < Children.Num(); ++i)
{
if (Results[i] == EStatus::Running)
Results[i] = Children[i]->Tick(); // a finished child keeps its result
if (Results[i] == EStatus::Success) ++Successes;
if (Results[i] == EStatus::Failure) ++Failures;
}
if (Successes >= SuccessThreshold) return Finish(EStatus::Success);
if (Failures >= FailureThreshold) return Finish(EStatus::Failure);
if (Successes + Failures == Children.Num()) return Finish(EStatus::Failure); // all done, no policy met
return EStatus::Running;
}
private:
EStatus Finish(EStatus Status)
{
for (EStatus& Result : Results) Result = EStatus::Running; // halt the running children here, then start over
return Status;
}
};Remembering suits actions, not monitors: once has_ammo had succeeded, a ResumingParallel would never check it again. So the soldier is two parallels. The move and the burst go in a ResumingParallel with a success threshold of 2 (both) and a failure threshold of 1 (either), and that sits beside has_ammo in the plain Parallel from Building blocks with M = 2, which checks the condition on every tick and fails the moment it does. A full runtime fills in the comment in finish: each child that is still running is told to stop. In Unreal’s own behavior trees, the closest built-in is the Simple Parallel composite, which runs one main task beside a background subtree and, in its Immediate finish mode, aborts the background when the main task finishes.
Where it comes from
Colledanchise and Ögren define the parallel node with a success threshold M of N children, and discuss when its children may disagree, in Behavior Trees in Robotics and AI (2018). Colledanchise and Natale compare how libraries implement parallel nodes, halting and memory in On the Implementation of Behavior Trees in Robotics (2021). Epic’s Behavior Tree in Unreal Engine: Overview describes the Simple Parallel composite. More reading is on the Resources page.
Next, see how the format writes a parallel in the tree format’s node types, or open the Build hub and put a monitor condition beside a long action of your own.