• Guides
  • Scripting
  • Visual Scripting
  • Behavior Trees

Behavior Trees

Overview

What is AI in the game? AI in the game means that units have "intelligent" behavior by simulating the actions of real players. Such as PVE mode, Intelligent AFK system, Enemy troops, Summons in-game, etc.

These are all effects achieved through the game AI. Game AI can spawn enemies, and simplify players' actions, mostly, as an essential part of gameplay, enhancing the player's engagement. Behavior is one way to realize game AI. A behavior tree is to set up a "tree" for NPCS' actions, such as walking, attacking, and jumping. This part is introduction of using ECA, the Lua can be known at Lua API – Behavior trees

Initialization

To get started, you need to set up some initialization. For example, the player unit and healer unit will be used frequently. To facilitate subsequent use, set global variables for storage first.

Set a global variable of type unit to store player units. here you store "mainUnit", or you can choose any unit you like.

Set another unit global variable to store healer.

For a better experience, you can set "Camera - Set Unit for Camera to Follow".

You set "Player - Select Unit", Let the player select the unit.

S3-1

Loop through units and perform actions on them

Because every unit participates in the AI behavior, and the AI behavior is different between each type, so you need to iterate through each unit to execute actions on it.

When the game has passed 1 second, which is when the game has started, the AI team starts to act autonomously. For easy management, set a global variable "unitGroupAI" of type Unit Group to store all neutral hostile units, that is, all enemies.

Each enemy participates in the AI action, so you need to loop through the unit group to perform the action.

There are two different unit types in the unit group, and the AI behavior is different in each type. Therefore, you need to pick out two unit types: guard and healers.

S3-2

Once the unit type is filtered, how do you tell them to perform different AI actions? You can transfer information by sending and receiving custom events.

In CliCli, you can select the trigger to send custom events.

When creating a custom event, you can also add parameters to be passed.

The passed parameter here is the Unit selected when traversing units.

The Receive Custom Events can also be selected in the trigger.

s3-3

s3-4

Once the guard and healer receive their own different events, they can perform different AI actions. For now, you have completed the first part of the triggers.

AI behavior of the guard is implemented.

Guard can perform the AI behavior when he receives a custom event from "AI_Guard".

You set a Unit local variable named "unit" to store the guard parameter sent from the previous trigger.

The guard then need to determine if an enemy is approaching the healer and if so, the guard need to protect the healer first.

Set a Float local variable named "distance" to store the distance between the player and the healer.

By determining whether the distance is less than 300, we can determine if the healer has an enemy nearby.

s3-5

If the distance is less than 300, the condition is true, then the guard attacks the enemy by issuing the command to Unit: Attack unitPlayer.

If the condition is false, indicating that there are no enemies near the healer, then proceed to determine if the guard unit has less than half of its maximum HP.

If the condition is true, it indicates less than half health, then the guard needs to return to the healer's location. Issue the command to unit: Move to Location point for unitHealer.

If the condition is false, indicating sufficient health, then proceed to determine if there are enemies in the areaAI area.

If so, attack the enemy.

If not, it moves to a random location in the areaAI and patrols.

s3-6

The healer's AI behavior is implemented.

When the healer receives the sent custom event AI_Healer, it is ready to perform its own AI behavior.

Similarly, set a unit local variable to store the healer parameter sent from the trigger in Part 1.

Then determine if the healer's current health is equal to its own maximum health.

If the condition is true, it indicates the healer is not injured and can treat the partner.

You can create a new function called "MinHP unit" to find the unit with the lowest health. You can use Function Library directly.

S3-7

In the first step of the function, setting proportionHP to a "Float" local variable, representing a percentage of HP, the initial value is 1.

Then set a "unit" local variable to store the unit with the lowest health. The initial value is empty, indicating that no guard is injured.

Then traversal the unit group to perform an action, so we can pick up the unit with the lowest HP.

Then set a "Float" local variable "proportionMinHP" to store the traversal result by the current life ÷ maximum life.

Then determine if proportionMinHP is less than proportionHP.

If the condition is true, it means iterating over a smaller number than the current number, setting proportionHP to equal proportionMinHP, and recording the smaller number.

Then set the unit with the lowest health as the Unit selected when traversing units.

If the condition is false, no action is performed.

In this way, you can pick up the unit with the lowest health in the unit group. The last step, return it.

S3-8

The function is created. You call this function and set the unit in the variable "unitInjured" to equal the function returned.

s3-9

Next, determine whether the unit with the lowest health "unitInjured" equals the empty unit returned by the function.

If the condition is true, indicating no partner was injured, then proceed to determine if the distance from the player unit to the healer is less than 200.

If the condition is true, it indicates the enemy is nearby, then attack the player's unit;

If the condition is false, it indicates that the enemy is not nearby, then maintain the center of the areaAI.

s3-10

If the unit returned from the function is not empty, indicating a partner has been injured, then the healer needs to stop the current action and treat the unit with the lowest health.

In order to simulate the action of the treat, you can choose to play an action animation. This adds realism to the game. Of course, in the beginning, if the healer's health is not enough, he will treat himself first.

When programming, you need to stop healer’s action and treat himself.

S3-11