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?
- 虽然
UAttributeSet::PreAttributeChange()
在每次值更新时都会被调用,但它不提供任何上下文,因此无法从那里访问 UI(FAggregator
广播的事件也不适合)。
- 可以改用 GameplayCue,在 UI 中设置
FGameplayAttributeData::CurrentValue
的值(提示由 GameplayEffect 谁设置了当前值)。这可以通过从 GameplayCueNotifyActor
派生并使用其事件 OnExecute
和 OnRemove
来实现。然而,仅仅为了更新 UI 而实例化一个 actor 似乎是一种资源浪费。
- 也可以使用 UI 本身获取信息(调用一个函数来访问每个 tick 或使用计时器的属性),但与事件驱动的 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
自己取值。您可能想使用 OnManaChange
的 AttributeValue
。
当属性的基值发生变化时,UAttributeSet::PostGameplayEffectExecute()
可用于访问(新)值和 GameplayEffect 及其上下文。我正在使用它来将更改后的值打印为 UI(这也在 ActionRPG 中完成)。
对于属性的当前值是否有类似的东西可用? FGameplayAttributeData::CurrentValue
更新时如何通知UI?
- 虽然
UAttributeSet::PreAttributeChange()
在每次值更新时都会被调用,但它不提供任何上下文,因此无法从那里访问 UI(FAggregator
广播的事件也不适合)。 - 可以改用 GameplayCue,在 UI 中设置
FGameplayAttributeData::CurrentValue
的值(提示由 GameplayEffect 谁设置了当前值)。这可以通过从GameplayCueNotifyActor
派生并使用其事件OnExecute
和OnRemove
来实现。然而,仅仅为了更新 UI 而实例化一个 actor 似乎是一种资源浪费。 - 也可以使用 UI 本身获取信息(调用一个函数来访问每个 tick 或使用计时器的属性),但与事件驱动的 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
自己取值。您可能想使用 OnManaChange
的 AttributeValue
。