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?
In video games, Artificial Intelligence (AI) usually refers to how a non-player character makes decisions. This could be as simple as an enemy seeing the player and then attacking. It could also be something more complex such as an AI-controlled player in a real-time strategy.
In Unreal Engine, you can create AI by using behavior trees. A behavior tree is a system used to determine which behavior an AI should perform. For example, you could have a fight and a run behavior. You could create the behavior tree so that the AI will fight if it is above 50% health. If it is below 50%, it will run away.
In this tutorial, you will learn how to:
- Create an AI entity that can control a Pawn
- Create and use behavior trees and blackboards
- Use AI Perception to give the Pawn sight
- Create behaviors to make the Pawn roam and attack enemies
- Part 1: Getting Started
- Part 2: Blueprints
- Part 3: Materials
- Part 4: UI
- Part 5: How To Create a Simple Game
- Part 6: Animation
- Part 7: Audio
- Part 8: Particle Systems
- Part 9: Getting Started With AI (you are here!)
- Part 10: How to Create a Simple FPS
- Part 1: Getting Started
- Part 2: Blueprints
- Part 3: Materials
- Part 4: UI
- Part 5: How To Create a Simple Game
- Part 6: Animation
- Part 7: Audio
- Part 8: Particle Systems
- Part 9: Getting Started With AI (you are here!)
- Part 10: How to Create a Simple FPS
Getting Started
Download the starter project and unzip it. Navigate to the project folder and open MuffinWar.uproject.
Press Play to start the game. Left-click within the fenced area to spawn a muffin.
In this tutorial, you will create an AI that will wander around. When an enemy muffin comes into the AI’s range of vision, the AI will move to the enemy and attack it.
To create an AI character, you need three things:
- Body: This is the physical representation of the character. In this case, the muffin is the body.
- Soul: The soul is the entity controlling the character. This could be the player or an AI.
- Brain: The brain is how the AI makes decisions. You can create this in different ways such as C++ code, Blueprints or behavior trees.
Since you already have the body, all you need is a soul and brain. First, you will create a controller which will be the soul.
What is a Controller?
A controller is a non-physical actor that can possess a Pawn. Possession allows the controller to—you guessed it—control the Pawn. But what does ‘control’ mean in this context?
For a player, this means pressing a button and having the Pawn do something. The controller receives inputs from the player and then it can send the inputs to the Pawn. The controller could also handle the inputs instead and then tell the Pawn to perform an action.
In the case of AI, the Pawn can receive information from the controller or brain (depending on how you program it).
To control the muffins using AI, you need to create a special type of controller known as an AI controller.
Creating an AI Controller
Navigate to Characters\Muffin\AI and create a new Blueprint Class. Select AIController as the parent class and name it AIC_Muffin.
Next, you need to tell the muffin to use your new AI controller. Navigate to Characters\Muffin\Blueprints and open BP_Muffin.
By default, the Details panel should show the default settings for the Blueprint. If it doesn’t, click Class Defaults in the Toolbar.
Go to the Details panel and locate the Pawn section. Set AI Controller Class to AIC_Muffin. This will spawn an instance of the controller when the muffin spawns.
Since you are spawning the muffins, you also need to set Auto Possess AI to Spawned. This will make sure AIC_Muffin automatically possesses BP_Muffin when spawned.
Click Compile and then close BP_Muffin.
Now, you will create the logic that will drive the muffin’s behavior. To do this, you can use behavior trees.
Creating a Behavior Tree
Navigate to Characters\Muffin\AI and select Add New\Artificial Intelligence\Behavior Tree. Name it BT_Muffin and then open it.
The Behavior Tree Editor
The behavior tree editor contains two new panels:
- Behavior Tree: This graph is where you will create nodes to make the behavior tree
- Details: Nodes you select will display its properties here
- Blackboard: This panel will show Blackboard keys (more on this later) and their values. Will only display when the game is running.
Like Blueprints, behavior trees consist of nodes. There are four types of nodes in behavior trees. The first two are tasks and composites.
What are Tasks and Composites?
As its name implies, a task is a node that "does" something. This can be something complex such as performing a combo. It could also be something simple such as waiting.
To execute tasks, you need to use composites. A behavior tree consists of many branches (the behaviors). At the root of each branch is a composite. Different types of composites have different ways of executing their child nodes.
For example, you have the following sequence of actions:
To perform each action in a sequence, you would use a Sequence composite. This is because a Sequence executes its children from left to right. Here’s what it would look like:
If any of a Sequence’s children fail, the Sequence will stop executing.
For example, if the Pawn is unable to move to the enemy, Move To Enemy will fail. This means Rotate Towards Enemy and Attack will not execute. However, they will execute if the Pawn succeeds in moving to the enemy.
Later on, you will also learn about the Selector composite. For now, you will use a Sequence to make the Pawn move to a random location and then wait.