Gaplay Abities -> AttributeSet -> ATTRIBUTE_ACCESSORS

I downloaded ActionRPG project and in RPGAttributeSet.h i see:

// Uses macros from AttributeSet.h
#define ATTRIBUTE_ACCESSORS(ClassName, PropertyName) \
	GAMEPLAYATTRIBUTE_PROPERTY_GETTER(ClassName, PropertyName) \
	GAMEPLAYATTRIBUTE_VALUE_GETTER(PropertyName) \
	GAMEPLAYATTRIBUTE_VALUE_SETTER(PropertyName) \
	GAMEPLAYATTRIBUTE_VALUE_INITTER(PropertyName)
..
..
..
	/** Current Health, when 0 we expect owner to die. Capped by MaxHealth */
	UPROPERTY(BlueprintReadOnly, Category = "Health", ReplicatedUsing=OnRep_Health)
	FGameplayAttributeData Health;
	ATTRIBUTE_ACCESSORS(URPGAttributeSet, Health)

Question:
What ATTRIBUTE_ACCESSORS do ???

1 Like

This is a MACRO that automatically generate all the boilerplate code for the property without you having to type out everything. Here is the comments for the MACRO as per unreal.

/**
 * This defines a set of helper functions for accessing and initializing attributes, to avoid having to manually write these functions.
 * It would creates the following functions, for attribute Health
 *
 *	static FGameplayAttribute UMyHealthSet::GetHealthAttribute();
 *	FORCEINLINE float UMyHealthSet::GetHealth() const;
 *	FORCEINLINE void UMyHealthSet::SetHealth(float NewVal);
 *	FORCEINLINE void UMyHealthSet::InitHealth(float NewVal);
 *
 * To use this in your game you can define something like this, and then add game-specific functions as necessary:
 *
 *	#define ATTRIBUTE_ACCESSORS(ClassName, PropertyName) \
 *	GAMEPLAYATTRIBUTE_PROPERTY_GETTER(ClassName, PropertyName) \
 *	GAMEPLAYATTRIBUTE_VALUE_GETTER(PropertyName) \
 *	GAMEPLAYATTRIBUTE_VALUE_SETTER(PropertyName) \
 *	GAMEPLAYATTRIBUTE_VALUE_INITTER(PropertyName)
 *
 *	ATTRIBUTE_ACCESSORS(UMyHealthSet, Health)
 */

So as you can see if you type “ATTRIBUTE_ACCESSORS(UMyHealthSet, Health)”, the MACRO will be replaced during compiling with following code.:

static FGameplayAttribute UMyHealthSet::GetHealthAttribute()	
{ 
	static UProperty* Prop = FindFieldChecked<UProperty>(UMyHealthSet::StaticClass(), 
        GET_MEMBER_NAME_CHECKED(UMyHealthSet, Health));
		return Prop;
	}
FORCEINLINE float UMyHealthSet::GetHealth() const
{ 
	return Health.GetCurrentValue();
}
FORCEINLINE void UMyHealthSet::SetHealth(float NewVal)
{ 
	UAbilitySystemComponent* AbilityComp = GetOwningAbilitySystemComponent(); 
	if (ensure(AbilityComp)) 
	{ 
		AbilityComp->SetNumericAttributeBase(GetHealthAttribute(), NewVal); 
	}; 
}
FORCEINLINE void UMyHealthSet::InitHealth(float NewVal)
{ 
	Health.SetBaseValue(NewVal); 
	Health.SetCurrentValue(NewVal); 
}
2 Likes