Intermediate

Behavior Trees

Behavior trees provide a modular, scalable way to design complex AI behaviors by composing simple nodes into tree structures.

What is a Behavior Tree?

A behavior tree (BT) is a tree-structured model for planning and decision-making. Each node in the tree represents a behavior or decision, and the tree is traversed from root to leaves every tick (frame). Nodes return one of three statuses: Success, Failure, or Running.

Behavior trees were popularized by Halo 2 and have become the industry standard for game AI decision-making because they are modular, reusable, and easy to debug visually.

Node Types

Node TypeCategoryDescription
SequenceCompositeRuns children left-to-right, fails if any child fails (AND logic)
SelectorCompositeRuns children left-to-right, succeeds on first success (OR logic)
InverterDecoratorInverts the result of its child (NOT logic)
RepeaterDecoratorRepeats its child a set number of times or until failure
ActionLeafPerforms an action (move, attack, play animation)
ConditionLeafChecks a condition (is enemy visible? health low?)
Python - Simple Behavior Tree
class Node:
    def tick(self):
        raise NotImplementedError

class Sequence(Node):
    def __init__(self, children):
        self.children = children

    def tick(self):
        for child in self.children:
            result = child.tick()
            if result != "SUCCESS":
                return result
        return "SUCCESS"

class Selector(Node):
    def __init__(self, children):
        self.children = children

    def tick(self):
        for child in self.children:
            result = child.tick()
            if result != "FAILURE":
                return result
        return "FAILURE"

# Example: Enemy AI
# Selector: Try attack, else patrol
#   Sequence: See player -> Move to player -> Attack
#   Sequence: Has patrol point -> Move to point
enemy_bt = Selector([
    Sequence([CanSeePlayer(), MoveToPlayer(), Attack()]),
    Sequence([HasPatrolPoint(), MoveToPatrolPoint()])
])

Advanced BT Concepts

Blackboard Pattern

A shared data store (blackboard) allows nodes to communicate without tight coupling. Nodes read and write to the blackboard instead of passing data directly between themselves.

Parallel Nodes

Run multiple children simultaneously. Useful for behaviors that happen concurrently, like walking while talking or shooting while taking cover.

Utility-Based Selection

Instead of fixed priority ordering, score each option based on context (distance to enemy, health level, ammo count) and select the highest-scoring behavior. This creates more dynamic, responsive AI.

BT vs Other Approaches

FeatureBehavior TreesFSMsGOAP
ModularityHigh (subtrees reusable)Low (states tightly coupled)High (actions independent)
ScalabilityScales wellSpaghetti at scaleScales well
DebuggingVisual tree inspectionState diagramPlan inspection
ReactivityGood (re-evaluated each tick)Event-drivenReplanning needed
Key takeaway: Behavior trees are the industry standard for game AI decision-making. Their modular, hierarchical structure makes it easy to build complex behaviors from simple, reusable components.

Ready to Go Deeper?

Live instructor-led courses from our partners. Affiliate disclosure.