-
Notifications
You must be signed in to change notification settings - Fork 196
A simple ability
In the previous section, we applied a Gameplay Effect
manually. Usually, you would use an Ability
to orchestrate the application of Gameplay Effects
, as well as performing/timing animations, etc. The Ability
is the cornerstone of the Gameplay Ability System
, and is the most complex component. Since Abilities
are highly game-specific, these will require coding to develop as appropriate.
An ability references various other assets, which at a minimum include:
- Cooldown Gameplay Effect
- Cost Gameplay Effect
- Gameplay Effect(s) to be applied
- Gameplay Tags
For the first part of this tutorial, we'll create a new ability that applies the buff we created in the last section. We'll use the Simple Ability
for this.
To keep assets organised, we'll store abilities in a Assets/Abilities/[Ability Name]
folder structure. We'll call this ability Health Buff
, so begin by creating a new folder called Assets/Abilities/Health Buff
. Create a new Simple Ability
via the Assets | Create | Gameplay Ability System | Abilities | Simple Ability
menu, and call it Health Buff
. Set the Ability Name
to Health Buff
in the Inspector.
Now, we need to create the Cooldown Gameplay Effects associated with our ability. Create a new Gameplay Effect
via the Assets | Create | Gameplay Ability System | Gameplay Effect Definition
menu, and call it Health Buff Cooldown Gameplay Effect
. We'll set this Gameplay Effect
to have a duration of 2 seconds, with no modifiers.
Create a new Gameplay Tag
(via Assets | Create | Gameplay Ability System | Tag
menu) called Health Buff.Cooldown
and assign it to the Gameplay Effect
Asset Tag
and add it to the Granted Tags
list.
Assign this Gameplay Effect
to the Cooldown
property of the Ability
in the inspector.
Create a new Gameplay Effect, setting the Duration Policy
to Instant
, and a new Gameplay Tag
for the Asset Tag
property:
Prior to casting an ability, a resource check is done based on the Cost
. Since the Health
attribute starts at 0, we would not be able to activate the ability. For this example, we can get around this by leaving the Modifiers
list empty.
Assign this Gameplay Effect
to the Cost
property of the Ability
in the inspector.
The Simple Ability
accepts a single Gameplay Effect
to apply when the ability is activated. Assign the Gameplay Effect
created in the previous section (Health Buff Gameplay Effect
).
Create a new Gameplay Tag
(via Assets | Create | Gameplay Ability System | Tag
menu), and call it Health Buff
. Assign this Gameplay Tag
to the Ability Tags/Ability Tag
property for the Health Buff
ability.
The ability is now properly set-up, and should look like:
Activating an ability is a two-step process:
- Create the Ability Spec, which is a unique instance of the ability
- Activate the ability using a character. Since an ability can span multiple frames (e.g. projectile travelling to target), the activation method is a
Coroutine
, andStartCoroutine()
must be used.
Assuming the AbilitySystemCharacter
is called abilitySystemCharacter
, and the AbstractAbilityScriptableObject
is called ability
:
AbstractAbilitySpec abilitySpec = ability.CreateSpec(abilitySystemCharacter);
StartCoroutine(abilitySpec.TryActivateAbility());
In the previous example, we noted that the Gameplay Effect
could be applied endlessly, unless we modified the Granted Tags
and Application Required Tags / Ignore Tags
lists. We'll now empty the Application Required Tags / Ignore Tags
list.
When you activate the ability, you should now see the Health
attribute increase. The ability cannot be activated more than once every 2 seconds, and the effect of the ability (the increase in health) stays for 5 seconds, and then reverts.
By coordinating the use of Gameplay Effects
, Gameplay Tags
and Abilities
, you can create complex abilities.