Skip to content
Tutorials Center
My learning

Topics

Lesson 31 of 38 ยท Animation and AI

Behavior Trees and Blackboards

Build enemy AI with Unreal Engine behavior trees. AI Controllers, blackboard keys, selector and sequence composites, tasks, decorators and services, plus a runnable tree model.

An enemy that stands still until you hit it is not much of an enemy. Unreal Engine's standard tool for decision-making is the Behavior Tree (BT): a tree of tasks and conditions evaluated from the top, with shared memory in a Blackboard. Behavior trees are easier to read and debug than a pile of Blueprint branches, and designers can edit them without touching code.

The Unreal Editor is not available on this site, so the workflow is described in words. The Python stand-in implements the core of a behavior tree so you can run and modify one.

The parts

  • AI Controller: a controller (not a pawn) that possesses an AI pawn. It runs the behavior tree. Set your Character's AI Controller Class and Auto Possess AI to Placed in World or Spawned.
  • Blackboard asset: a table of typed keys such as TargetActor (Object), PatrolPoint (Vector), HasLineOfSight (Bool), Health (Float).
  • Behavior Tree asset: the tree itself, with a Blackboard assigned at the top.

Start it from the AI Controller, for example in OnPossess:

C++
void AEnemyController::OnPossess(APawn* InPawn)
{
    Super::OnPossess(InPawn);
    if (BehaviorTreeAsset)
    {
        RunBehaviorTree(BehaviorTreeAsset);
    }
}

(BehaviorTreeAsset is a UPROPERTY(EditAnywhere) TObjectPtr<UBehaviorTree>.) Create the assets via right-click > Artificial Intelligence > Behavior Tree / Blackboard.

Node types

Composite nodes control the flow of children, evaluated left to right:

  • Selector: tries children in order until one succeeds, then stops. Think "try this, else that". Fails only if all children fail.
  • Sequence: runs children in order until one fails, then stops. Think "do this, then that". Succeeds only if all succeed.
  • Simple Parallel: runs a main task together with a background tree.

Task nodes are leaves that do something and return Succeeded, Failed or In Progress: Move To, Wait, Rotate to face BB entry, Play Animation, Run EQS Query, or a custom task (Blueprint or UBTTaskNode).

Decorators attach to a node as conditions: Blackboard (key is set / equals), Cooldown, Loop, Time Limit, Does Path Exist, Compare Distance. With Observer Aborts (Self, Lower Priority, Both) a decorator can interrupt running branches when the condition changes, which is how an enemy stops patrolling the moment it sees you.

Services attach to a composite and tick at an interval, such as "update HasLineOfSight" or "find closest target" every 0.3 seconds.

Behavior tree nodes

Selector First success wins
Sequence All must succeed
Decorator Condition on a node
Service Updates on an interval

Evaluated from the root, left to right

1 / 2

A typical enemy

Text
Root
โ””โ”€ Selector
   โ”œโ”€ Sequence  [Decorator: HasTarget is set]
   โ”‚   โ”œโ”€ Move To (TargetActor, acceptable radius 150)
   โ”‚   โ””โ”€ Attack
   โ”œโ”€ Sequence  [Decorator: Health < 25]
   โ”‚   โ””โ”€ Flee
   โ””โ”€ Sequence
       โ”œโ”€ Pick Random Patrol Point
       โ”œโ”€ Move To (PatrolPoint)
       โ””โ”€ Wait 2s

Read top to bottom: if there is a target, chase and attack it; otherwise, if hurt, flee; otherwise, patrol. The leftmost branch has the highest priority.

A behavior tree in Python

The stand-in runs the tree each "tick" against a blackboard dictionary. Tasks are instantaneous here (real tasks can take many frames and return In Progress).

Python Stand-in: selector and sequence

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Change the blackboard values and re-run to see the priority order in action.

Observer aborts and services

In a real tree, the Move To task can take seconds. A service keeps TargetActor current, and a decorator on the combat branch with Observer Aborts: Both drops the patrol branch as soon as TargetActor is set. The stand-in below models that with a tick loop.

Python Stand-in: service updates the blackboard, tree re-evaluates

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Behavior trees vs. state machines

State machines (like the animation ones in Animation Blueprints and state machines) store the current state and need explicit transitions between every pair of states. A behavior tree re-evaluates priorities from the root, so adding a new behavior is usually a new branch rather than new edges everywhere. For very simple AI, a State Tree (a newer Unreal feature combining both ideas) or a plain Blueprint can be enough; behavior trees shine when behavior has many priorities.

Debugging

While playing, press the apostrophe key (') to open the Gameplay Debugger: it shows the active tree nodes, Blackboard values and perception info for the selected AI. In the Behavior Tree editor, pick the running AI in the Debug dropdown to see highlighted executing nodes.

Common mistakes

  • Forgetting to run the tree. Without RunBehaviorTree, the AI does nothing.
  • Blackboard key type mismatch. A vector key cannot hold an Actor.
  • No decorator aborts. The AI finishes its patrol before reacting to you.
  • Heavy work in service ticks. Keep intervals around 0.2 to 0.5 seconds.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A Selector node succeeds when...

    A Selector tries children left to right until one succeeds.
  2. 2. What does a Sequence node do when its second child fails?

    A Sequence stops at the first failure and fails.
  3. 3. Which object runs a Behavior Tree?

    An AI Controller possesses the pawn and calls RunBehaviorTree.
  4. 4. Why use Observer Aborts on a decorator?

    Aborts let a higher-priority branch take over immediately instead of waiting for a task to finish.

Summary

  • An AI Controller runs a Behavior Tree that reads and writes a Blackboard.
  • Selectors choose the first success, Sequences require all successes; the leftmost child has priority.
  • Decorators are conditions, services update data on an interval, tasks do the work.
  • Use Observer Aborts for reactivity and the Gameplay Debugger to inspect the running tree.

Next: AI Perception and Navigation