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 4 of 4 of this article. Click here to view the first page.

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.

Using Observer Aborts

Observer aborts will abort a subtree if the selected blackboard key has changed. There are two types of aborts:

  1. Self: This setting will allow the attack subtree to abort itself when Enemy becomes invalid. This can occur if the Enemy dies before the attack subtree completes.
  2. Lower Priority: This setting will cause lower priority trees to abort when Enemy is set. Since the roam subtree is after attack, it is of lower priority.

Set Observer Aborts to Both. This will enable both abort types.

Unreal Engine 4 AI Tutorial

Now, the attack subtree can immediately go into roaming if it no longer has an enemy. Also, the roam subtree can immediately go into attack mode once it detects an enemy.

Here is the complete behavior tree:

Unreal Engine 4 AI Tutorial

Summary of attack subtree:

  1. Selector will run the attack subtree if Enemy is set
  2. If it is set, the Pawn will move and rotate towards the enemy
  3. Afterwards, it will perform an attack
  4. Finally, the Pawn will wait two seconds

Summary of roam subtree:

  1. Selector will run the roam subtree if the attack subtree fails. In this case, it will fail if Enemy is not set.
  2. BTService_SetRandomLocation will generate a random location
  3. The Pawn will move to the generated location
  4. Afterwards, it will wait for five seconds

Close BT_Muffin and then press Play. Spawn some muffins and prepare for the deadliest battle royale ever!

Unreal Engine 4 AI Tutorial

Where to Go From Here?

You can download the completed project here.

As you can see, it’s easy to create a simple AI character. If you want to create more advanced AI, check out the Environment Query System. This system will allow your AI to collect data about the environment and react to it.

If you want to keep learning, check out the next post in the series, where I’ll show you how to create a simple first-person shooter.

Tommy Tran

Contributors

Tommy Tran

Author

Over 300 content creators. Join our team.