Blueprint Behavior Tree Tutorial - Epic Wiki

# Blueprint Behavior Tree Tutorial

Rate this Page:

5.00

(5 votes)

Approved for Versions:4.5.0

# Contents

# 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.

Tut Rev 01.png

Note: P toggles path visibility

Tut Rev 02.png

# Creating the AIController

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

Tut 01.png

# Creating the AICharacter

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

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)

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.)

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 )

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.

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

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:

Tut 05.png

  • Name this asset BasicBehaviorTree.

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

BasicAiBlackBoard, BasicAICharacter, BasicAIController, and BasicBehaviorTree

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

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.

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

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."

Behavior 12.png

# Setting Up the Behavior

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

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.

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:

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.

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

Retrieved from "https://wiki.unrealengine.com/index.php?title=Blueprint_Behavior_Tree_Tutorial&oldid=9988"

Categories:

Hidden category: