Unreal Behavior Trees
Author NPC decision-making in UE5 with Behavior Trees driven by a Blackboard: structure the tree with composites, gate branches with decorators, keep state current with services, and run it from an AIController. Targets UE 5.8.
When to use
- Use when building enemy/NPC AI: creating a
BT_/BB_asset pair, structuring - Use when the project has Behavior Tree (
BT_) and Blackboard (BB_) assets and an
Selector/Sequence branches, adding decorators (conditions) and services (periodic updates), writing custom BTTask/BTService nodes, or wiring an AIController to run the tree.
AAIController.
**When *not* to use:** the *concept* of AI (FSM vs BT vs steering, cross-engine) → game-ai. Pure navigation/pathing math is engine navmesh (BT's MoveTo uses it). Simple one-off logic may be cheaper as a small state machine than a full tree.
Core workflow
- Create the pair: a Blackboard (
BB_) holds typed keys (the AI's memory:TargetActor, - Possess and run. An
AAIControllerpossesses the pawn and callsRunBehaviorTree(BT), - Structure with composites. Selector runs children left→right until one *succeeds*
- Gate branches with Decorators that read Blackboard keys (e.g. "Has Target?" guards the
- Keep the Blackboard current with Services attached to a branch — they tick periodically
- Do work in Tasks, which return
Succeeded,Failed, orInProgress(latent tasks like - Verify with the Behavior Tree debugger during PIE — it highlights the running node and
LastKnownLocation, bIsInvestigating); a Behavior Tree (BT_) references that Blackboard.
which also initializes the referenced Blackboard.
(priority/fallback: "attack, else chase, else patrol"). Sequence runs children until one *fails* (do-all: "move to cover → reload → peek"). Simple Parallel runs one main task alongside a secondary.
combat branch). Set Observer Aborts so the tree re-evaluates when the key changes.
(e.g. update TargetActor via a sight check) only while that branch is active.
MoveTo finish later).
shows live Blackboard values, so you see exactly which branch executes.
Patterns
1. AIController that runs the tree (C++)
void AEnemyAIController::OnPossess(APawn* InPawn)
{
Super::OnPossess(InPawn);
if (BehaviorTree) // UPROPERTY(EditAnywhere) TObjectPtr<UBehaviorTree>
RunBehaviorTree(BehaviorTree); // initializes & uses the Blackboard the BT references
}
2. A priority tree (node structure)
ROOT
└── Selector (try combat, else investigate, else patrol)
├── Sequence [Decorator: Blackboard 'TargetActor' Is Set, Observer Aborts: Both]
│ ├── Task: MoveTo (TargetActor) // latent: returns InProgress then Succeeded
│ └── Task: Attack
├── Sequence [Decorator: 'LastKnownLocation' Is Set]
│ ├── Task: MoveTo (LastKnownLocation)
│ └── Task: Wait (3s) + clear key
└── Task: Patrol (BTTask_FindPatrolPoint -> MoveTo)
Observer Aborts: Both makes the combat branch interrupt patrol the instant TargetActor is set, and bail out when it's cleared — this is what makes the AI feel reactive.
3. Updating the Blackboard from code (e.g. on seeing the player)
void AEnemyAIController::SetTarget(AActor* Target)
{
if (UBlackboardComponent* BB = GetBlackboardComponent())
BB->SetValueAsObject(TEXT("TargetActor"), Target); // key name must match the BB asset
}
// Clear with BB->ClearValue(TEXT("TargetActor")); to drop back to a lower-priority branch.
Pitfalls
- AI never starts — the pawn isn't possessed (set the Pawn's *Auto Possess AI* to "Placed
MoveToinstantly fails — no NavMesh in the level (add a Nav Mesh Bounds Volume), or the- Branch doesn't react to changes — the gating Decorator's Observer Aborts is set to
- A task hangs the tree — a custom task returned
InProgressand never calls - Blackboard key typos —
SetValueAsObject("Taget", ...)silently does nothing; match the - Sequence vs Selector confusion — Sequence = AND (stops on first failure); Selector = OR
in World or Spawned" and assign the AIController), or RunBehaviorTree was never called.
target is off the navmesh.
None; set it to Self/Lower Priority/Both so the tree re-evaluates when the key changes.
FinishLatentTask. Always complete latent tasks.
key name and type exactly, or use a cached FBlackboardKeySelector.
(stops on first success). Swapping them inverts the behaviour.
References
- For a custom C++
UBTTaskNode(instant and latentExecuteTaskreturningEBTNodeResult, - Primary docs: "Behavior Trees in Unreal Engine"
with a FBlackboardKeySelector), read references/custom-bttask.md.
(https://dev.epicgames.com/documentation/en-us/unreal-engine/behavior-trees-in-unreal-engine).
Related skills
game-ai— engine-agnostic AI design (FSM, BT, steering, pathfinding choices).unreal-cpp-gameplay— the AIController and pawn classes in C++.fps-shooter/tower-defense— genres that compose enemy AI.