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.

4.8 (29) · 1 Review

Save for later
Share
You are currently viewing page 3 of 4 of this article. Click here to view the first page.

Creating an Enemy Key

Open BB_Muffin and then add a key of type Object. Rename it to Enemy.

Unreal Engine 4 AI Tutorial

Right now, you will not be able to use Enemy in a MoveTo. This is because the key is an Object but MoveTo only accepts keys of type Vector or Actor.

To fix this, select Enemy and then expand Key Type. Set Base Class to Actor. This will allow the behavior tree to recognize Enemy as an Actor.

Unreal Engine 4 AI Tutorial

Close BB_Muffin. Now, you need to create a behavior to move towards an enemy.

Moving Towards An Enemy

Open BT_Muffin and then disconnect Sequence and Root. You can do this by alt-clicking the wire connecting them. Move the roam subtree aside for now.

Next, create the highlighted nodes and set their Blackboard Key to Enemy:

Unreal Engine 4 AI Tutorial

This will move the Pawn towards Enemy. In some cases, the Pawn will not completely face towards its target so you also use Rotate to face BB entry.

Now, you need to set Enemy when AI Perception detects another muffin.

Setting the Enemy Key

Open AIC_Muffin and then select the AIPerception component. Add an On Perception Updated event.

Unreal Engine 4 AI Tutorial

This event will execute whenever a sense updates. In this case, whenever the AI sees or loses sight of something. This event also provides a list of actors it currently senses.

Add the highlighted nodes. Make sure you set Make Literal Name to Enemy.

Unreal Engine 4 AI Tutorial

This will check if the AI already has an enemy. If it doesn’t, you need to give it one. To do this, add the highlighted nodes:

unreal engine 4 ai tutorial

Summary:

  1. IsValid will check if the Enemy key is set
  2. If it is not set, loop over all the currently perceived actors
  3. Cast To BP_Muffin will check if the actor is a muffin
  4. If it is a muffin, check if it is dead
  5. If IsDead returns false, set the muffin as the new Enemy and then break the loop

Click Compile and then close AIC_Muffin. Press Play and then spawn two muffins so that one is in front of the other. The muffin behind will automatically walk towards the other muffin.

Unreal Engine 4 AI Tutorial

Next, you will create a custom task to make the muffin perform an attack.

Creating an Attack Task

You can create a task within the Content Browser instead of the behavior tree editor. Create a new Blueprint Class and select BTTask_BlueprintBase as the parent.

Unreal Engine 4 AI Tutorial

Name it BTTask_Attack and then open it. Add an Event Receive Execute AI node. This node will execute when the behavior tree executes BTTask_Attack.

Unreal Engine 4 AI Tutorial

First, you need to make the muffin attack. BP_Muffin contains an IsAttacking variable. When set, the muffin will perform an attack. To do this, add the highlighted nodes:

Unreal Engine 4 AI Tutorial

If you use the task in its current state, execution will become stuck on it. This is because the behavior tree doesn’t know if the task has finished. To fix this, add a Finish Execute to the end of the chain.

Unreal Engine 4 AI Tutorial

Next, enable Success. Since you are using a Sequence, this will allow nodes after BTTask_Attack to execute.

Unreal Engine 4 AI Tutorial

This is what your graph should look like:

Unreal Engine 4 AI Tutorial

Summary:

  1. Event Receive Execute AI will execute when the behavior tree runs BTTask_Attack
  2. Cast To BP_Muffin will check if Controlled Pawn is of type BP_Muffin
  3. If it is, its IsAttacking variable is set
  4. Finish Execute will let the behavior tree know the task has finished successfully

Click Compile and then close BTTask_Attack.

Now, you need to add BTTask_Attack to the behavior tree.

Adding Attack to the Behavior Tree

Open BT_Muffin. Afterwards, add a BTTask_Attack to the end of the Sequence

Unreal Engine 4 AI Tutorial

Next, add a Wait to the end of the Sequence. Set its Wait Time to 2. This will make sure the muffin doesn’t constantly attack.

Unreal Engine 4 AI Tutorial

Go back to the main editor and press Play. Spawn two muffins like last time. The muffin will move and rotate towards the enemy. Afterwards, it will attack and wait two seconds. It will then perform the entire sequence again if it sees another enemy.

Unreal Engine 4 AI Tutorial

In the final section, you will combine the attack and roam subtrees together.

Combining the Subtrees

To combine the subtrees, you can use a Selector composite. Like Sequences, they also execute from left to right. However, a Selector will stop when a child succceeds rather than fail. By using this behavior, you can make sure the behavior tree only executes one subtree.

Open BT_Muffin and then create a Selector after the Root node. Afterwards, connect the subtrees like so:

Unreal Engine 4 AI Tutorial

This set up will allow only one subtree to run at a time. Here is how each subtree will run:

  • Attack: Selector will run the attack subtree first. If all tasks succeed, the Sequence will also succeed. The Selector will detect this and then stop executing. This will prevent the roam subtree from running.
  • Roam: The selector will attempt to run the attack subtree first. If Enemy is not set, MoveTo will fail. This will cause Sequence to fail as well. Since the attack subtree failed, Selector will execute its next child which is the roam subtree.

Go back to the main editor, press Play. Spawn some muffins to test it out.

Unreal Engine 4 AI Tutorial

"Hang on, why doesn’t the muffin attack the other one immediately?"

In traditional behavior trees, execution starts from the root every update. This means every update, it would try the attack subtree first and then the roam subtree. This means the behavior tree can instantly change subtrees if the value of Enemy changes.

However, Unreal’s behavior trees do not work the same way. In Unreal, execution picks up from the last executed node. Since AI Perception does not sense other actors immediately, the roam subtree begins running. The behavior tree now has to wait for the roam subtree to finish before it can re-evaluate the attack subtree.

To fix this, you can use the final type of node: decorators.

Creating a Decorator

Like services, decorators attach to tasks or composites. Generally, you use decorators to perform checks. If the result is true, the decorator will also return true and vice versa. By using this, you can control if a decorator’s parent can execute.

Decorators also have the ability to abort a subtree. This means you can stop the roam subtree once Enemy is set. This will allow the muffin to attack an enemy as soon as one is detected.

To use aborts, you can use a Blackboard decorator. These simply check if a blackboard key is or isn’t set. Open BT_Muffin and then right-click on the Sequence of the attack subtree. Select Add Decorator\Blackboard. This will attach a Blackboard decorator to the Sequence.

Unreal Engine 4 AI Tutorial

Next, select the Blackboard decorator and go to the Details panel. Set Blackboard Key to Enemy.

Unreal Engine 4 AI Tutorial

This will check if Enemy is set. If it is not set, the decorator will fail and cause the Sequence to fail. This will then allow the roam subtree to run.

In order to abort the roam subtree, you need to use the Observer Aborts setting.

Tommy Tran

Contributors

Tommy Tran

Author

Over 300 content creators. Join our team.