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?
Moving to a Random Location
Create a Sequence and connect it to the Root.
Next, you need to move the Pawn. Create a MoveTo and connect it to Sequence. This node will move the Pawn to a specified location or actor.
Afterwards, create a Wait and connect it to Sequence. Make sure you place it to the right of MoveTo. Order is important here because the children will execute from left to right.
Congratulations, you have just created your first behavior! It will move the Pawn to a specified location and then wait for five seconds.
To move the Pawn, you need to specify a location. However, MoveTo only accepts values provided through blackboards so you will need to create one.
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.
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.