In this lesson, you will learn how to implement Parallel Nodes in your Behavior Tree to allow your AI to execute multiple sub-tasks simultaneously, such as scanning for threats while patrolling.
The Concept of Parallelism
Until now, your Behavior Tree nodes—like Sequences and Selectors—have functioned linearly. A Sequence runs one child after another, halting if any child fails. While efficient for simple tasks, this prevents an AI from doing two things at once. A Parallel Node solves this by executing all its children branches concurrently.
Think of it like a human worker: you can walk to the kitchen (a movement task) while simultaneously looking for your keys (a sensory task). If you only used a Sequence, the AI would have to finish walking completely before it even started looking for keys, making it slow to react to its environment.
Implementation Logic
A Parallel Node typically operates under a "policy" that determines when the node itself finishes. A common implementation is the Parallel Selector or Parallel Sequence. For your patrolling AI, you would place a Parallel node at the root of a branch with two children:
- Action Node: A MoveToWaypoint task.
- Action Node: A ScanForEnemy task.
Here is a simplified structural representation:
public class ParallelNode : Node {
public List<Node> children;
public override NodeState Evaluate() {
foreach (var child in children) {
// Tick every child every frame
child.Evaluate();
}
return NodeState.RUNNING;
}
}
In this setup, the MoveToWaypoint node remains in a RUNNING state while the agent traverses the path, while the ScanForEnemy node runs every frame, updating the Blackboard with target information. Because the Parallel node keeps both active, the AI doesn't wait for the patrol to finish to detect the player.
When designing these, ensure your tasks are "non-blocking." If an action node relies on a yield return or a long loop, it will stall the other parallel branches. Keep your logic granular so that each sub-task can tick quickly and return control to the tree.
Key Takeaway: Parallel nodes allow your AI to maintain situational awareness by executing reactive sensor tasks alongside movement or state-based objectives without waiting for the primary task to complete.