{"id":3130,"date":"2021-01-02T14:09:03","date_gmt":"2021-01-02T06:09:03","guid":{"rendered":"http:\/\/blog.coolcoding.cn\/?p=3130"},"modified":"2021-01-02T19:42:18","modified_gmt":"2021-01-02T11:42:18","slug":"gameplayabilities-api-implementing-stats-with-uattributeset","status":"publish","type":"post","link":"https:\/\/blog.coolcoding.cn\/?p=3130","title":{"rendered":"GameplayAbilities API &#8211; Implementing stats with UAttributeSet"},"content":{"rendered":"\n<p> The\u00a0GameplayAbilities\u00a0API allows you to associate a set of attributes, that is,\u00a0UAttributeSet, to an Actor.\u00a0UAttributeSet\u00a0describes properties appropriate for that Actor&#8217;s in-game attributes, such as\u00a0Hp,\u00a0Mana,\u00a0Speed,\u00a0Armor,\u00a0AttackDamage, 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. <\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Getting ready<\/h1>\n\n\n\n<p>AbilitySystemComponent&nbsp;is the first thing you will need to add to your actors to equip them to use the&nbsp;GameplayAbilities&nbsp;API and&nbsp;UAttributeSet&nbsp;classes. To define your custom&nbsp;UAttributeSet, you will simply derive from the&nbsp;UAttributeSet&nbsp;base class and extend the base class with your own series of&nbsp;UPROPERTY&nbsp;members. After that, you must register your custom&nbsp;AttributeSet&nbsp;with your&nbsp;Actor&nbsp;class&#8217;s&nbsp;AbilitySystemComponent.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">How to do it&#8230;<\/h1>\n\n\n\n<ol><li>If you have not done so already, complete Steps 1-4 of the&nbsp;<em>GameplayAbilities API \u2013 triggering an actor&#8217;s gameplay abilities with game controls<\/em>&nbsp;recipe to link to the&nbsp;GameplayAbilities&nbsp;API in your&nbsp;ProjectName.Build.cs&nbsp;file and enable its functionality.<\/li><li>Create a new C++ class&nbsp; by going to the&nbsp;Content Browser&nbsp;and selecting&nbsp;Add New | Add C++ Class. From the&nbsp;Add C++ Class&nbsp;menu, check the&nbsp;Show All Classes&nbsp;option. From there, type in&nbsp;attr&nbsp;and select&nbsp;AttributeSet&nbsp;as your parent class. From there, click the&nbsp;Next&nbsp;button:<\/li><\/ol>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"http:\/\/blog.coolcoding.cn\/wp-content\/uploads\/2021\/01\/98d22785-cb67-4d52-b392-4c4424e5a76e.png\" alt=\"\"\/><\/figure>\n\n\n\n<ol><li>Give the class a&nbsp;Name&nbsp;of&nbsp;GameUnitAttributeSet&nbsp;and click on&nbsp;Create Class:<\/li><\/ol>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"http:\/\/blog.coolcoding.cn\/wp-content\/uploads\/2021\/01\/df7f58db-1f4a-46d1-a5e5-3c5e43478d3d.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>Once created, deck the class out with a set of&nbsp;UPROPERTY&nbsp;specifiers that you want each Actor to have in their property set.<\/p>\n\n\n\n<ol><li>For example, you might want to declare your&nbsp;UAttributeSet&nbsp;derivate class similar to&nbsp;what&#8217;s given in the following piece of code:<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-preformatted\">#pragma once<br><br>#include \"CoreMinimal.h\"<br>#include \"AttributeSet.h\"<br>#include \"GameUnitAttributeSet.generated.h\"<br><br>\/**<br> * <br> *\/<br>UCLASS(<strong>Blueprintable, BlueprintType<\/strong>)<br>class CHAPTER_11_API UGameUnitAttributeSet : public UAttributeSet<br>{<br>    GENERATED_BODY()<br>    <br><strong>public:<\/strong><br><strong>    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = GameUnitAttributes)<\/strong><br><strong>    float Hp;<\/strong><br><br><strong>    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = GameUnitAttributes)<\/strong><br><strong>    float Mana;<\/strong><br><br><strong>    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = GameUnitAttributes)<\/strong><br><strong>    float Speed;<\/strong><br><strong>};<\/strong><br><br><\/pre>\n\n\n\n<p>If your code is networked, you might want to enable replication on each of the&nbsp;UPROPERTY&nbsp;specifiers&nbsp;with the replicated declaration in the&nbsp;UPROPERTY&nbsp;macro.<\/p>\n\n\n\n<ol><li>Connect&nbsp;GameUnitAttributeSet&nbsp;with your&nbsp;AbilitySystemComponent&nbsp;inside your&nbsp;Actor&nbsp;class. We can do this with the&nbsp;Warrior&nbsp;class we created previously by opening the&nbsp;Warrior.h&nbsp;file and adding the following function declaration:<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-preformatted\">virtual void PostInitializeComponents() override;<\/pre>\n\n\n\n<ol><li>Then, open&nbsp;Warrior.cpp&nbsp;and add the following&nbsp;#include:<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-preformatted\">#include \"GameUnitAttributeSet.h\"<\/pre>\n\n\n\n<ol><li>Afterwards, implement that function:<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-preformatted\">void AWarrior::PostInitializeComponents()<br>{<br>    Super::PostInitializeComponents();<br><br>    if(AbilitySystemComponent)<br>    {<br>        AbilitySystemComponent-&gt;InitStats(UGameUnitAttributeSet::StaticClass(), NULL);<br>    }<br>}<\/pre>\n\n\n\n<p>You can put this call somewhere in&nbsp;PostInitializeComponents(), or in code that is called later than that.<\/p>\n\n\n\n<ol><li>Once you have registered&nbsp;UAttributeSet, you can move on with the next recipe and apply&nbsp;GameplayEffect&nbsp;to some of the elements in the attribute set.<\/li><li>Be sure your&nbsp;Actor&nbsp;class object implements&nbsp;IAbilitySystemInterface&nbsp;by deriving from it. This is extremely important as the&nbsp;UAbilitySet&nbsp;object will attempt a cast to&nbsp;IAbilitySystemInterface&nbsp;to call&nbsp;GetAbilitySystemComponent()&nbsp;on it at various places in the code.<\/li><\/ol>\n\n\n\n<h1 class=\"wp-block-heading\">How it works&#8230;<\/h1>\n\n\n\n<p>UAttributeSets&nbsp;simply allow you to enumerate and define attributes of different actors.&nbsp;GameplayEffects&nbsp;will be your means to make changes to the attributes of a specific actor.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">There&#8217;s more&#8230;<\/h1>\n\n\n\n<p>You can code in definitions of&nbsp;GameplayEffects, which will be things that act on the&nbsp;AbilitySystemComponent&#8217;s&nbsp;AttributeSet&nbsp;collections. You can also write&nbsp;GameplayTasks&nbsp;for generic functions that run at specific times or follow particular events, or even in response to a tag addition (GameplayTagResponseTable.cpp). You can define&nbsp;GameplayTags&nbsp;to modify&nbsp;GameplayAbility&nbsp;behavior, as well as select and match gameplay units during play.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The\u00a0GameplayAbilities\u00a0API allows you to associate a set [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27,1],"tags":[26],"_links":{"self":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/3130"}],"collection":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3130"}],"version-history":[{"count":1,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/3130\/revisions"}],"predecessor-version":[{"id":3142,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/3130\/revisions\/3142"}],"wp:attachment":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3130"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3130"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3130"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}