Skip to content
This repository has been archived by the owner on Aug 13, 2022. It is now read-only.

Initialising Stats

Sahil Jain edited this page May 6, 2021 · 1 revision

Before we can interact with character attributes using abilities, we need to set up our character's starting stats. We do this by using a special type of ability - Stat Initialisation Ability.

Stat Initialisation Ability

Create a new Stat Initialisation Ability via the Assets | Create | Gameplay Ability System | Abilities | Stat Initialisation menu, and call it Player Initial Stats. Set the Ability Name to Player Initial Stats in the Inspector.

We can define optional Cost and Cooldown Gameplay Effects, but our ability won't use these, so we will leave them blank.

The Initialisation GE list contains which Gameplay Effects to apply to the character. We want our character to start with 5 health, so we need to create a Gameplay Effect:

  1. Create a new Gameplay Effect via the Assets | Create | Gameplay Ability System | Gameplay Effect Definition menu, and name it Player Initial Health Gameplay Effect
  2. Configure the Gameplay Effect to grant 5 health instantly:

Assign the Gameplay Effect to the Initialisation GE list in the Player Intial Stats ability.

Now, we need some way of activating this ability when the game starts. Activating an ability is a simple two step-procedure:

AbstractAbilitySpec abilitySpec = ability.CreateSpec(abilitySystemCharacter);
StartCoroutine(abilitySpec.TryActivateAbility());

So, we'll create a MonoBehaviour on our player which activates the ability on Awake().

public class InitialStatsBehaviour : MonoBehaviour
{
    [SerializeField] private AbstractAbilityScriptableObject ability;
    [SerializeField] private AbilitySystemCharacter abilitySystemCharacter;
    void Awake()
    {
        AbstractAbilitySpec abilitySpec = ability.CreateSpec(abilitySystemCharacter);
        StartCoroutine(abilitySpec.TryActivateAbility());
    }

}

Press Play to test the ability. You should see that the player has 5 health.