Unreal GAS:当属性改变时将其当前值打印到 UI [英] Unreal GAS: Print out the current value of an attribute to UI when it changes

查看:38
本文介绍了Unreal GAS:当属性改变时将其当前值打印到 UI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When the base value of an attribute changes, there is UAttributeSet::PostGameplayEffectExecute() available to access the (new) value and GameplayEffect with its context. I'm using this to print the changed value to UI (this is also done in ActionRPG).

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

Is there something similar available for the current value of an attribute? How to notify UI, when FGameplayAttributeData::CurrentValue is updated?

  • 虽然 UAttributeSet::PreAttributeChange() 会在每次值更新时调用,但它不提供任何上下文,因此无法从那里访问 UI(由 FAggregator 广播的事件 也不适合).
  • 可以改用 GameplayCue 来设置 UI 中 FGameplayAttributeData::CurrentValue 的值(提示由 GameplayEffect 谁设置当前值).这可以通过从 GameplayCueNotifyActor 派生并使用其事件 OnExecuteOnRemove 来实现.然而,仅仅为了更新 UI 而实例化一个 actor 似乎是一种资源浪费.
  • 也可以使用 UI 本身获取信息(调用一个函数,该函数在每个滴答或使用计时器访问属性),但与事件驱动的 UI 更新相比,这也是一种浪费.
  • Though UAttributeSet::PreAttributeChange() is called on every value update, it doesn't provide any context so it is not possible to access the UI from there (events broadcasted by FAggregator also don't fit).
  • It is possible to use a GameplayCue instead, to set the value of FGameplayAttributeData::CurrentValue within the UI (the cue is triggered by the GameplayEffect who sets the current value). This is possible by deriving from a GameplayCueNotifyActor and use its events OnExecute and OnRemove. However, instantiating an actor just to update UI seems to be a waste of resources.
  • It is also possible to fetch the information using the UI itself (calling a function which accesses the attribute each tick or with a timer), but in comparison to event driven UI update, this is also wasteful.

推荐答案

GameplayAbilitySystem 有 UAbilitySystemComponent::GetGameplayAttributeValueChangeDelegate() 返回一个被触发的 FOnGameplayAttributeValueChange 类型的回调每当属性更改时(基值或当前值).这可用于注册可用于更新 UI 的委托/回调.

The GameplayAbilitySystem has UAbilitySystemComponent::GetGameplayAttributeValueChangeDelegate() which returns a callback of type FOnGameplayAttributeValueChange that is triggered whenever an attribute is changed (base value or current value). This can be used to register a delegate/callback, which can be used to update the 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 的函数.

Example for updating the UI via the EventGraph of the character blueprint, which derived from MyCharacter. UpdatedManaInUI is the function which prints the value to the UI.

此处,UpdatedManaInUI 自行检索值.您可能想要使用 OnManaChangeAttributeValue.

Here, UpdatedManaInUI retrieves the value by itself. You might want to use the AttributeValue of OnManaChange.

这篇关于Unreal GAS:当属性改变时将其当前值打印到 UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