In this lesson, we will use C++ in Unreal Engine 4 to write some basic classes which will be used for making blueprints for AI. As you may have learnt in lectures, game engines like Unreal Engine 4 have AIs that are driven by behaviour trees.
Making A New Project
For this lesson, we will not be using the Master Starter Project. Instead, we will start with the basic third-person project template provided by Unreal Engine 4.
Launch Unreal Engine 4, choose New Project > Third Person, and set your project location and project name. Then, click on Create Project to create it.
When you open your new project, you will see a level called ThirdPersonExampleMap. In this level, you can playtest with the third-person character and walk around with it.
We need to make some preparations for the AI path finding in this level with a Nav Mesh Bounds Volume. It is an object which you can put in a level which renders with static meshes in order to generate a walkable area inside its volume area for the AI character. To add it into the level, we can search for it in the object panel and add it in.
You will need to set an appropriate scale size for the Nav Mesh Bounds Volume to cover the whole area in the level. Press “P” to check the walkable path area rendered by the object, and it will be shown in green in the level.
Next, we need to create a Behavior Tree and a Blackboard which will be used to set the AI actions. A Blackboard is usually used for data sharing between different modules that look like global variables. A Behavior Tree is a tree which can decide how the AI acts. To create them in the project folder, right click and select Artificial Intelligence > Behaviour Tree (and Blackboard). Name them as AI_BT and AI_BB respectively.
Right click again and this time choose New C++ Class twice. We need to create two Classes for the AI character: AICharacter of which the parent class is Character, and a MyAIController class of which the parent class is AIController.
Coding The Classes
We will start with setting up the MyAIController Class first. This class will be used later for generating blueprints which can be used to control the actions of AICharacter.
Let's set the header file as such:
In this header file, we declared the Blackboard and Behavior Tree as private components. We have also set the property of the Blackboard key as “Target”. The target here is our player, as we would like the AI to follow our player. The function SetSeenTarget will be used to set the target that is seen by the AI Character.
Now, let's set the .cpp file as such:
Now, we can continue with the AICharacter Class. This class will also be used later for generating blueprints. Set its header file as such:
In this header file, we have declared two properties for the AI Character. VisibleAnywhere is used for the AI seeing our player, while EditAnywhere used to set the Behavior Tree for the AI Character. We have also defined the function OnSeePlayer to set the AI's action when it sees our player.
Now, let's set the .cpp file as such:
Building Up The Blueprint
Now that we are done coding out both Classes, we can generate our Blueprints from them. Right click on these C++ classes and choose Create Blueprint class based on AICharacter (and MyAIController). Name these Blueprints as BP_AICharacter and BP_MyAIController respectively.
Next, we need to set up some properties for BP_AICharacter. Open up the Blueprint editor.
Set the following properties under Transform, Animation, and Mesh:
Then, select BP_AICharacter (self) and set it with the following properties and save:
Take note that if you have modified any of the Classes, you will need to recompile them before using the Blueprints.
Building Up The Behavior Tree
Now that we have built up our Blueprint, we now have to build up the Behavior Tree for the AI. First, go to AI_BB to set up the target. As there is only one target in the logic (which is our player), what we should do is just rename the Entry Name found under Key to “Target”.
Next, go to AI_BT and set the Blackboard Asset to AI_BB.
Now, we need to set up the logic below. It means that when the target is seen (set), the AI will move to the target. Otherwise, the AI would wait.
Follow the next few pictorial steps to set up the logic:
When you are done, save it. Your Behavior Tree is now complete!
You now have a functional AI! To test it, pull BP_AICharacter into the level and play it. You will find that after the AI sees your character, it will start following.
We’ve covered a lot of material in this lesson. Here is a recap:
Setting up a Nav Mesh Bounds Volume.
Creating and coding out C++ Classes for the AICharacter and MyAIController.
Building up our Blueprint and Behavior Tree.
miniProject 8 - AI
In this miniProject, you’ll need to program the behaviour of an AI character in your game using C++. The AI character should have 3 abilities (states). You may need to design a suitable theme for your AI to utilize their abilities.
You can also design a life system for your AI (the dead state can be seen as one state of your AI).
You need to come up with your own theme and make your AI act accordingly to it (so your AI should not acting randomly).
This miniProject is worth 3.0% of your Assignment Component. For HW_FBTE, the breakdown of the marks is as such:
Breakdown / Grading
Implemented 3 states for AI meaningfully.
Implemented only 2 states for AI.
Implemented only 1 (or none) state for AI.
The experience runs at a consistent 75 fps.
The experience has occasional judders.
The experience judders frequently.
All meshes have appropriate materials, are placed with purpose, and don’t overlap.
Most meshes have appropriate materials, are placed with purpose, and don’t overlap.
The project is riddled with out of place materials/meshes.
Audio enhances the experience meaningfully.
Audio is secondary to the rest of the experience.
Audio is hardly (or not) present.
The student has experimented to create a unique experience that includes a novel or intriguing perspective and locomotion technique.
The student has created an interesting experience but does not seem to have considered creating something unique.
The perspective and locomotion techniques are unchanged and the experience is bland.
Fits the theme creatively; the player has put in thorough thought and effort into the project.
Somewhat fits the theme; the student seems to have put adequate effort into the project.
Does not fit the theme (if any); the student clearly did not put effort into the project.
Should you need any further clarification, feel free to contact the TA marking this miniProject.
If you do not have a VR headset, that is fine. Just design the level as per normal and "imagine" what it would look like with the headset. Your TAs will not penalise you on this.
For this miniProject, you will need to set the Game Default Map to your starting level by clicking on Edit > Project Settings > Maps & Modes and build the project for Windows (32-bit) by clicking on File > Package Project > Windows > Windows (32-bit).
You must be running Unreal Engine 4 on Windows (and not on any other operating system) in order to build the project for Windows. If you do not have a computer running Windows, save a copy of your project online or on an external drive and use one of the computers found in Media Teaching Lab 2A (AS6 04-26) to build the project.
After building the project, compress its contents as a .zip file titled as [YourMatricNumber]_[YourFullName] (e.g. A0123456Z_JohnSmith.zip).
Make sure to include a read-me file titled README.txt, which must list out all of the states implemented and their transitions and a short description of the theme you have decided to work with.
If you do not follow the submission guidelines exactly, your marks may get deducted!