Utility AI in UE4 (1/8)
I'll use one of the boss characters from my game as an example.
The "Bug Queen" can choose one of three actions:
1. Wait (or **Idle**)
2. Attack (with a **Laser**)
3. Spawn some minions (or **MakeBug**)
(2/8)
I use two new classes to accomplish this.
1. UtilityTaskManager: An ActorComponent that chooses an action for Bug Queen every 0.5 seconds
2. UtilityTask: An Object that I extend to define each task. Each Task Type here inherits from UtilityTask.
(3/8)
UtilityTaskManager (UTM) is the most complicated part.
We set up the possible actions using the TaskTypes array.
Every 0.5s, UTM loops through the Tasks, scores them, and executes the winner.
If a task is going to take longer than 0.5s, it can deactivate UTM for more time.
(4/8)
UtilityTasks are just an Object with some functions defined:
1. Construct: Called on NPC spawn
2. Score: Rate task from 0 - 1. NPC does task w/ highest score.
3. Start: Called when task begins
4. Cancel: Called when NPC switches to another task
(5/8)
I extend UtilityTask into a character-specific task class.
BugQueenTask is a UtilityTask + variables for the queen's controller and pawn.
It overrides the Construct function of UtilityTask to set up those variables
(6/8)
I extend BugQueenTask into a class for each possible action.
BQT_Laser overrides the Score and Start functions from UtilityTask.
I score this task based on time since the last laser.
On start, I call an event on the controller to fire the laser.
(7/8)
Here's the code for MFT_Laser.
I put the code for this inside the controller, because it gives me access to async functions like MoveComponentTo, and timelines.
When the laser attack starts, I pause the UtilityTaskManager and resume when the attack is done.
(8/8)
Why I like this approach:
1. All blueprint. No behavior trees/blackboards
2. Every action separated into its own file
3. Still gives you full access to the character controller
4. Can easily add Utility AI to any NPC when you need it.
You can follow @MorganWalkup.
Tip: mention @twtextapp on a Twitter thread with the keyword “unroll” to get a link to it.

Latest Threads Unrolled: