大家好,我是刘茗。
ActionRPG示例项目是UE4中一个展示GameplayAbilitySystem的项目。
使用技能系统的第一步,那就是启用 GameplayAbilities 插件并创建属性集类
下文GAS为GameplayAbilitySystem的简称
一、GAS插件启动
目前GAS是作为UE4的一个引擎插件使用的,所以要先去启动。
打开插件设置,找到GameplayAbilities插件启动
二、代码文件结构
三、按键绑定(PC端)
普攻:左键
技能:F键(进入坦克)
翻滚:右键
切换武器:鼠标滑轮
道具栏:Tab键
自动挂机:回车
暂停:ESC键
使用道具:R键
道具栏界面,点击右上角的加号可以增加Souls用来购买技能和装备。
使用GAS需要添加UAbilitySystemComponent,在ActionRPG.Build.cs文件中添加了GAS模块
项目中使用的是派生类URPGAbilitySystemComponent
URPGAbilitySystemComponent在ARPGCharacterBase的构造函数中添加:
四、创建属性集
因为此系统仅在C++中支持,因为属性集必须是UAttributeSet的C++子类。
在ARPG项目中,定义了如下属性:
生命、法力、攻击力、防御力、暴击、移动速度等。
这里使用的数据结构是FGameplayAttributeData,它内部存储两个数据:基数值和当前值。
获取/修改通过Get/Set函数
UPROPERTY(BlueprintReadOnly, Category = "Damage", ReplicatedUsing = OnRep_DefensePower)
FGameplayAttributeData DefensePower;
ATTRIBUTE_ACCESSORS(URPGAttributeSet, DefensePower)
#define ATTRIBUTE_ACCESSORS(ClassName, PropertyName)
这个宏注册了这个属性的4个基础函数
FGameplayAttribute Get##PropertyName##Attribute()
float Get##PropertyName()
void Set##PropertyName
void Init##PropertyName
void URPGAttributeSet::OnRep_Mana()
{
GAMEPLAYATTRIBUTE_REPNOTIFY(URPGAttributeSet, Mana);
}
GAMEPLAYATTRIBUTE_REPNOTIFY这个宏用于RepNotify函数中,在客户端中修改属性
URPGAttributeSet在RPGCharacterBase.h中声明了变量,并作为Character的友元
// Create the attribute set, this replicates by default
AttributeSet = CreateDefaultSubobject<URPGAttributeSet>(TEXT("AttributeSet"));
在Character的构造函数中,创建了属性集对象
以上是对GAS的第一部分的浅析,感谢阅读!