Gamedev Hub

Behavior Trees

Finite State Machines (FSMs) are great for simple logic (Mario), but for complex AI (Halo, Uncharted), they become a “spaghetti” mess of transitions.

Behavior Trees (BTs) solve this by structuring AI as a hierarchy of tasks. They are modular, reusable, and easy to debug.

1. The Core Concept

A Behavior Tree is executed (Ticked) from the Root down. Every node returns one of three statuses:

  1. Success: “I completed the task.”
  2. Failure: “I cannot complete the task.”
  3. Running: “I am still working on it. Check back next frame.”

2. The Nodes

Composites (Control Flow)

Decorators (Wrappers)

Leaves (Actions & Conditions)

3. Implementation (C# Example)

public enum Status { Success, Failure, Running }

public abstract class Node {
    public abstract Status Tick();
}

public class Selector : Node {
    List<Node> children;
    public override Status Tick() {
        foreach (var child in children) {
            Status s = child.Tick();
            if (s != Status.Failure) return s; // Success or Running
        }
        return Status.Failure;
    }
}

4. Why use BTs?