Unreal Engine 4 Tutorial: Artificial Intelligence
In this Unreal Engine 4 tutorial, you will learn how to use behavior trees and AI Perception to create a simple AI character that roams and attacks enemies. By Tommy Tran.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
Unreal Engine 4 Tutorial: Artificial Intelligence
25 mins
- Getting Started
- What is a Controller?
- Creating an AI Controller
- Creating a Behavior Tree
- The Behavior Tree Editor
- What are Tasks and Composites?
- Moving to a Random Location
- Creating a Blackboard
- The Blackboard Editor
- Creating the Target Location Key
- What is a Service?
- Creating a Service
- Generating a Random Location
- Selecting a Blackboard
- Running the Behavior Tree
- Setting Up AI Perception
- Creating an Enemy Key
- Moving Towards An Enemy
- Setting the Enemy Key
- Creating an Attack Task
- Adding Attack to the Behavior Tree
- Combining the Subtrees
- Creating a Decorator
- Using Observer Aborts
- Where to Go From Here?
Creating a Blackboard
A blackboard is an asset whose sole function is to hold variables (known as keys). You can think of it as the AI’s memory.
While you are not required to use them, blackboards offer a convenient way to read and store data. It is convenient because many of the nodes in behavior trees only accept blackboard keys.
To create one, go back to the Content Browser and select Add New\Artificial Intelligence\Blackboard. Name it BB_Muffin and then open it.
The Blackboard Editor
The blackboard editor consists of two panels:
- Blackboard: This panel will display a list of your keys
- Blackboard Details: This panel will display the properties of the selected key
Now, you need to create a key that will hold the target location.
Creating the Target Location Key
Since you are storing a location in 3D space, you need to store it as a vector. Click New Key and select Vector. Name it TargetLocation.
Next, you need a way to generate a random location and store it in the blackboard. To do this, you can use the third type of behavior tree node: service.
What is a Service?
Services are like tasks in that you use them to do something. However, instead of making the Pawn perform an action, you use services to perform checks or update the blackboard.
Services are not individual nodes. Instead, they attach to tasks or composites. This results in a more organized behavior tree because you have less nodes to deal with. Here’s how it would look using a task:
Here’s how it would look using a service:
Now, it’s time to create a service that will generate a random location.
Creating a Service
Go back to BT_Muffin and click New Service.
This will create a new service and open it automatically. Name it BTService_SetRandomLocation. You’ll need to go back to the Content Browser to rename it.
The service only needs to execute when the Pawn wants to move. To do this, you need to attach it to MoveTo.
Open BT_Muffin and then right-click on MoveTo. Select Add Service\BTService Set Random Location.
Now, BTService_SetRandomLocation will activate when MoveTo activates.
Next, you need to generate a random target location.
Generating a Random Location
Open BTService_SetRandomLocation.
To know when the service activates, create an Event Receive Activation AI node. This will execute when the service’s parent (the node it’s attached to) activates.
To generate a random location, add the highlighted nodes. Make sure to set Radius to 500.
This will give you a random navigable location within 500 units of the Pawn.
If you would like to create your own NavMesh, create a Nav Mesh Bounds Volume. Scale it so that it encapsulates the area you want to be navigable.
Next, you need to store the location into the blackboard. There are two ways of specifying which key to use:
- You can specify the key by using its name in a Make Literal Name node
- You can expose a variable to the behavior tree. This will allow you to select a key from a drop-down list.
You will use the second method. Create a variable of type Blackboard Key Selector. Name it BlackboardKey and enable Instance Editable. This will allow the variable to appear when you select the service in the behavior tree.
Afterwards, create the highlighted nodes:
Summary:
- Event Receive Activation AI executes when it’s parent (in this case, MoveTo) activates
- GetRandomPointInNavigableRadius returns a random navigable location within 500 units of the controlled muffin
- Set Blackboard Value as Vector sets the value of a blackboard key (provided by BlackboardKey) to the random location
Click Compile and then close BTService_SetRandomLocation.
Next, you need to tell the behavior tree to use your blackboard.
Selecting a Blackboard
Open BT_Muffin and make sure you don’t have anything selected. Go to the Details panel. Under Behavior Tree, set Blackboard Asset to BB_Muffin.
Afterwards, MoveTo and BTService_SetRandomLocation will automatically use the first blackboard key. In this case, it is TargetLocation.
Finally, you need to tell the AI controller to run the behavior tree.
Running the Behavior Tree
Open AIC_Muffin and connect a Run Behavior Tree to Event BeginPlay. Set BTAsset to BT_Muffin.
This will run BT_Muffin when AIC_Controller spawns.
Click Compile and then go back to the main editor. Press Play, spawn some muffins and watch them roam around.
That was a lot of set up but you got there! Next, you will set up the AI controller so that it can detect enemies within its range of vision. To do this, you can use AI Perception.
Setting Up AI Perception
AI Perception is a component you can add to actors. Using it, you can give senses (such as sight and hearing) to your AI.
Open AIC_Muffin and then add an AIPerception component.
Next, you need to add a sense. Since you want to detect when another muffin moves into view, you need to add a sight sense.
Select AIPerception and then go to the Details panel. Under AI Perception, add a new element to Senses Config.
Set element 0 to AI Sight config and then expand it.
There are three main settings for sight:
- Sight Radius: The maximum distance the muffin can see. Leave this at 3000.
- Lose Sight Radius: If the muffin has seen an enemy, this is how far the enemy must move away before the muffin loses sight of it. Leave this at 3500.
- Peripheral Vision Half Angle Degrees: How wide the muffin’s vision is. Set this to 45. This will give the muffin a 90 degree range of vision.
By default, AI Perception only detects enemies (actors assigned to a different team). However, actors do not have a team by default. When an actor doesn’t have a team, AI Perception considers it neutral.
As of writing, there isn’t a method to assign teams using Blueprints. Instead, you can just tell AI Perpcetion to detect neutral actors. To do this, expand Detection by Affiliation and enable Detect Neutrals.
Click Compile and then go back to the main editor. Press Play and spawn some muffins. Press the ‘ key to display the AI debug screen. Press 4 on the numpad to visualize AI Perception. When a muffin moves into view, a green sphere will appear.
Next, you will move the muffin towards an enemy. To do this, the behavior tree needs to know about the enemy. You can do this by storing a reference to the enemy in the blackboard.