网站开发用到的虚拟机有哪些,网站建设实践总结,图片描述 wordpress,发稿推广前面几章#xff0c;我们实现了通过GameplayEffect对Attribute值的修改#xff0c;比如血量和蓝量#xff0c;我们都是有一个最大血量和最大蓝量去限制它的最大值#xff0c;而且血量和蓝量最小值不会小于零。之前我们是没有实现相关限制的#xff0c;接下来#xff0c;我…前面几章我们实现了通过GameplayEffect对Attribute值的修改比如血量和蓝量我们都是有一个最大血量和最大蓝量去限制它的最大值而且血量和蓝量最小值不会小于零。之前我们是没有实现相关限制的接下来我们需要在AttributeSet函数里面实现一下对实际值的范围限制。
实现
首先覆盖父类函数在PreAttributeChange()函数这个函数会在AttributeSet里的监听的值发生改变前触发回调
virtual void PreAttributeChange(const FGameplayAttribute Attribute, float NewValue) override;回调有返回两个参数一个是Attribute我们可以通过此值判断哪个属性被修改掉了另一个是将要修改成的值接下来我们打印一下值看一下结果。 if(Attribute GetHealthAttribute()){UE_LOG(LogTemp, Warning, TEXT(Health: %f), NewValue);}if(Attribute GetMaxHealthAttribute()){UE_LOG(LogTemp, Warning, TEXT(MaxHealth: %f), NewValue);}if(Attribute GetManaAttribute()){UE_LOG(LogTemp, Warning, TEXT(Mana: %f), NewValue);}if(Attribute GetMaxManaAttribute()){UE_LOG(LogTemp, Warning, TEXT(MaxMana: %f), NewValue);}编译打开UE点击场景左下角的输出日志 选择停靠在布局中 然后让角色去吃药瓶水晶以及去踩火堆看看属性变化我们会发现所有属性变化都能够如实的反应在打印上面 接着使用clamp函数将血量和蓝量数值限制在0到最大血量和蓝量的范围内
NewValue FMath::Clamp(NewValue, 0.f, GetMaxHealth());运行UE再查看发现数值都被限制在了范围内
PostGameplayEffectExecute
PostGameplayEffectExecute()函数是在数值变化后触发的一般只会在Instant类型的GameplayEffect才可以触发Duration和Infinite类的GameplayEffect如果设置Period也可以触发。 这个函数的应用场景很多我们可以做一些逻辑操作比如死亡无敌不扣血等等。 使用它我们需要先覆盖父类
virtual void PostGameplayEffectExecute(const FGameplayEffectModCallbackData Data) override;它只有一个返回参数就是Data但是Data里面包含的内容很多 if(Data.EvaluatedData.Attribute GetHealthAttribute()){UE_LOG(LogTemp, Warning, TEXT(Health: %f), GetHealth());UE_LOG(LogTemp, Warning, TEXT(Magnitude: %f), Data.EvaluatedData.Magnitude);}打开UE可以查看到当前的血量以及这次Effect造成的伤害数值。 我们打一个断点 可以看到Data里面有三项数据 EffectSpec就是效果的实例里面包含很多的数据我们可以通过它获取到时哪个Actor将此GE应用到目标身上的 EvaluatedData就是修改的数据相关的内容当前值修改了多少值修改的什么属性等等 Target就是目标的ASC 接下来我们将从Data中获取到需要的然后封装成一个结构体方便后续使用。
首先创建一个FEffectProperties的结构体用于存储施放GE的相关对象和目标的相关对象。这个结构体将GE的上下文并将施放者和目标的ASC AvatarActor Controller Character都保存了下来
USTRUCT()
struct FEffectProperties
{GENERATED_BODY()FEffectProperties(){}FGameplayEffectContextHandle EffectContextHandle;UPROPERTY()UAbilitySystemComponent* SourceASC nullptr;UPROPERTY()AActor* SourceAvatarActor nullptr;UPROPERTY()AController* SourceController nullptr;UPROPERTY()ACharacter* SourceCharacter nullptr;UPROPERTY()UAbilitySystemComponent* TargetASC nullptr;UPROPERTY()AActor* TargetAvatarActor nullptr;UPROPERTY()AController* TargetController nullptr;UPROPERTY()ACharacter* TargetCharacter nullptr;
};接下来创建一个私有函数我们在这个函数里面去处理生成结构体属性的值。函数接收两个值一个是PostGameplayEffectExecute()函数返回的Data另一个是需要填充的结构体。
static void SetEffectProperties(const FGameplayEffectModCallbackData Data, FEffectProperties Props);接着实现函数在函数内设置属性前面将了可以通过Data获取到相关的属性
void UAttributeSetBase::SetEffectProperties(const FGameplayEffectModCallbackData Data, FEffectProperties Props)
{//Source 效果的所有者 Target 效果应用的目标Props.EffectContextHandle Data.EffectSpec.GetContext();Props.SourceASC Props.EffectContextHandle.GetOriginalInstigatorAbilitySystemComponent(); //获取效果所有者的ASC//获取效果所有者的相关对象if(IsValid(Props.SourceASC) Props.SourceASC-AbilityActorInfo.IsValid() Props.SourceASC-AbilityActorInfo-AvatarActor.IsValid()){Props.SourceAvatarActor Props.SourceASC-AbilityActorInfo-AvatarActor.Get(); //获取ActorProps.SourceController Props.SourceASC-AbilityActorInfo-PlayerController.Get(); //获取PlayerControllerif(Props.SourceController nullptr Props.SourceAvatarActor ! nullptr){if(const APawn* Pawn CastAPawn(Props.SourceAvatarActor)){Props.SourceController Pawn-GetController();}}if(Props.SourceController){Props.SourceCharacter CastACharacter(Props.SourceController-GetPawn());}}if(Data.Target.AbilityActorInfo.IsValid() Data.Target.AbilityActorInfo-AvatarActor.IsValid()){Props.TargetAvatarActor Data.Target.AbilityActorInfo-AvatarActor.Get();Props.TargetController Data.Target.AbilityActorInfo-PlayerController.Get();Props.TargetCharacter CastACharacter(Props.TargetAvatarActor);Props.TargetASC UAbilitySystemBlueprintLibrary::GetAbilitySystemComponent(Props.TargetAvatarActor);}
}接着只需要在PostGameplayEffectExecute()内创建一个结构体并调用函数生成内容即可。
void UAttributeSetBase::PostGameplayEffectExecute(const FGameplayEffectModCallbackData Data)
{Super::PostGameplayEffectExecute(Data);FEffectProperties Props;SetEffectProperties(Data, Props);}在使用时我们就可以通过结构体去获取相应的内容逻辑更加整洁
源代码
AttributeSetBase.h
// 版权归暮志未晚所有。#pragma once#include CoreMinimal.h
#include AttributeSet.h
#include AbilitySystemComponent.h
#include AttributeSetBase.generated.h// Uses macros from AttributeSet.h
#define ATTRIBUTE_ACCESSORS(ClassName, PropertyName) \
GAMEPLAYATTRIBUTE_PROPERTY_GETTER(ClassName, PropertyName) \
GAMEPLAYATTRIBUTE_VALUE_GETTER(PropertyName) \
GAMEPLAYATTRIBUTE_VALUE_SETTER(PropertyName) \
GAMEPLAYATTRIBUTE_VALUE_INITTER(PropertyName)USTRUCT()
struct FEffectProperties
{GENERATED_BODY()FEffectProperties(){}FGameplayEffectContextHandle EffectContextHandle;UPROPERTY()UAbilitySystemComponent* SourceASC nullptr;UPROPERTY()AActor* SourceAvatarActor nullptr;UPROPERTY()AController* SourceController nullptr;UPROPERTY()ACharacter* SourceCharacter nullptr;UPROPERTY()UAbilitySystemComponent* TargetASC nullptr;UPROPERTY()AActor* TargetAvatarActor nullptr;UPROPERTY()AController* TargetController nullptr;UPROPERTY()ACharacter* TargetCharacter nullptr;
};/*** 技能系统属性集*/
UCLASS()
class AURA_API UAttributeSetBase : public UAttributeSet
{GENERATED_BODY()public:UAttributeSetBase();virtual void GetLifetimeReplicatedProps(TArrayFLifetimeProperty OutLifetimeProps) const override;virtual void PreAttributeChange(const FGameplayAttribute Attribute, float NewValue) override;virtual void PostGameplayEffectExecute(const FGameplayEffectModCallbackData Data) override;UPROPERTY(BlueprintReadOnly,ReplicatedUsing OnRep_Health, CategoryVital Attributes)FGameplayAttributeData Health;ATTRIBUTE_ACCESSORS(UAttributeSetBase, Health);UPROPERTY(BlueprintReadOnly,ReplicatedUsing OnRep_MaxHealth, CategoryVital Attributes)FGameplayAttributeData MaxHealth;ATTRIBUTE_ACCESSORS(UAttributeSetBase, MaxHealth);UPROPERTY(BlueprintReadOnly,ReplicatedUsing OnRep_Mana, CategoryVital Attributes)FGameplayAttributeData Mana;ATTRIBUTE_ACCESSORS(UAttributeSetBase, Mana);UPROPERTY(BlueprintReadOnly,ReplicatedUsing OnRep_MaxMana, CategoryVital Attributes)FGameplayAttributeData MaxMana;ATTRIBUTE_ACCESSORS(UAttributeSetBase, MaxMana);UFUNCTION()void OnRep_Health(const FGameplayAttributeData OldHealth) const;UFUNCTION()void OnRep_MaxHealth(const FGameplayAttributeData OldMaxHealth) const;UFUNCTION()void OnRep_Mana(const FGameplayAttributeData OldMana) const;UFUNCTION()void OnRep_MaxMana(const FGameplayAttributeData OldMaxMana) const;private:static void SetEffectProperties(const FGameplayEffectModCallbackData Data, FEffectProperties Props);
};
AttributeSetBase.cpp
// 版权归暮志未晚所有。#include AbilitySystem/AttributeSetBase.h#include AbilitySystemBlueprintLibrary.h
#include GameplayEffectExtension.h
#include GameFramework/Character.h
#include Net/UnrealNetwork.hUAttributeSetBase::UAttributeSetBase()
{InitHealth(30.f);InitMaxHealth(100.f);InitMana(30.f);InitMaxMana(100.f);
}void UAttributeSetBase::GetLifetimeReplicatedProps(TArrayFLifetimeProperty OutLifetimeProps) const
{Super::GetLifetimeReplicatedProps(OutLifetimeProps);DOREPLIFETIME_CONDITION_NOTIFY(UAttributeSetBase, Health, COND_None, REPNOTIFY_Always);DOREPLIFETIME_CONDITION_NOTIFY(UAttributeSetBase, MaxHealth, COND_None, REPNOTIFY_Always);DOREPLIFETIME_CONDITION_NOTIFY(UAttributeSetBase, Mana, COND_None, REPNOTIFY_Always);DOREPLIFETIME_CONDITION_NOTIFY(UAttributeSetBase, MaxMana, COND_None, REPNOTIFY_Always);
}void UAttributeSetBase::PreAttributeChange(const FGameplayAttribute Attribute, float NewValue)
{Super::PreAttributeChange(Attribute, NewValue);if(Attribute GetHealthAttribute()){NewValue FMath::Clamp(NewValue, 0.f, GetMaxHealth());// UE_LOG(LogTemp, Warning, TEXT(Health: %f), NewValue);}if(Attribute GetManaAttribute()){NewValue FMath::Clamp(NewValue, 0.f, GetMaxMana());}
}void UAttributeSetBase::SetEffectProperties(const FGameplayEffectModCallbackData Data, FEffectProperties Props)
{//Source 效果的所有者 Target 效果应用的目标Props.EffectContextHandle Data.EffectSpec.GetContext();Props.SourceASC Props.EffectContextHandle.GetOriginalInstigatorAbilitySystemComponent(); //获取效果所有者的ASC//获取效果所有者的相关对象if(IsValid(Props.SourceASC) Props.SourceASC-AbilityActorInfo.IsValid() Props.SourceASC-AbilityActorInfo-AvatarActor.IsValid()){Props.SourceAvatarActor Props.SourceASC-AbilityActorInfo-AvatarActor.Get(); //获取ActorProps.SourceController Props.SourceASC-AbilityActorInfo-PlayerController.Get(); //获取PlayerControllerif(Props.SourceController nullptr Props.SourceAvatarActor ! nullptr){if(const APawn* Pawn CastAPawn(Props.SourceAvatarActor)){Props.SourceController Pawn-GetController();}}if(Props.SourceController){Props.SourceCharacter CastACharacter(Props.SourceController-GetPawn());}}if(Data.Target.AbilityActorInfo.IsValid() Data.Target.AbilityActorInfo-AvatarActor.IsValid()){Props.TargetAvatarActor Data.Target.AbilityActorInfo-AvatarActor.Get();Props.TargetController Data.Target.AbilityActorInfo-PlayerController.Get();Props.TargetCharacter CastACharacter(Props.TargetAvatarActor);Props.TargetASC UAbilitySystemBlueprintLibrary::GetAbilitySystemComponent(Props.TargetAvatarActor);}
}void UAttributeSetBase::PostGameplayEffectExecute(const FGameplayEffectModCallbackData Data)
{Super::PostGameplayEffectExecute(Data);FEffectProperties Props;SetEffectProperties(Data, Props);
}void UAttributeSetBase::OnRep_Health(const FGameplayAttributeData OldHealth) const
{GAMEPLAYATTRIBUTE_REPNOTIFY(UAttributeSetBase, Health, OldHealth);
}void UAttributeSetBase::OnRep_MaxHealth(const FGameplayAttributeData OldMaxHealth) const
{GAMEPLAYATTRIBUTE_REPNOTIFY(UAttributeSetBase, MaxHealth, OldMaxHealth);
}void UAttributeSetBase::OnRep_Mana(const FGameplayAttributeData OldMana) const
{GAMEPLAYATTRIBUTE_REPNOTIFY(UAttributeSetBase, MaxHealth, OldMana);
}void UAttributeSetBase::OnRep_MaxMana(const FGameplayAttributeData OldMaxMana) const
{GAMEPLAYATTRIBUTE_REPNOTIFY(UAttributeSetBase, MaxHealth, OldMaxMana);
}