In this lesson, you will learn how to encapsulate character statistics like Health and Stamina into dedicated UAttributeSet classes to serve as the data backbone for your Gameplay Ability System (GAS).
Understanding Attribute Sets
In GAS, an AttributeSet acts as a centralized container for your game’s numerical data. Instead of scattering variables across your Character or PlayerState classes, you define them here so the Ability System can track them, replicate them over a network, and manipulate them through Gameplay Effects.
Every attribute inside this set is defined using the FGameplayAttributeData struct. This structure is essential because it stores two values for each attribute: the Base Value (the raw stat) and the Current Value (the stat after all modifiers like buffs or debuffs are applied).
Defining Your Attributes
To create an attribute, you define it in your C++ header file using the GAMEPLAYATTRIBUTE_PROPERTY_GETTER macros. These macros allow the engine to find and access your variables dynamically.
UCLASS()
class MYGAME_API UMyAttributeSet : public UAttributeSet
{
GENERATED_BODY()
public:
// Define Health
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Attributes")
FGameplayAttributeData Health;
GAMEPLAYATTRIBUTE_PROPERTY_GETTER(UMyAttributeSet, Health);
GAMEPLAYATTRIBUTE_VALUE_GETTER(Health);
GAMEPLAYATTRIBUTE_VALUE_SETTER(Health);
GAMEPLAYATTRIBUTE_VALUE_INITTER(Health);
};
Why Use Attribute Sets?
When you define your stats this way, you gain three immediate benefits:
- Clamping: You can override the
PreAttributeChangefunction in yourAttributeSetto automatically clamp values (e.g., ensuringHealthnever drops below 0 or exceedsMaxHealth). - Replication: By overriding
GetLifetimeReplicatedProps, you ensure that attribute changes are automatically synchronized from the server to clients. - Interaction: Because these are formal attributes, Gameplay Effects can target them automatically, allowing you to create complex damage or healing logic without writing custom boilerplate code for every single interaction.
By moving your stats into an AttributeSet, you stop managing simple variables and start managing a robust, systemic data layer that communicates natively with the rest of the Gameplay Ability System.
Key Takeaway: Attribute Sets provide a structured, thread-safe, and networked way to manage character stats, allowing the Gameplay Ability System to automatically calculate, clamp, and replicate your data across the game.