Unreal GAS:当一个属性改变时打印出当前值到UI

Unreal GAS: Print out the current value of an attribute to UI when it changes

当属性的基值发生变化时,UAttributeSet::PostGameplayEffectExecute() 可用于访问(新)值和 GameplayEffect 及其上下文。我正在使用它来将更改后的值打印为 UI(这也在 ActionRPG 中完成)。

对于属性的当前值是否有类似的东西可用? FGameplayAttributeData::CurrentValue更新时如何通知UI?


GameplayAbilitySystem 具有 UAbilitySystemComponent::GetGameplayAttributeValueChangeDelegate() 其中 returns 类型为 FOnGameplayAttributeValueChange 的回调,只要更改属性(基值或当前值)就会触发。这可以用来注册一个delegate/callback,可以用来更新UI.

最小示例

MyCharacter.h

// Declare the type for the UI callback.
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FAttributeChange, float, AttributeValue);

UCLASS()
class MYPROJECT_API MyCharacter : public ACharacter, public IAbilitySystemInterface
{
    // ...

    // This callback can be used by the UI.
    UPROPERTY(BlueprintAssignable, Category = "Attribute callbacks")
    FAttributeChange OnManaChange;

    // The callback to be registered within AbilitySystem.
    void OnManaUpdated(const FOnAttributeChangeData& Data);

    // Within here, the callback is registered.
    void BeginPlay() override;

    // ...
}

MyCharacter.cpp

void MyCharacter::OnManaUpdated(const FOnAttributeChangeData& Data)
{
    // Fire the callback. Data contains more than NewValue, in case it is needed.
    OnManaChange.Broadcast(Data.NewValue);
}

void MyCharacter::BeginPlay()
{
    Super::BeginPlay();
    if (AbilitySystemComponent)
    {
        AbilitySystemComponent->GetGameplayAttributeValueChangeDelegate(MyAttributeSet::GetManaAttribute()).AddUObject(this, &MyCharacterBase::OnManaUpdated);
    }
}

MyAttributeSet.h

UCLASS()
class MYPROJECT_API MyAttributeSet : public UAttributeSet
{
    // ...
    UPROPERTY(BlueprintReadOnly, Category = "Mana", ReplicatedUsing=OnRep_Mana)
    FGameplayAttributeData Mana;

    // Add GetManaAttribute().
    GAMEPLAYATTRIBUTE_PROPERTY_GETTER(URPGAttributeSet, Mana)

    // ...
}

通过派生自MyCharacter的角色蓝图的EventGraph更新UI的示例。 UpdatedManaInUI 是将值打印到 UI.

的函数

这里,UpdatedManaInUI自己取值。您可能想使用 OnManaChangeAttributeValue