Selector

Execute child nodes sequentially until one succeeds. Like a logical OR operation.

Introduction

A Selector tries its children in order. As soon as a child returns Success, the Selector also succeeds and stops trying other children. If all children fail, the Selector fails.

Think of it like the logical OR operator (||) in code:

Selector("Choose") → A() || B() || C()

Returns success as soon as one child succeeds.

Usage

Selector(() =>
{
    // Try to attack first
    D.Condition(() => EnemyVisible);
    AttackEnemy();

    // If attack fails, try to chase
    D.Condition(() => EnemyTooFar());
    ChaseEnemy();

    // If chase fails, try to patrol
    Patrol();

    // If all fail, the selector fails
});
Selector(() =>
{
    // Try to attack first
    D.Condition(() => EnemyVisible);
    AttackEnemy();

    // If attack fails, try to chase
    D.Condition(() => EnemyTooFar());
    ChaseEnemy();

    // If chase fails, try to patrol
    Patrol();

    // If all fail, the selector fails
});

Children are executed in the order they appear. The first child to succeed ends the selector.

Next: Reactive Pattern

Build reactive behavior trees that respond to changes.

Read about Reactive Pattern →