1.
打开MMOARPGAttributeSet.h,增加经验值属性
UPROPERTY(BlueprintReadOnly, Category = "Attribute", ReplicatedUsing = OnRep_EmpiricalValue) FGameplayAttributeData EmpiricalValue;//经验值 ATTRIBUTE_ACCESSORS(UMMOARPGAttributeSet, EmpiricalValue) UPROPERTY(BlueprintReadOnly, Category = "Attribute", ReplicatedUsing = OnRep_MaxEmpiricalValue) FGameplayAttributeData MaxEmpiricalValue;//最大经验值 ATTRIBUTE_ACCESSORS(UMMOARPGAttributeSet, MaxEmpiricalValue)
2.
绑定OnRep函数,属性值更改时调用
UFUNCTION() virtual void OnRep_EmpiricalValue(const FGameplayAttributeData& OldValue); UFUNCTION() virtual void OnRep_MaxEmpiricalValue(const FGameplayAttributeData& OldValue);
进行实现
void UMMOARPGAttributeSet::OnRep_EmpiricalValue(const FGameplayAttributeData& OldValue) { GAMEPLAYATTRIBUTE_REPNOTIFY(UMMOARPGAttributeSet, EmpiricalValue, OldValue); } void UMMOARPGAttributeSet::OnRep_MaxEmpiricalValue(const FGameplayAttributeData& OldValue) { GAMEPLAYATTRIBUTE_REPNOTIFY(UMMOARPGAttributeSet, MaxEmpiricalValue, OldValue); }
3.
属性复制
virtual void GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const override;
进行实现
void UMMOARPGAttributeSet::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const { Super::GetLifetimeReplicatedProps(OutLifetimeProps); DOREPLIFETIME(UMMOARPGAttributeSet, EmpiricalValue); DOREPLIFETIME(UMMOARPGAttributeSet, MaxEmpiricalValue); }
4.
提前准备一个方法,用于将属性值提出
virtual void ToMMOARPGCharacterAttribute(FMMOARPGCharacterAttribute& OutData);
进行实现
void UMMOARPGAttributeSet::ToMMOARPGCharacterAttribute(FMMOARPGCharacterAttribute& OutData) { //将属性值提取出来存放进OutData RegistrationParam(OutData.Level, Level); RegistrationParam(OutData.Health, Health); RegistrationParam(OutData.MaxHealth, MaxHealth); RegistrationParam(OutData.Mana, Mana); RegistrationParam(OutData.MaxMana, MaxMana); RegistrationParam(OutData.PhysicsAttack, PhysicsAttack); RegistrationParam(OutData.MagicAttack, MagicAttack); RegistrationParam(OutData.PhysicsDefense, PhysicsDefense); RegistrationParam(OutData.MagicDefense, MagicDefense); RegistrationParam(OutData.AttackRange, AttackRange); RegistrationParam(OutData.MaxEmpiricalValue, MaxEmpiricalValue); RegistrationParam(OutData.EmpiricalValue, EmpiricalValue); }