Roi*_*ton 3 c++ unreal-engine4 unreal-gameplay-ability-system
当属性的基值更改时,可以UAttributeSet::PostGameplayEffectExecute()访问(新)值和GameplayEffect及其上下文。我用它来将更改后的值打印到 UI(这也在 ActionRPG 中完成)。
是否有类似的东西可用于属性的当前值?如何通知UI,何时FGameplayAttributeData::CurrentValue更新?
UAttributeSet::PreAttributeChange()在每次值更新时都会调用它,但它不提供任何上下文,因此无法从那里访问 UI(广播的事件FAggregator也不适合)。FGameplayAttributeData::CurrentValue(提示由设置当前值的GameplayEffect触发)。这可以通过从 a 派生GameplayCueNotifyActor并使用其事件OnExecute和 来实现OnRemove。然而,仅仅为了更新 UI 而实例化一个 Actor 似乎是一种资源浪费。GameplayAbilitySystem 会UAbilitySystemComponent::GetGameplayAttributeValueChangeDelegate()返回一个回调类型FOnGameplayAttributeValueChange,每当属性发生更改(基值或当前值)时就会触发该回调。这可用于注册委托/回调,从而可用于更新 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;
// ...
}
Run Code Online (Sandbox Code Playgroud)
在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);
}
}
Run Code Online (Sandbox Code Playgroud)
在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)
// ...
}
Run Code Online (Sandbox Code Playgroud)
通过角色蓝图的 EventGraph 更新 UI 的示例,该示例源自MyCharacter. UpdatedManaInUI是将值打印到 UI 的函数。
此处,UpdatedManaInUI自行检索值。您可能想使用AttributeValueof OnManaChange。
| 归档时间: |
|
| 查看次数: |
2847 次 |
| 最近记录: |