Overview
GGA also has another code module called GenericGameplayAttribute, which is responsible for expanding the GameplayAttribute part of the GAS system and includes some built-in GameplayAttributes, also explained how the AttributeSet code generation works.
This module is very lightweight.
Attribute and Tag Mapping
GAS very much embraces GameplayTags, however, GameplayAttribute is not driven by GameplayTag, for this reason, GGA provides an optional mapping of GameplayAttribute to GameplayTag.
You can get Attribute through Tag and you can get Tag through Attribute.
You can also register Attribute to a Tag at runtime (this operation is global).
Built-in Attributes
The GenericGameplayAttributes module comes with three commonly used attribute sets, AS_Health, AS_Mana, and AS_Stamina.
You can use it or not. It is abstract enough and is automatically generated (see below).
How to design an AttributeSet
If you refer to various examples and tutorials on the web, you will find that people are used to using one AttributeSet to define all the attributes used in the game, and they will write a lot of gameplay logic in the AttributeSet, which I don't think is desirable.
Instead, I think AttributeSet should be divided according to the function in the game, and just play the role of data definition. It doesn't need to contain gameplay logic, and only needs to be processed externally according to the logic of attribute changes.
Assuming your game requires two attributes, Health, and Stamina, you should define two AttributeSets.
AS_Health(attributes related to health)
Health | Current value |
MaxHealth | Max value |
Healing | Value to add |
Damage | Value to reduce |
AS_Stamina(attributes related to stamina)
Stamina | Current value |
MaxStamina | Max value |
Healing | Value to add |
Damage | Value to reduce |
The advantages of doing this are:
- In multiplayer games, different characters may need different attributes. In order to avoid a lot of network replication costs, you should only give the set of attributes needed by the characters that need it.
- At the same time, this modular design can go far in the future. (For example, if you use GameFeature, and one of your subFeatures defines its dedicated set of attributes)
Don't abuse GameplayAttributes. Here's a great post that teaches you how to use a small number of attributes and accomplish complex tasks.
About Code Generation
GGA provides an code generation funationality that allows you to generate C++ AttributeSet codes.
Even for C++ users, adding new attributes can be a very tedious process that requires a lot of code to be written by hand.
In order to use code generation, you must install visual studio on windows or XCode on your mac by following official documentation, and then convert your project into a C++ project.
Code Generation Guide
GGA provides code generation functionality to help you generate C++ AttributeSet code. Even for C++ users, adding new attributes is a very tedious process, requiring a lot of boilerplate code to be written manually. To use the code generation feature, you need to install Visual Studio on Windows or Xcode on Mac (please refer to the official documentation) and convert your project into a code project.
Steps to add a new attribute set:
- Create a new attribute set definition asset: Right-click in the blank area of the project browser -> GenericGameplayAbilities -> CoreConfig -> AttributeSetDefinition.
- Name the new attribute set asset: It is recommended to name the new attribute set definition with the prefix ASDefinition_, such as ASDefinition_Health|Combat|Stamina.
- Configure code generation: Specify which project module and path the code will be generated in, the code file name, the attributes it must have, etc. You can also select a threshold template from the template drop-down list to quickly fill in the configuration.
- Click the Build/Rebuild button. After the code is generated, you need to recompile the project.
Note
You can add other allowed code subpaths in Project Settings -> GGA Attribute Set Generator settings, such as "MyFolder/Attributes". By default, only the two folders, module name/Attributes|AttributeSets, are allowed.
Generated Code
Here is an example of generating gameplay attribute code from json.
Above AttributeSetDefinition generates the following code:
// Copyright 2025 https://yuewu.dev/en All Rights Reserved.
#pragma once
#include "CoreMinimal.h"
#include "AttributeSet.h"
#include "AbilitySystemComponent.h"
#include "NativeGameplayTags.h"
#include "AS_Combat.generated.h"
namespace AS_Combat
{
GENERICGAMEPLAYATTRIBUTES_API UE_DECLARE_GAMEPLAY_TAG_EXTERN(Damage)
GENERICGAMEPLAYATTRIBUTES_API UE_DECLARE_GAMEPLAY_TAG_EXTERN(DamageNegation)
GENERICGAMEPLAYATTRIBUTES_API UE_DECLARE_GAMEPLAY_TAG_EXTERN(GuardDamageNegation)
GENERICGAMEPLAYATTRIBUTES_API UE_DECLARE_GAMEPLAY_TAG_EXTERN(StaminaDamage)
GENERICGAMEPLAYATTRIBUTES_API UE_DECLARE_GAMEPLAY_TAG_EXTERN(StaminaDamageNegation)
}
#define ATTRIBUTE_ACCESSORS(ClassName, PropertyName) \
GAMEPLAYATTRIBUTE_PROPERTY_GETTER(ClassName, PropertyName) \
GAMEPLAYATTRIBUTE_VALUE_GETTER(PropertyName) \
GAMEPLAYATTRIBUTE_VALUE_SETTER(PropertyName) \
GAMEPLAYATTRIBUTE_VALUE_INITTER(PropertyName)
UCLASS()
class GENERICGAMEPLAYATTRIBUTES_API UAS_Combat : public UAttributeSet
{
GENERATED_BODY()
public:
UAS_Combat();
virtual void GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const override;
virtual void PreAttributeChange(const FGameplayAttribute& Attribute, float& NewValue) override;
virtual void PostAttributeChange(const FGameplayAttribute& Attribute, float OldValue, float NewValue) override;
virtual bool PreGameplayEffectExecute(struct FGameplayEffectModCallbackData& Data) override;
virtual void PostGameplayEffectExecute(const FGameplayEffectModCallbackData& Data) override;
// The damage that will apply to target
UPROPERTY(BlueprintReadOnly, ReplicatedUsing = OnRep_Damage, Category = "Attribute|CombatSet", Meta = (AllowPrivateAccess = true))
FGameplayAttributeData Damage{ 0.0 };
ATTRIBUTE_ACCESSORS(ThisClass, Damage)
// The damage reduction(percentage) for incoming health damage
UPROPERTY(BlueprintReadOnly, ReplicatedUsing = OnRep_DamageNegation, Category = "Attribute|CombatSet", Meta = (AllowPrivateAccess = true))
FGameplayAttributeData DamageNegation{ 0.0 };
ATTRIBUTE_ACCESSORS(ThisClass, DamageNegation)
// The damage reduction(percentage) for incoming health damage while guarding
UPROPERTY(BlueprintReadOnly, ReplicatedUsing = OnRep_GuardDamageNegation, Category = "Attribute|CombatSet", Meta = (AllowPrivateAccess = true))
FGameplayAttributeData GuardDamageNegation{ 0.0 };
ATTRIBUTE_ACCESSORS(ThisClass, GuardDamageNegation)
// The stamina damage that will apply to target
UPROPERTY(BlueprintReadOnly, ReplicatedUsing = OnRep_StaminaDamage, Category = "Attribute|CombatSet", Meta = (AllowPrivateAccess = true))
FGameplayAttributeData StaminaDamage{ 0.0 };
ATTRIBUTE_ACCESSORS(ThisClass, StaminaDamage)
// The damage reduction(percentage) for incoming stamina damage
UPROPERTY(BlueprintReadOnly, ReplicatedUsing = OnRep_StaminaDamageNegation, Category = "Attribute|CombatSet", Meta = (AllowPrivateAccess = true))
FGameplayAttributeData StaminaDamageNegation{ 0.0 };
ATTRIBUTE_ACCESSORS(ThisClass, StaminaDamageNegation)
UFUNCTION(BlueprintCallable,BlueprintPure,meta=(DisplayName="GetDamageAttribute"), Category = "Attribute|CombatSet")
static FGameplayAttribute Bp_GetDamageAttribute();
UFUNCTION(BlueprintPure,meta=(DisplayName="GetDamage"), Category = "Attribute|CombatSet")
float Bp_GetDamage() const;
UFUNCTION(BlueprintCallable,meta=(DisplayName="SetDamage"), Category = "Attribute|CombatSet")
void Bp_SetDamage(float NewValue);
UFUNCTION(BlueprintCallable,meta=(DisplayName="InitDamage"), Category = "Attribute|CombatSet")
void Bp_InitDamage(float NewValue);
UFUNCTION(BlueprintCallable,BlueprintPure,meta=(DisplayName="GetDamageNegationAttribute"), Category = "Attribute|CombatSet")
static FGameplayAttribute Bp_GetDamageNegationAttribute();
UFUNCTION(BlueprintPure,meta=(DisplayName="GetDamageNegation"), Category = "Attribute|CombatSet")
float Bp_GetDamageNegation() const;
UFUNCTION(BlueprintCallable,meta=(DisplayName="SetDamageNegation"), Category = "Attribute|CombatSet")
void Bp_SetDamageNegation(float NewValue);
UFUNCTION(BlueprintCallable,meta=(DisplayName="InitDamageNegation"), Category = "Attribute|CombatSet")
void Bp_InitDamageNegation(float NewValue);
UFUNCTION(BlueprintCallable,BlueprintPure,meta=(DisplayName="GetGuardDamageNegationAttribute"), Category = "Attribute|CombatSet")
static FGameplayAttribute Bp_GetGuardDamageNegationAttribute();
UFUNCTION(BlueprintPure,meta=(DisplayName="GetGuardDamageNegation"), Category = "Attribute|CombatSet")
float Bp_GetGuardDamageNegation() const;
UFUNCTION(BlueprintCallable,meta=(DisplayName="SetGuardDamageNegation"), Category = "Attribute|CombatSet")
void Bp_SetGuardDamageNegation(float NewValue);
UFUNCTION(BlueprintCallable,meta=(DisplayName="InitGuardDamageNegation"), Category = "Attribute|CombatSet")
void Bp_InitGuardDamageNegation(float NewValue);
UFUNCTION(BlueprintCallable,BlueprintPure,meta=(DisplayName="GetStaminaDamageAttribute"), Category = "Attribute|CombatSet")
static FGameplayAttribute Bp_GetStaminaDamageAttribute();
UFUNCTION(BlueprintPure,meta=(DisplayName="GetStaminaDamage"), Category = "Attribute|CombatSet")
float Bp_GetStaminaDamage() const;
UFUNCTION(BlueprintCallable,meta=(DisplayName="SetStaminaDamage"), Category = "Attribute|CombatSet")
void Bp_SetStaminaDamage(float NewValue);
UFUNCTION(BlueprintCallable,meta=(DisplayName="InitStaminaDamage"), Category = "Attribute|CombatSet")
void Bp_InitStaminaDamage(float NewValue);
UFUNCTION(BlueprintCallable,BlueprintPure,meta=(DisplayName="GetStaminaDamageNegationAttribute"), Category = "Attribute|CombatSet")
static FGameplayAttribute Bp_GetStaminaDamageNegationAttribute();
UFUNCTION(BlueprintPure,meta=(DisplayName="GetStaminaDamageNegation"), Category = "Attribute|CombatSet")
float Bp_GetStaminaDamageNegation() const;
UFUNCTION(BlueprintCallable,meta=(DisplayName="SetStaminaDamageNegation"), Category = "Attribute|CombatSet")
void Bp_SetStaminaDamageNegation(float NewValue);
UFUNCTION(BlueprintCallable,meta=(DisplayName="InitStaminaDamageNegation"), Category = "Attribute|CombatSet")
void Bp_InitStaminaDamageNegation(float NewValue);
protected:
/** Helper function to proportionally adjust the value of an attribute when it's associated max attribute changes. (i.e. When MaxHealth increases, Health increases by an amount that maintains the same percentage as before) */
virtual void AdjustAttributeForMaxChange(FGameplayAttributeData& AffectedAttribute, const FGameplayAttributeData& MaxAttribute, float NewMaxValue, const FGameplayAttribute& AffectedAttributeProperty);
UFUNCTION()
virtual void OnRep_Damage(const FGameplayAttributeData& OldValue);
UFUNCTION()
virtual void OnRep_DamageNegation(const FGameplayAttributeData& OldValue);
UFUNCTION()
virtual void OnRep_GuardDamageNegation(const FGameplayAttributeData& OldValue);
UFUNCTION()
virtual void OnRep_StaminaDamage(const FGameplayAttributeData& OldValue);
UFUNCTION()
virtual void OnRep_StaminaDamageNegation(const FGameplayAttributeData& OldValue);
};As you can see, just a simple set of attributes requires so much code. So code generation is given!
Generated Blueprints
Some thoughts
Since the attribute system of GAS has to be done in C++, this has hindered a large portion of blueprint users, while various solutions for blueprint users have appeared in Unreal Marketplace. Even a pure blueprint GameplayAttribute solution appeared.
However, I think that if you take a big detour to use GameplayAttribute with all kinds of hacks just because you have a fear of code projects, I think you are putting the cart before the horse.
What if in the future Unreal comes with blueprint support for GameplayAttribute?
What if in the future the Unreal team makes it so that GAS no longer requires C++?
Of course, this is just my own personal opinion. If you're going to develop serious games, you're always going to be faced with C++.