{"id":89,"date":"2022-01-13T10:48:34","date_gmt":"2022-01-13T10:48:34","guid":{"rendered":"https:\/\/www.thegames.dev\/?p=89"},"modified":"2024-01-17T01:27:10","modified_gmt":"2024-01-17T01:27:10","slug":"gameplay-ability-sets","status":"publish","type":"post","link":"https:\/\/www.thegames.dev\/?p=89","title":{"rendered":"Gameplay Ability Set&#8217;s"},"content":{"rendered":"\n<p>Gameplay Ability Sets allow you define a set of abilities and effects which are given to the target ASC, I use these to apply the default generic abilities a player has, and default generic effects each player will have. These can also be used for things like perks, etc. The other benefit is, they can be added and removed via a simple handle.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"http:\/\/thegames.dev\/snaps\/UnrealEditor-Win64-DebugGame_a5skJ4RMz9.png\" alt=\"\"\/><\/figure>\n\n\n\n<p> Here is the base class:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: Relevant Code:; notranslate\" title=\"Relevant Code:\">\n\/\/ Fill out your copyright notice in the Description page of Project Settings.\n\n\n#include &quot;AbilitySystem\/Core\/KaosAbilitySet.h&quot;\n#include &quot;AbilitySystem\/Abilities\/KaosGameplayAbility.h&quot;\n#include &quot;KaosLogging.h&quot;\n#include &quot;KaosAbilitySystemComponent.h&quot;\n\n#include UE_INLINE_GENERATED_CPP_BY_NAME(KaosAbilitySet)\n\nnamespace KaosAbilitySetHandle_Impl\n{\n\tstatic int32 LastHandleId = 0;\n\tstatic int32 GetNextQueuedHandleIdForUse() { return ++LastHandleId; }\n}\n\nUKaosAbilitySet::UKaosAbilitySet(const FObjectInitializer&amp; ObjectInitializer)\n\t: Super(ObjectInitializer)\n{\n}\n\nFKaosAbilitySetHandle UKaosAbilitySet::GiveAbilitySetTo(UKaosAbilitySystemComponent* ASC, UObject* OverrideSourceObject) const\n{\n\tcheck(ASC);\n\n\tif (!ASC-&gt;IsOwnerActorAuthoritative())\n\t{\n\t\t\/\/ Must be authoritative to give or take ability sets.\n\t\treturn FKaosAbilitySetHandle();\n\t}\n\n\tFKaosAbilitySetHandle OutHandle;\n\tOutHandle.HandleId = KaosAbilitySetHandle_Impl::GetNextQueuedHandleIdForUse();\n\tOutHandle.AbilitySystemComponent = ASC;\n\t\n\t\/\/ Grant the gameplay abilities.\n\tfor (int32 AbilityIndex = 0; AbilityIndex &lt; GrantedGameplayAbilities.Num(); ++AbilityIndex)\n\t{\n\t\tconst FKaosAbilitySet_GameplayAbility&amp; AbilityToGrant = GrantedGameplayAbilities&#x5B;AbilityIndex];\n\n\t\tif (!IsValid(AbilityToGrant.Ability))\n\t\t{\n\t\t\tUE_LOG(LogKaosAbilitySystem, Error, TEXT(&quot;GrantedGameplayAbilities&#x5B;%d] on ability set &#x5B;%s] is not valid.&quot;), AbilityIndex, *GetNameSafe(this));\n\t\t\tcontinue;\n\t\t}\n\n\t\tUKaosGameplayAbility* AbilityCDO = AbilityToGrant.Ability-&gt;GetDefaultObject&lt;UKaosGameplayAbility&gt;();\n\n\t\tFGameplayAbilitySpec AbilitySpec(AbilityCDO, AbilityToGrant.AbilityLevel);\n\t\tAbilitySpec.SourceObject = OverrideSourceObject;\n\t\tAbilitySpec.DynamicAbilityTags.AddTag(AbilityToGrant.InputTag);\n\n\t\tconst FGameplayAbilitySpecHandle AbilitySpecHandle = ASC-&gt;GiveAbility(AbilitySpec);\n\t\tOutHandle.AddAbilitySpecHandle(AbilitySpecHandle);\n\t}\n\n\t\/\/ Grant the gameplay effects.\n\tfor (int32 EffectIndex = 0; EffectIndex &lt; GrantedGameplayEffects.Num(); ++EffectIndex)\n\t{\n\t\tconst FKaosAbilitySet_GameplayEffect&amp; EffectToGrant = GrantedGameplayEffects&#x5B;EffectIndex];\n\n\t\tif (!IsValid(EffectToGrant.GameplayEffect))\n\t\t{\n\t\t\tUE_LOG(LogKaosAbilitySystem, Error, TEXT(&quot;GrantedGameplayEffects&#x5B;%d] on ability set &#x5B;%s] is not valid&quot;), EffectIndex, *GetNameSafe(this));\n\t\t\tcontinue;\n\t\t}\n\n\t\tconst UGameplayEffect* GameplayEffect = EffectToGrant.GameplayEffect-&gt;GetDefaultObject&lt;UGameplayEffect&gt;();\n\t\tconst FActiveGameplayEffectHandle GameplayEffectHandle = ASC-&gt;ApplyGameplayEffectToSelf(GameplayEffect, EffectToGrant.EffectLevel, ASC-&gt;MakeEffectContext());\n\t\tOutHandle.AddGameplayEffectHandle(GameplayEffectHandle);\n\t}\n\n\t\/\/ Grant the attribute sets.\n\tfor (int32 SetIndex = 0; SetIndex &lt; GrantedAttributes.Num(); ++SetIndex)\n\t{\n\t\tconst FKaosAbilitySet_AttributeSet&amp; SetToGrant = GrantedAttributes&#x5B;SetIndex];\n\n\t\tif (!IsValid(SetToGrant.AttributeSet))\n\t\t{\n\t\t\tUE_LOG(LogKaosAbilitySystem, Error, TEXT(&quot;GrantedAttributes&#x5B;%d] on ability set &#x5B;%s] is not valid&quot;), SetIndex, *GetNameSafe(this));\n\t\t\tcontinue;\n\t\t}\n\n\t\tUAttributeSet* NewSet = NewObject&lt;UAttributeSet&gt;(ASC-&gt;GetOwner(), SetToGrant.AttributeSet);\n\t\tASC-&gt;AddAttributeSetSubobject(NewSet);\n\t\tOutHandle.AddAttributeSet(NewSet);\n\t}\n\n\treturn OutHandle;\n}\n\nFKaosAbilitySetHandle UKaosAbilitySet::GiveAbilitySetToInterface(TScriptInterface&lt;IKaosAbilitySystemInterface&gt; AbilitySystemInterface, UObject* OverrideSourceObject) const\n{\n\tUKaosAbilitySystemComponent* KaosASC = Cast&lt;UKaosAbilitySystemComponent&gt;(AbilitySystemInterface.GetObject());\n\treturn GiveAbilitySetTo(KaosASC, OverrideSourceObject);\n}\n\nvoid UKaosAbilitySet::TakeAbilitySet(FKaosAbilitySetHandle&amp; AbilitySetHandle)\n{\n\tif (!AbilitySetHandle.IsValid())\n\t{\n\t\treturn;\n\t}\n\t\n\tif (!AbilitySetHandle.AbilitySystemComponent-&gt;IsOwnerActorAuthoritative())\n\t{\n\t\t\/\/ Must be authoritative to give or take ability sets.\n\t\treturn;\n\t}\n\n\tfor (const FGameplayAbilitySpecHandle&amp; Handle : AbilitySetHandle.AbilitySpecHandles)\n\t{\n\t\tif (Handle.IsValid())\n\t\t{\n\t\t\tAbilitySetHandle.AbilitySystemComponent-&gt;ClearAbility(Handle);\n\t\t}\n\t}\n\n\tfor (const FActiveGameplayEffectHandle&amp; Handle : AbilitySetHandle.GameplayEffectHandles)\n\t{\n\t\tif (Handle.IsValid())\n\t\t{\n\t\t\tAbilitySetHandle.AbilitySystemComponent-&gt;RemoveActiveGameplayEffect(Handle);\n\t\t}\n\t}\n\n\tfor (UAttributeSet* Set : AbilitySetHandle.GrantedAttributeSets)\n\t{\n\t\tAbilitySetHandle.AbilitySystemComponent-&gt;GetSpawnedAttributes_Mutable().Remove(Set);\n\t}\n\n\tAbilitySetHandle.Reset();\n}\n<\/pre><\/div>\n\n\n<p>and the corresponding header file<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: Relevant Code:; notranslate\" title=\"Relevant Code:\">\n\/\/ Fill out your copyright notice in the Description page of Project Settings.\n\n#pragma once\n\n#include &quot;CoreMinimal.h&quot;\n#include &quot;KaosAbilitySystemInterface.h&quot;\n#include &quot;KaosAbilityTypes.h&quot;\n#include &quot;Engine\/DataAsset.h&quot;\n#include &quot;KaosAbilitySet.generated.h&quot;\n\nclass UKaosAbilitySystemComponent;\n\n\/**\n * FKaosAbilitySet_GameplayAbility\n *\n *\tData used by the ability set to grant gameplay abilities.\n *\/\nUSTRUCT(BlueprintType)\nstruct FKaosAbilitySet_GameplayAbility\n{\n\tGENERATED_BODY()\n\npublic:\n\t\/\/ Gameplay ability to grant.\n\tUPROPERTY(EditDefaultsOnly)\n\tTSubclassOf&lt;UKaosGameplayAbility&gt; Ability = nullptr;\n\n\t\/\/ Level of ability to grant.\n\tUPROPERTY(EditDefaultsOnly)\n\tint32 AbilityLevel = 1;\n};\n\n\n\/**\n * FKaosAbilitySet_GameplayEffect\n *\n *\tData used by the ability set to grant gameplay effects.\n *\/\nUSTRUCT(BlueprintType)\nstruct FKaosAbilitySet_GameplayEffect\n{\n\tGENERATED_BODY()\n\npublic:\n\t\/\/ Gameplay effect to grant.\n\tUPROPERTY(EditDefaultsOnly)\n\tTSubclassOf&lt;UGameplayEffect&gt; GameplayEffect = nullptr;\n\n\t\/\/ Level of gameplay effect to grant.\n\tUPROPERTY(EditDefaultsOnly)\n\tfloat EffectLevel = 1.0f;\n};\n\n\/**\n * FKaosAbilitySetHandle\n *\n *\tData used to store handles to what has been granted by the ability set.\n *\/\nUSTRUCT(BlueprintType)\nstruct FKaosAbilitySetHandle\n{\n\tGENERATED_BODY()\n\n\tbool IsValid() const\n\t{\n\t\treturn AbilitySystemComponent.IsValid() &amp;&amp; HandleId != 0;\n\t}\n\nprivate:\n\tfriend class UKaosAbilitySet;\n\n\tvoid AddAbilitySpecHandle(const FGameplayAbilitySpecHandle&amp; Handle);\n\tvoid AddGameplayEffectHandle(const FActiveGameplayEffectHandle&amp; Handle);\n\n\t\/\/ Handles to the granted abilities.\n\tUPROPERTY()\n\tTArray&lt;FGameplayAbilitySpecHandle&gt; AbilitySpecHandles;\n\n\t\/\/ Handles to the granted gameplay effects.\n\tUPROPERTY()\n\tTArray&lt;FActiveGameplayEffectHandle&gt; GameplayEffectHandles;\n\n\tint32 HandleId = 0;\n\n\tTWeakObjectPtr&lt;UKaosAbilitySystemComponent&gt; AbilitySystemComponent = nullptr;\n\n\tvoid Reset()\n\t{\n\t\tAbilitySpecHandles.Reset();\n\t\tGameplayEffectHandles.Reset();\n\t\tAbilitySystemComponent.Reset();\n\t\tHandleId = 0;\n\t}\n};\n\n\/**\n * \n *\/\nUCLASS(BlueprintType, Const)\nclass KAOSGAME_API UKaosAbilitySet : public UPrimaryDataAsset\n{\n\tGENERATED_BODY()\npublic:\n\n\tUKaosAbilitySet(const FObjectInitializer&amp; ObjectInitializer = FObjectInitializer::Get());\n\n\tconst TArray&lt;FKaosAbilitySet_GameplayAbility&gt;&amp; GetGrantedGameplayAbilities() const { return GrantedGameplayAbilities; }\n\tconst TArray&lt;FKaosAbilitySet_GameplayEffect&gt;&amp; GetGrantedGameplayEffects() const { return GrantedGameplayEffects; }\n\tconst TArray&lt;FKaosAbilitySet_AttributeSet&gt;&amp; GetGrantedAttributes() const { return GrantedAttributes; }\n\n\tFKaosAbilitySetHandle GiveAbilitySetTo(UKaosAbilitySystemComponent* ASC, UObject* OverrideSourceObject = nullptr) const;\n\tFKaosAbilitySetHandle GiveAbilitySetToInterface(TScriptInterface&lt;IKaosAbilitySystemInterface&gt; AbilitySystemInterface, UObject* OverrideSourceObject = nullptr) const;\n\tstatic void TakeAbilitySet(FKaosAbilitySetHandle&amp; AbilitySetHandle);\nprotected:\n\n\t\/\/ Gameplay abilities to grant when this ability set is granted.\n\tUPROPERTY(EditDefaultsOnly, Category = &quot;Gameplay Abilities&quot;, meta=(TitleProperty=Ability))\n\tTArray&lt;FKaosAbilitySet_GameplayAbility&gt; GrantedGameplayAbilities;\n\n\t\/\/ Gameplay effects to grant when this ability set is granted.\n\tUPROPERTY(EditDefaultsOnly, Category = &quot;Gameplay Effects&quot;, meta=(TitleProperty=GameplayEffect))\n\tTArray&lt;FKaosAbilitySet_GameplayEffect&gt; GrantedGameplayEffects;\n\n\t\/\/ Attribute sets to grant when this ability set is granted.\n\tUPROPERTY(EditDefaultsOnly, Category = &quot;Attribute Sets&quot;, meta=(TitleProperty=AttributeSet))\n\tTArray&lt;FKaosAbilitySet_AttributeSet&gt; GrantedAttributes;\n};\n<\/pre><\/div>\n\n\n<p>The Ability Sets can be granted by issuing the AbilitySet-&gt;GiveAbilitySetTo or you can make some BP statics to do that like exampled below:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: Relevant Code:; notranslate\" title=\"Relevant Code:\">\nvoid UKaosAbilityStatics::UnequipKaosAbilitySet(FKaosAbilitySetHandle&amp; AbilitySetHandle)\n{\n\tif (AbilitySetHandle.IsValid())\n\t{\n\t\tUKaosAbilitySet::TakeAbilitySet(AbilitySetHandle);\n\t}\n}\n\nFKaosAbilitySetHandle UKaosAbilityStatics::EquipKaosAbilitySet(TScriptInterface&lt;IAbilitySystemInterface&gt; AbilitySystemInterface, const UKaosAbilitySet* AbilitySet, UObject* OverrideSourceObject)\n{\n\treturn AbilitySet-&gt;GiveAbilitySetToInterface(AbilitySystemInterface, OverrideSourceObject);\n}\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>Gameplay Ability Sets allow you define a set of abilities and effects which are given to the target ASC, I use these to apply the default generic abilities a player has, and default generic effects each player will have. These can also be used for things like perks, etc. The other benefit is, they can <a href=\"https:\/\/www.thegames.dev\/?p=89\" class=\"more-link\">&#8230;<span class=\"screen-reader-text\">  Gameplay Ability Set&#8217;s<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,2],"tags":[],"class_list":["post-89","post","type-post","status-publish","format-standard","hentry","category-gameplay-ability-system","category-unreal-engine"],"rise-blocks_total_comments":0,"rise-blocks_categories":[{"term_id":4,"name":"Gameplay Ability System","slug":"gameplay-ability-system","term_group":0,"term_taxonomy_id":4,"taxonomy":"category","description":"Unreal Engine's Gameplay Ability System (GAS)","parent":2,"count":16,"filter":"raw","cat_ID":4,"category_count":16,"category_description":"Unreal Engine's Gameplay Ability System (GAS)","cat_name":"Gameplay Ability System","category_nicename":"gameplay-ability-system","category_parent":2},{"term_id":2,"name":"Unreal Engine","slug":"unreal-engine","term_group":0,"term_taxonomy_id":2,"taxonomy":"category","description":"All unreal engine related tutorials, tips and tricks.","parent":0,"count":5,"filter":"raw","cat_ID":2,"category_count":5,"category_description":"All unreal engine related tutorials, tips and tricks.","cat_name":"Unreal Engine","category_nicename":"unreal-engine","category_parent":0}],"rise-blocks_excerpt":"Gameplay Ability Sets allow you define a set of abilities and effects which are given to the target ASC, I use these to apply the default generic abilities a player has, and default generic effects each player will have. These can also be used for things like perks, etc. The other benefit is, they can be added and removed via..","_links":{"self":[{"href":"https:\/\/www.thegames.dev\/index.php?rest_route=\/wp\/v2\/posts\/89","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.thegames.dev\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.thegames.dev\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.thegames.dev\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.thegames.dev\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=89"}],"version-history":[{"count":8,"href":"https:\/\/www.thegames.dev\/index.php?rest_route=\/wp\/v2\/posts\/89\/revisions"}],"predecessor-version":[{"id":220,"href":"https:\/\/www.thegames.dev\/index.php?rest_route=\/wp\/v2\/posts\/89\/revisions\/220"}],"wp:attachment":[{"href":"https:\/\/www.thegames.dev\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=89"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.thegames.dev\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=89"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.thegames.dev\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=89"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}