【Unreal Engine】Implementing Enemy AI with Behavior Tree: Blackboard and Task/Decorator Basics

Created: 2025-12-12

Basic usage of Behavior Tree for managing enemy AI decision-making. Covers data sharing with Blackboard, roles of Selector/Sequence/Decorator/Task nodes, and implementing typical behavior patterns like searching, chasing, and attacking.

Behavior Tree Overview

When developing games with Unreal Engine, are you facing challenges like "enemy AI movement is monotonous and uninteresting" or "trying to make complex situation judgments turns Blueprint into spaghetti code"?

Especially when implementing AI that switches between multiple behaviors based on situations—player detection, chasing, attacking, and patrolling when losing sight—traditional State Machines tend to make management very cumbersome.

This article thoroughly explains Unreal Engine's powerful AI tool Behavior Tree—which allows visual and hierarchical AI decision-making design—from basic concepts to concrete implementation steps even beginners can immediately put into practice, with abundant Blueprint examples.

What is a Behavior Tree

A Behavior Tree is a hierarchical decision-making structure for determining "what the AI should do next." Starting from the tree's root, it traverses nodes and executes AI behaviors from top to bottom, or conditionally.

Main Components

Understanding Behavior Trees requires these three essential elements:

Element NameRoleDetails
Behavior Tree (BT)Decision-making structureTree structure defining the AI's entire behavior logic.
Blackboard (BB)AI memory/dataPlace storing data the AI shares (target position, HP, current state, etc.). BT nodes reference and update BB information to determine actions.
AI ControllerAI controllerController that owns BT and BB, operating the Pawn (character).

Node Types

Behavior Trees are mainly composed of four types of nodes:

  1. Composite Nodes: Control child node execution order.
    • Sequence: Executes child nodes left to right, returning success if all succeed. Returns failure immediately if any fails.
    • Selector: Executes child nodes left to right, returning success if any one succeeds. Returns failure only if all fail.
  2. Task Nodes: Nodes that make AI execute specific actions (movement, attack, waiting, etc.). Usually created with custom Blueprint or C++.
  3. Decorator Nodes: Condition nodes that determine whether child nodes can execute. Check Blackboard values or AI state.
  4. Service Nodes: Execute at regular intervals to update Blackboard values or monitor surroundings.

Enemy AI Implementation Example

Here we'll explain the steps to implement the most common enemy AI behaviors—"player detection (patrolling)," "chasing after detection," and "attacking at certain distance"—with Behavior Tree.

AI Controller and Blackboard Setup

First, perform basic setup for controlling the AI.

  1. Create AI Controller: Create a Blueprint inheriting from AIController class and set it as the enemy Pawn's default AI Controller.
  2. Create Blackboard: Right-click in Content Browser, select Artificial Intelligence -> Blackboard to create a Blackboard asset.
  3. Add Blackboard Keys: Open the created Blackboard and add the following Keys:
    • TargetActor (Object type): The target the AI chases/attacks, like the player.
    • IsTargetDetected (Boolean type): Whether a target was detected.
    • PatrolLocation (Vector type): Patrol destination point.
  4. Create Behavior Tree: Select Artificial Intelligence -> Behavior Tree to create a BT asset. Open the BT asset and set the Blackboard asset in the Blackboard Asset field in the Details panel. In the AI Controller, just specifying this BT asset with the Run Behavior Tree node automatically associates the Blackboard.

💡 Blackboard Configuration Location

It's common to set the Blackboard on the BT asset side. On the AI Controller side, you only specify the BT asset—no need to separately specify the Blackboard.

Behavior Tree Design

AI decision-making is structured with this logic:

AI Logic:

  1. Selector: Has the target been detected?
  2. YES case (Sequence): Chase and attack the target.
  3. NO case (Sequence): Patrol.

Implementing Detection (Service Node)

Implement logic to periodically check if the AI detected the player using a Service node.

  1. Create Service Node: Create a Blueprint inheriting from BTService_BlueprintBase (e.g., BTS_CheckForTarget).
  2. Implement Logic: In the Receive Tick AI event, use Sphere Trace or Get Actors in Range to search for the player.
  3. Update Blackboard: If player is found, set the player to Blackboard's TargetActor and set IsTargetDetected to True. Set to False if lost.
  4. Place in BT: Attach this Service node to the Selector node directly under the Behavior Tree root.

