03 AttributeSet与属性复制

实现可复制、可钳制的属性集,区分 Base、Current 与属性变化回调。

专项导航 · 资料索引

章节重点

声明能被 GE 修改、能复制到客户端的数值。用一次临时增益解释 BaseValue 与 CurrentValue,再把动态钳制和 HUD 通知放到正确位置。

先决定哪些数值需要独立存在

A、B 都从 Energy=30、Integrity=60、MaxIntegrity=100 开始。RepairEfficiency 初值为 1,第 08 章用来模拟维修效率下降。Energy 在本案例固定上限 30,MaxIntegrity 在基础实验固定为 100。资源消耗和维修都用 Instant GE;持续效果留给效率这类由聚合器计算的数值。这样在记录中容易区分“历史结算”与“当前加成”。

// RelayVitals.h
#pragma once
#include "CoreMinimal.h"
#include "AttributeSet.h"
#include "AbilitySystemComponent.h"
#include "RelayVitals.generated.h"

#define RELAY_ACCESSORS(Property) \
    GAMEPLAYATTRIBUTE_PROPERTY_GETTER(URelayVitals, Property) \
    GAMEPLAYATTRIBUTE_VALUE_GETTER(Property) \
    GAMEPLAYATTRIBUTE_VALUE_SETTER(Property) \
    GAMEPLAYATTRIBUTE_VALUE_INITTER(Property)

UCLASS()
class RELAYLAB_API URelayVitals : public UAttributeSet
{
    GENERATED_BODY()
public:
    URelayVitals();
    UPROPERTY(BlueprintReadOnly, ReplicatedUsing=OnRep_Energy, Category="Vitals")
    FGameplayAttributeData Energy;
    RELAY_ACCESSORS(Energy)
    UPROPERTY(BlueprintReadOnly, ReplicatedUsing=OnRep_Integrity, Category="Vitals")
    FGameplayAttributeData Integrity;
    RELAY_ACCESSORS(Integrity)
    UPROPERTY(BlueprintReadOnly, ReplicatedUsing=OnRep_MaxIntegrity, Category="Vitals")
    FGameplayAttributeData MaxIntegrity;
    RELAY_ACCESSORS(MaxIntegrity)
    UPROPERTY(BlueprintReadOnly, ReplicatedUsing=OnRep_RepairEfficiency, Category="Vitals")
    FGameplayAttributeData RepairEfficiency;
    RELAY_ACCESSORS(RepairEfficiency)

    virtual void GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const override;
    virtual void PostGameplayEffectExecute(const FGameplayEffectModCallbackData& Data) override;

protected:
    UFUNCTION() void OnRep_Energy(const FGameplayAttributeData& OldValue);
    UFUNCTION() void OnRep_Integrity(const FGameplayAttributeData& OldValue);
    UFUNCTION() void OnRep_MaxIntegrity(const FGameplayAttributeData& OldValue);
    UFUNCTION() void OnRep_RepairEfficiency(const FGameplayAttributeData& OldValue);
};
#undef RELAY_ACCESSORS
// RelayVitals.cpp
#include "RelayVitals.h"
#include "GameplayEffectExtension.h"
#include "Net/UnrealNetwork.h"

URelayVitals::URelayVitals()
{
    InitMaxIntegrity(100.f);
    InitIntegrity(60.f);
    InitEnergy(30.f);
    InitRepairEfficiency(1.f);
}

void URelayVitals::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
{
    Super::GetLifetimeReplicatedProps(OutLifetimeProps);
    DOREPLIFETIME_CONDITION_NOTIFY(URelayVitals, Energy, COND_None, REPNOTIFY_Always);
    DOREPLIFETIME_CONDITION_NOTIFY(URelayVitals, Integrity, COND_None, REPNOTIFY_Always);
    DOREPLIFETIME_CONDITION_NOTIFY(URelayVitals, MaxIntegrity, COND_None, REPNOTIFY_Always);
    DOREPLIFETIME_CONDITION_NOTIFY(URelayVitals, RepairEfficiency, COND_None, REPNOTIFY_Always);
}

