Blueprint Behavior Tree Tutorial

This tutorial serves as a basic introduction for how to create a working AI character that uses a Behavior Tree to execute Blueprint Tasks.

Updated about 3 years ago Edit Page Revisions

Overview

This tutorial serves as a basic introduction for how to create a working AI character that uses a Behavior Tree to execute Blueprint Tasks.

Scene Setup

1. Launch Latest version of Unreal Engine

2. Create a new Project. Select ÔÇ£Top DownÔÇØ

3. Name project something like ÔÇ£MyBehaviorTreeÔÇØ and press ÔÇ£Create ProjectÔÇØ Button.

4. Next add a NavMeshBoundsVolume to the scene and scale it to encompass the playspace. This volume is responsible to building navmesh that the AI will use to navigate.

File:Tut Rev 01.png

Note: P toggles path visibility

File:Tut Rev 02.png

Creating the AIController

Create a new Blueprint that uses the AIController class as its parent and name it BasicAIController

File:Tut 01.png

Creating the AICharacter

Create a new Blueprint that uses the Character class as its parent and name it BasicAICharacter

File:BBT CreateAICharacter.JPG

Open the newly created Character Blueprint and set its default AIController Class to the one your created in the previous step, (BasicAIController)

File:BBT SettingAIController.jpg

Finally add a mesh component to the Character Blueprint so we can see the character (*Note: Creating the AI Character automatically adds skeletal mesh under components, however, you can still add a static mesh component and ignore the skeletal mesh icon -but you cannot remove the skeletal mesh from the component list.)

File:Tut Rev 03.png

Creating the BlackBoard Data Asset

Create a new BlackBoard Data Asset. This is done by right clicking inside the content browser then selecting Miscellaneous and then -> BlackBoardD (Name this asset BasicAIBlackboard )

File:Tut 03.png

The BlackBoard asset allows you to store information in keys that can then be used by the Behavior Tree. Create a key named TargetPoint and set its Key Type to Vector.

File:Tut Rev 04.png

Creating the Behavior Tree

  • In versions of UE4 before 4.5 you must first enable it in Edit --> Editor Preferences --> Experimental

File:BBT CreateBehaviorTree 02.jpg

In UE4.5, however, you no longer need to perform the previous step and can easily create a new Behavior Tree from the Content Browser as shown:

File:Tut 05.png

  • Name this asset BasicBehaviorTree.

At this stage you should have the following 4 assets created.

BasicAiBlackBoard, BasicAICharacter, BasicAIController, and BasicBehaviorTree

File:Tut Rev 06.png

Creating a Task

We are going to create a simple Task for the Behavior Tree to execute. Create a new Blueprint using BTTask_BlueprintBase as its parent. Name this BasicTask

File:BBT CreateTask 01.JPG

Next open up the created Task blueprint and go to its EventGraph. We need a Receive Execute event for when the task is called by the behavior tree and a Finish Execute event that returns success or failure upon task completion. Everything in between is task logic.

File:BBT TaskCreation 02.JPG

Before we fill in the logic for this task we need to create a public variable of type BlackboardKeySelector to store the random location that we find. Note: To create a public variable, make sure that the eye icon beside the variable is highlighted yellow and open. Do so by left clicking the icon. Name this Variable Destination

File:Behavior 11.png

We want this task to take the AI Characters current worldspace location and find a random point within a set radius. So we get the Actors current location, find a random point within a specified radius, and then set the BlackBoard value to that location. Make sure to set the Radius of the Get Random Point in Radius to something large, like 2000, or else the AI may just be told to move an inch and therefore ignore the instruction. *Note: Make sure Success is checked under "Task Completion."

File:Behavior 12.png

Setting Up the Behavior

Open up the Behavior Tree and set it to use your created BlackBoard Asset

File:BBT CreateBehaviorTree 01.jpg

When the Behavior Tree is run it begins at the root and proceeds down the hierarchy executing tasks and returning successes or failures. For our simple wander example we are going to drag out from the bottom of the Root node and create a Sequence. From the bottom of the Sequence we are going to drag out and expand the tasks dropdown. Select the Blueprint Task Destination we created in the previous step. Then from the same sequence node drag out again and select the Move To TargetPoint. Your behavior tree should now look like this.

File:Behavior 13.png

  • IMPORTANT: Note that the acceptable radius here needs to be set to much lower, like 10 to 20, than the value we set up earlier for the AI CharacterÔÇÖs current world space when we set the Radius of the Get Random Point in Radius to around 2000. Setting the value too high here will result in the AI Character not moving at all.

Now, to run the Behavior Tree we have to call it from the AI Controller so in the Graph of your Blueprint AI Controller create the following:

File:BBT Behavior 02.JPG

Please note to select the Behaviour Tree asset from the drop-down box.

This will begin the Behavior Tree Simulation as soon as the Begin Play event is called.

Spawning the AI

The 2 easiest ways to preview the AI are to either place the AI Character blueprint in the level or use spawn actor in the level blueprint to create an instance of it.

File:Tut Rev 07.png

Extras

To get your AI character to rotate based on movement enter the BasicAICharacter>Defaults tab. Find Use Controller Rotation Yaw and set this to false. Open the Components tab and find the Character Movement. Find Orient Rotation to Movement and set it to true.

Useful Links

In Depth Behavior Tree Thread

Video Tutorial for setting up a similar behavior tree system