GameplayAbilities API – Implementing stats with UAttributeSet

The GameplayAbilities API allows you to associate a set of attributes, that is, UAttributeSet, to an Actor. UAttributeSet describes properties appropriate for that Actor's in-game attributes, such as Hp, Mana, Speed, Armor, AttackDamage, and so on. You can either define a single game-wide set of attributes common to all Actors, or several different sets of attributes appropriate for the different classes of actors.

Getting ready

AbilitySystemComponent is the first thing you will need to add to your actors to equip them to use GameAbilities API and UAttributeSets. To define your custom UAttributeSet, you will simply derive from the UAttributeSet base class and extend the base class with your own series of UPROPERTY members. After that, you must register your custom AttributeSet with your Actor class' AbilitySystemComponent.

How to do it…

  1. Link to the GameplayAbilities API in your ProjectName.Build.cs file.
  2. In its own file, derive from the UAttributeSet class and deck the class out with a set of UPROPERTY that you want each Actor to have in their property set. For example, you might want to declare your UAttributeSet derivate class similar to the following piece of code:
    #include "Runtime/GameplayAbilities/Public/AttributeSet.h"
    #include "GameUnitAttributeSet.generated.h"
    
    UCLASS(Blueprintable, BlueprintType)
    class CHAPTER12_API UGameUnitAttributeSet : public UAttributeSet
    {
      GENERATED_BODY()
      public:
      UGameUnitAttributeSet( const FObjectInitializer& PCIP );
      UPROPERTY( EditAnywhere, BlueprintReadWrite, Category = GameUnitAttributes )  float Hp;
      UPROPERTY( EditAnywhere, BlueprintReadWrite, Category = GameUnitAttributes )  float Mana;
      UPROPERTY( EditAnywhere, BlueprintReadWrite, Category = GameUnitAttributes )  float Speed;
    };

    Tip

    If your code is networked, you might want to enable replication on each of the UPROPERTY with the replicated declaration in the UPROPERTY macro.

  3. Connect GameUnitAttributeSet with your AbilitySystemComponent inside your Actor class by calling the following code:
    AbilitySystemComponent->InitStats( 
      UGameUnitAttributeSet::StaticClass(), NULL );

    You can put this call somewhere in PostInitializeComponents(), or in code that is called later than that.

  4. Once you have registered UAttributeSet, you can move on with the next recipe and apply GameplayEffect to some of the elements in the attribute set.
  5. Be sure your Actor class object implements IAbilitySystemInterface by deriving from it. This is extremely important as the UAbilitySet object will attempt a cast to IAbilitySystemInterface to call GetAbilitySystemComponent() on it at various places in the code.

How it works…

UAttributeSets simply allow you to enumerate and define attributes of different actors. GameplayEffects will be your means to make changes to the attributes of a specific actor.

There's more…

You can code in definitions of GameplayEffects, which will be things that act on the AbilitySystemComponent's AttributeSet collections. You can also write GameplayTasks for generic functions that run at specific time or events, or even in response to tag addition (GameplayTagResponseTable.cpp). You can define GameplayTags to modify GameplayAbility behavior as well as select and match gameplay units during play.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.220.106.241