void URelayVitals::OnRep_Energy(const FGameplayAttributeData& OldValue)
{
    GAMEPLAYATTRIBUTE_REPNOTIFY(URelayVitals, Energy, OldValue);
}
void URelayVitals::OnRep_Integrity(const FGameplayAttributeData& OldValue)
{
    GAMEPLAYATTRIBUTE_REPNOTIFY(URelayVitals, Integrity, OldValue);
}
void URelayVitals::OnRep_MaxIntegrity(const FGameplayAttributeData& OldValue)
{
    GAMEPLAYATTRIBUTE_REPNOTIFY(URelayVitals, MaxIntegrity, OldValue);
}
void URelayVitals::OnRep_RepairEfficiency(const FGameplayAttributeData& OldValue)
{
    GAMEPLAYATTRIBUTE_REPNOTIFY(URelayVitals, RepairEfficiency, OldValue);
}

void URelayVitals::PostGameplayEffectExecute(const FGameplayEffectModCallbackData& Data)
{
    Super::PostGameplayEffectExecute(Data);
    const FGameplayAttribute& Changed = Data.EvaluatedData.Attribute;
    if (Changed == GetIntegrityAttribute() || Changed == GetMaxIntegrityAttribute())
    {
        SetIntegrity(FMath::Clamp(GetIntegrity(), 0.f, FMath::Max(0.f, GetMaxIntegrity())));
    }
    else if (Changed == GetEnergyAttribute())
    {
        SetEnergy(FMath::Clamp(GetEnergy(), 0.f, 30.f));
    }
}

GAMEPLAYATTRIBUTE_REPNOTIFY 把复制过来的值交还给 ASC 的属性更新路径。在预测端,显示值可能已经等于服务器发来的数值,也仍需要处理确认。这说明“收到的属性包即使值看起来相同也通知”和“每帧都发送这个属性”是两件事。本实验开放复制便于在观察端对照,实际项目可只向拥有者发送私有资源。

同一个面板显示 0.5,底层可能有两种状态

给 B 一个 5 秒无周期 GE,在 RepairEfficiency 上做 Additive −0.5。应用前 Base/Current 为 1/1,期间为 1/0.5,到期回到 1/1。把它改成 Instant 后,应用完是 0.5/0.5,5 秒后仍然如此。当前值会随活跃修饰重新计算,不适合当作新的永久基准再写回。可分别用 ASC 的 GetNumericAttributeBase 与 GetNumericAttribute 记录。

PostGameplayEffectExecute 适用于一次执行修改基础值后的业务处理。本例 B 的 Integrity=95,维修 +20 后钳到 100。若将来允许持续 GE 改 MaxIntegrity,施加和移除修饰都可能引起当前上限重算,仅靠这个回调覆盖不了。应另行设计上限缩放策略,借助 PreAttributeChange 限定当前值、观察上限变化并按规则修正资源,还要测试增益消退。不要把临时 Current 当 Base 写回。

章节回顾

属性声明、复制注册和 RepNotify 共同建立多人属性路径。临时效果主要改变聚合后的读数;即时效果会留下基础值变化。回调选择应由变化来源决定。

测试题

  1. B 已有 95 点完整度,本例 +20 后服务器与观察端应各显示多少?
  2. 持续 −0.5 效率到期,是否应由业务代码自己再加 +0.5?
  3. 服务端 GE 能改数,客户端 HUD 总不更新,哪三处要连起来检查?
  4. 为什么本例在 PostGameplayEffectExecute 限制资源,但不把它作为所有 buff 的移除通知?

测试题答案

研读入口

Gameplay Attributes and Attribute Sets · UAttributeSet · UAbilitySystemComponent


← 搭建ASC并授予首个能力 · 专项学习导航 · 标签、输入与激活条件 →