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 Type | Category | Description |
|---|---|---|
| Sequence | Composite | Runs children left-to-right, fails if any child fails (AND logic) |
| Selector | Composite | Runs children left-to-right, succeeds on first success (OR logic) |
| Inverter | Decorator | Inverts the result of its child (NOT logic) |
| Repeater | Decorator | Repeats its child a set number of times or until failure |
| Action | Leaf | Performs an action (move, attack, play animation) |
| Condition | Leaf | Checks a condition (is enemy visible? health low?) |
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
| Feature | Behavior Trees | FSMs | GOAP |
|---|---|---|---|
| Modularity | High (subtrees reusable) | Low (states tightly coupled) | High (actions independent) |
| Scalability | Scales well | Spaghetti at scale | Scales well |
| Debugging | Visual tree inspection | State diagram | Plan inspection |
| Reactivity | Good (re-evaluated each tick) | Event-driven | Replanning needed |
Ready to Go Deeper?
Live instructor-led courses from our partners. Affiliate disclosure.
AI & ML Courses - 30% Off
Live instructor-led AI, machine learning, data science, and cloud courses for working professionals. Use code Limited30 at checkout.
EdurekaDataCamp - AI & Data Science
Hands-on Python, machine learning, and AI courses with interactive exercises and real projects.
DataCampedX - Top AI Courses
University-level AI courses from MIT, Harvard, Stanford. Earn certificates that employers recognize.
edX