UAttributeSet
#include "AttributeSet.h" Description
The base class for defining numeric attributes (health, stamina, damage) that GAS effects can read and modify. Subclass this and declare FGameplayAttributeData properties.
Caveats & Gotchas
- • Use the ATTRIBUTE_ACCESSORS macro to generate GetHealth/SetHealth/InitHealth helpers — writing these by hand is error-prone.
- • PreAttributeChange and PostAttributeChange are your hooks to clamp values. Clamping in PreAttributeChange is preferred to avoid out-of-range reads, but the value is already applied by the time PostAttributeChange fires.
- • Attributes are replicated individually — mark them with UPROPERTY(Replicated) and implement GetLifetimeReplicatedProps. Forgetting replication means clients see stale values.
Example
Minimal attribute set with health C++
UCLASS()
class UMyAttributeSet : public UAttributeSet
{
GENERATED_BODY()
public:
UPROPERTY(BlueprintReadOnly, ReplicatedUsing=OnRep_Health, Category="Attributes")
FGameplayAttributeData Health;
ATTRIBUTE_ACCESSORS(UMyAttributeSet, Health)
virtual void PreAttributeChange(const FGameplayAttribute& Attribute, float& NewValue) override
{
if (Attribute == GetHealthAttribute())
NewValue = FMath::Clamp(NewValue, 0.f, GetMaxHealth());
}
}; Tags
Version History
Introduced in: unknown
| Version | Status | Notes |
|---|---|---|
| 5.6 | stable | — |
Feedback
Was this helpful?