Page cover

Minion AI (Behavior Trees)

Minion AI in this project is powered by Behavior Trees

Classes and Assets

C++ classes and Unreal assets for this project's AI can be found on GitHub:

Jump to Section...

AI Setup

Behavior Trees run on an AI Controller, which assumes control of an AI Character or Pawn. Set up both as follows:

1. Extend the AAIController class, adding a BehaviorTree reference:

2. Run the Behavior Tree in your new class's BeginPlay()

3. Create a Blueprint with your new AI Controller class and assign your behavior tree of choice to it

4. Assign your new AI Controller Class to your AI Character or Pawn.

5. If you plan on spawning your AI minion and want AI to active immediately, set the following in your AI Character class constructor:

Minion Behavior Tree

The full behavior tree for the ranged minion AI (broken down below). Services are Green || Decorators are Blue || Composites are Gray || Tasks are Purple

Unreal Behavior Tree terminology overview:

chevron-rightClick to Expand...hashtag
  • Differences in UE4 Behavior Treesarrow-up-right (compared to traditional behavior trees)

  • Terminology:

    • Blackboard - Key:value store for sharing data between behaviors in the tree (optimized for access and performance).

    • Service Nodes - Execute at a defined frequency as long as their branch is being executed. Often used to make checks and update the Blackboard.

    • Decorator Nodes - Attach to other nodes and make decisions on whether or not a branch or node in the tree can execute. Decorator nodes are able to change the flow of a tree by aborting lower priority executing nodes and executing their branch immediately (for example, to have an AI immediately stop whatever it was doing to flee when its health drops low, you might put a decorator node that monitors its health status value in the blackboard and aborts other running nodes when it's set to "Low").

    • Task Nodes - Actionable things to do. Task nodes perform some behavior and don't have an output connection.

    • Composite Nodes - The root of a branch that defines how the branch is executed (in sequence, parallel, or select one). Composite nodes can have decorators applied to them to control entry into the branch, and services that will only be active if the children of the composite are being executed.

Behavior: Move Near Target and Attack

AI moves near target and attack it (left) and the behavior tree powering it (right).

Custom nodes:

Behavior tree branch:

Services are Green || Decorators are Blue || Selectors are Gray || Tasks are Purple

Sequence: Attack Target if within Attack Range

TBTService_CheckAttackRange.cpparrow-up-right (not pictured) constantly monitors the distance between the AI's controlled minion and the target, setting the WithinAttackRange Blackboard key accordingly. If the target is within attack range when execution flow is passed to this branch, the AI executes TBTTask_RangedAttack.cpparrow-up-right, firing a projectile from the AI minion's muzzle at the target three times. This attack has a configurable deviation that can be used to tune AI difficulty (higher deviations make the attack more inaccurate). The attack is then put on cooldown.

Sequence: Move Closer to Target Actor

If the target actor is not WithinAttackRange or the attack is on cooldown, the AI will run an Environmental Query Systemarrow-up-right (EQS) query to find a random location on the navmesh nearby and with line of site to the target actor, and move toarrow-up-right that location. If there is no target actor set (a target has not been spotted by the AI yet), the AI "cheats" and picks Player 0 (first player to join the session) as its target instead.

Behavior: Hide and Heal

Upon dropping to low health, the AI will stop whatever it is doing immediately to go hide and heal.

Custom nodes:

Behavior tree branch:

Services are Green || Decorators are Blue || Selectors are Gray || Tasks are Purple

Observer Aborts: Stop all other actions when health drops low:

TBTService_CheckHealth.cpparrow-up-right (not pictured) monitors minion's health, updating the HealthLow Blackboard key when it drops below a configurable threshold. The AI monitors the HealthLow key for changes (via the "HealthLow?" decorator). When it observes a change, this decorator immediately abortsarrow-up-right lower priority executing nodes and starts executing the hide and heal sequence.

Sequence: Find a hiding spot and move to it:

The AI will then find a safe spot on the navmesh via Unreal's EQS and move to it. A "safe spot" is determined via EQS's scoring systemarrow-up-right. Candidates are scored based on line of site to the AI's target (strong weight), and then a distance to the AI's target (lower weight, farther is better). The end result is the AI picks from locations without line of site from the target ("hidden") first, and then based on distance to the target second (within a maximum radius of the target, so the AI doesn't run too far away).

Sequence: Heal Self and Cooldown:

Finally, once it reaches its determined safe spot, the AI heals itself via executing TBTTask_HealSelf.cpparrow-up-right. This branch then goes on cooldown for 60 seconds (meaning the AI will not perform this sequence nor abort other lower priority nodes the next time the AI's health drops low as long as this cooldown is active).

Last updated