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

谁说胖子不能爱 提交于 2019-12-02 10:57:39

问题


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).

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


  • 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.

回答1:


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.

Minimal example

In 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;

    // ...
}

In 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);
    }
}

In 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)

    // ...
}

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.

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



来源:https://stackoverflow.com/questions/53036364/unreal-gas-print-out-the-current-value-of-an-attribute-to-ui-when-it-changes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!