💡 More Efficient Detection: AIPerception Component

Periodic detection in Service nodes (Tick-based) is simple, but AIPerceptionComponent is recommended from a performance perspective. AIPerception is event-driven, allowing Blackboard updates only when targets are detected using the OnTargetPerceptionUpdated event. This method is more efficient for large-scale games with numerous AIs.

Implementing Chasing and Attacking (Task Nodes)

  1. Chase Task: Use the standard Move To task node. Specify the Blackboard-created TargetActor in this node's Blackboard Key.
  2. Attack Task: Create a Blueprint inheriting from BTTask_BlueprintBase (e.g., BTT_AttackTarget).
    • In the Receive Execute AI event, play attack animation and handle damage, then return Success with the Finish Execute node.

💡 Best Practice: Separate Condition Checking with Decorators

The attack Task above could check "whether within attack range" inside the Task, but separating distance checking with Decorator nodes is recommended. For example, using BTDecorator_IsAtLocation or custom Decorators to make the attack Task execute only when within attack range. This lets Tasks focus purely on the "attack" action, clearly separating conditions from actions.

Implementing Patrolling (Task Node)

  1. Patrol Point Setting Task: Create a custom Task (e.g., BTT_FindPatrolLocation) to calculate a random patrol point and set it to Blackboard's PatrolLocation Key.
  2. Patrol Movement Task: Use the standard Move To task node and specify PatrolLocation for Blackboard Key.

Best Practices

Behavior Trees are powerful, but poor design quickly leads to complexity. Design with these best practices in mind.

Prioritize Blackboard Design

The true power of Behavior Trees lies in data-driven decision-making through Blackboards.

  • Centralized Data Management: Store all AI state (attacking, patrolling, etc.) and important information (target, HP, ammo count) in Blackboard, avoiding direct data exchange between nodes.
  • Key Naming Conventions: Establish clear naming conventions for Keys (e.g., Target_Player, State_Patrolling) to make it clear which nodes reference/update which Keys.

Clarify Node Roles

  • Task Nodes for "Actions" Only: Within Task nodes, execute only concrete actions like movement or attack, leaving condition checking to Decorators.
  • Decorator Nodes for "Conditions" Only: Decorators should be pure condition checking of Blackboard values, without side effects like changing Blackboard values.

Common Mistakes

Common MistakeSolution (Best Practice)
Infinite Loops: Task node keeps returning Failure, and Selector node immediately moves to next child node.Use Wait or Delay appropriately within Task nodes to adjust execution frequency. Or clarify conditions for Finish Execute to return Success.
Too-Frequent Services: Service node execution interval is too short, degrading performance.Even for processes requiring frequent checks like detection, set intervals of at least 0.2-0.5 seconds. Also, control with Decorators to execute Services only when needed.
Blackboard Key Misuse: Using the same Key for data with different meanings.Clearly separate Keys by purpose; especially for Vector and Object types, distinguish like TargetLocation vs PatrolLocation.

Key Points for Behavior Tree Usage

Behavior Tree is the most powerful and recommended method for implementing complex enemy AI in Unreal Engine.

  • Hierarchical Design: Complex decision-making can be hierarchically organized with Sequences and Selectors.
  • Data-Driven: Managing data centrally through Blackboard reduces inter-node dependencies and increases reusability.
  • Visual Debugging: AI execution paths can be confirmed in real-time in the editor, making debugging easy.

Use the basic concepts and implementation steps introduced in this article to design intelligent, challenging enemy AI that adds depth to your game.

💡 Advanced AI: EQS (Environment Query System)

Behavior Trees provide decision-making structure, but EQS (Environment Query System) is effective for environment-based choices like "where to move" or "which target to choose." EQS scans the surrounding environment and scores to select optimal positions or targets. Combining BT with EQS enables smarter, more situationally adaptive AI.