In this lesson, you will learn how to structure your AI’s decision-making flow using Selector and Sequence composite nodes to create intelligent, prioritized behavior branches.
In Unreal Engine Behavior Trees, Composites act as the branching logic that determines which tasks your AI executes. While Tasks represent the actual actions (like moving or playing an animation), Composites determine the "rules of the road" for how those tasks are chosen.
The Selector Composite
The Selector acts like an "OR" gate. It executes its child nodes from left to right and stops as soon as one child succeeds. Think of this as a priority list: "Try Task A; if that fails, try Task B; if that fails, try Task C."
For example, an AI might use a Selector to decide on a defensive stance:
- Child 1: Play "Dodge" (If the AI is currently under attack).
- Child 2: Play "Block" (If the AI has stamina).
- Child 3: Play "Idle" (The default fallback).
The Sequence Composite
The Sequence acts like an "AND" gate. It executes its children from left to right, but only continues to the next node if the current one succeeds. If a child node returns a "Fail" status, the entire sequence stops, and the parent composite is notified of the failure. This is ideal for multi-step operations that must complete in order.
A classic Sequence implementation looks like this:
- Step 1:
MoveTo(Target: Player) - Step 2:
PlayAnimation(Attack) - Step 3:
Wait(Cooldown)
If the MoveTo fails (perhaps the path is blocked), the PlayAnimation and Wait nodes will never trigger, preventing the AI from attacking thin air.
Mixing Them Together
By nesting these nodes, you build complex logic. You might place a Sequence under a Selector. The Selector ensures that if the "Attack" sequence fails (maybe the player went out of range), the AI immediately falls back to the "Patrol" sequence. This hierarchical structure allows your AI to react dynamically to the game state tracked in your Blackboard.
Key Takeaway: Use Selectors to define priority-based choices (try this, if not, try that) and Sequences to enforce strict procedural steps (do this, then that, then finally this).