2 min read

[13/50] Triggering GAS ability from Behavior Tree in Unreal

Stable diffusion thinks this is an image of a behavior tree in the backdrop of mountains.
Stable diffusion thinks this is an image of a behavior tree in the backdrop of mountains.

The Gameplay Ability System or GAS is framework for creating and managing player and NPC abilities during the gameplay through a flexible system that involves the use Gamplay Tags.

When using GAS, instead of  having to manually trigger, track execution and update attributes, the developers can declare abilities, tasks and effects and grant or trigger them in response to gameplay events.

More importantly the Ability System acts as a nice abstraction for managing various events and data when entities, including the player, attain, release or deploy abilities. Let's look at a simple BTTask below which executes an ability.

⚠️
Note: While GAS has an extensive Blueprint interface, the initial setup does require a fair bit of C++.
Unreal Blueprint showing a BTTask to execute gameplay-ability
Unreal Blueprint showing a BTTask to execute gameplay-ability

In the above task, we check if the pawn, which is controlled by the Behavior Tree, triggering the task is an entity configured with the ability system. If yes, it proceeds to activate that ability defined by the Ability Class variable. Now the ability will only be executed if the entity has already been granted the same. Next we add a 2-second delay (which may or may not be required in your case) before marking the task as finished successfully.

Using the Return Value pin on the Try Activate Ability by Class node, we could have branched to mark execution as success only if the activation was successful as in the image below.

Unreal Blueprint showing branching to mark execution of BTTask success.
Unreal Blueprint showing branching to mark execution of BTTask success.

But the catch is Try Activate ability by Class can return false positives and therefore it cannot be of consistent utility and we design around this limitation elsewhere in the chain and simply mark all executions as successful.

The context menu of Unreal Behaviour Tree showing our new BTTask
The context menu of Unreal Behaviour Tree showing our new BTTask

And finally, we have our new task available in the Behavior Tree context menu to utilise as desired.