1.在CCAttributeSet.h中定义宏函数:
// * 要在游戏中使用它,您可以定义类似这样的内容,然后根据需要添加特定于游戏的功能:
#define ATTRIBUTE_ACCESSORS(ClassName, PropertyName) \
GAMEPLAYATTRIBUTE_PROPERTY_GETTER(ClassName, PropertyName) \
GAMEPLAYATTRIBUTE_VALUE_GETTER(PropertyName) \
GAMEPLAYATTRIBUTE_VALUE_SETTER(PropertyName) \
GAMEPLAYATTRIBUTE_VALUE_INITTER(PropertyName)
宏组成结构:
GAMEPLAYATTRIBUTE_PROPERTY_GETTER:生成属性名的静态获取方法GAMEPLAYATTRIBUTE_VALUE_GETTER:生成属性值的实例获取方法GAMEPLAYATTRIBUTE_VALUE_SETTER:生成属性值的实例设置方法GAMEPLAYATTRIBUTE_VALUE_INITTER:生成属性值的初始化方法
典型应用场景:
- 配合
FGameplayAttributeData类型属性使用 - 自动生成如
GetHealth()/SetHealth()等标准访问方法 - 常用于
UAttributeSet子类中定义角色属性
实现优势:
- 减少重复代码量约75%(每个属性节省4个标准方法)
- 保持方法命名一致性,符合GAS规范
- 支持蓝图调用和网络同步
2.在属性后面使用:
UPROPERTY(BlueprintReadOnly, Category="Vital Attributes", ReplicatedUsing = OnRep_Health)
FGameplayAttributeData Health;
ATTRIBUTE_ACCESSORS(UCCAttributeSet, Health);
需要添加头文件:“#include "AbilitySystemComponent.h"”
3.构造函数:
UCCAttributeSet::UCCAttributeSet()
{
InitHealth(100.f);
InitMaxHealth(100.f);
InitMana(50.f);
InitMaxMana(50.f);
}
运行调试模式,按一下~,输入showdebug abilitysystem。看看:
![]()

完美!
3540

被折叠的 条评论
为什么被折叠?



