网站设计建设流程,trellis wordpress,沧州网站建设制作,为什么使用html5网站上一篇#xff0c;我们实现了玩家角色和敌人的等级的获取#xff0c;使用MMC的提前工作已经准备完成#xff0c;那么#xff0c;这一篇讲一下#xff0c;如何使用MMC#xff0c;通过角色等级和体力值设置角色的最大血量。 MMC 全称 Mod Magnitude Calculation#xff0c…上一篇我们实现了玩家角色和敌人的等级的获取使用MMC的提前工作已经准备完成那么这一篇讲一下如何使用MMC通过角色等级和体力值设置角色的最大血量。 MMC 全称 Mod Magnitude Calculation为自定义计算数值的类可以使用蓝图继承也可以使用c继承在这个项目里面我们使用的c实现。 它需要我们覆写父类的函数CalculateBaseMagnitude_Implementation()在内部实现计算并返回接下来我们将先实现对等级属性和在AS里的体力属性的获取然后计算并返回。
创建MMC
打开UE创建C类选择所有类在里面找到GameplayModeMagnitudeCalculation类作为基类 创建类放到一个单独的文件夹后续我们可能需要制作很多的MMC 添加完成后就可以将UE关闭在编辑器内编写MMC代码了。
打开首先添加一个构造函数我们需要在构造函数内去设置需要获取的AS属性
public:UMMC_MaxHealth();我们需要覆盖父类的CalculateBaseMagnitude_Implementation(const FGameplayEffectSpec Spec)这个函数返回了一个应用此MMC的GE的实例我们可以通过Spec去获取需要的内容然后计算数值。
virtual float CalculateBaseMagnitude_Implementation(const FGameplayEffectSpec Spec) const override;创建一个FGameplayEffectAttributeCaptureDefinition 属性我们会在cpp文件内使用它去获取AS内的体力的值。
private:FGameplayEffectAttributeCaptureDefinition VigorDef;以下为.h文件的代码
// 版权归暮志未晚所有。#pragma once#include CoreMinimal.h
#include GameplayModMagnitudeCalculation.h
#include MMC_MaxHealth.generated.h/*** */
UCLASS()
class AURA_API UMMC_MaxHealth : public UGameplayModMagnitudeCalculation
{GENERATED_BODY()public:UMMC_MaxHealth();virtual float CalculateBaseMagnitude_Implementation(const FGameplayEffectSpec Spec) const override;private:FGameplayEffectAttributeCaptureDefinition VigorDef;
};
接着转到cpp文件内编写构造函数设置需要拾取的参数的内容用于拾取目标身上的属性的值。
UMMC_MaxHealth::UMMC_MaxHealth()
{VigorDef.AttributeToCapture UAttributeSetBase::GetVigorAttribute(); //设置需要获取的属性对象VigorDef.AttributeSource EGameplayEffectAttributeCaptureSource::Target; //设置拾取对象为GE的应用目标VigorDef.bSnapshot false;RelevantAttributesToCapture.Add(VigorDef); //添加到捕获属性数值只有添加到列表才会去获取属性值
}接下来就是编写CalculateBaseMagnitude_Implementation函数我们首先要实现对体力值的获取。获取使用到的函数是GetCapturedAttributeMagnitude()里面需要FAggregatorEvaluateParameters 参数那么我们先创建此参数并设置目标和源的Tag设置给它 // 从 source 和 target 获取 Tagconst FGameplayTagContainer* SourceTags Spec.CapturedSourceTags.GetAggregatedTags();const FGameplayTagContainer* TargetTags Spec.CapturedTargetTags.GetAggregatedTags();FAggregatorEvaluateParameters EvaluateParameters;EvaluateParameters.SourceTags SourceTags;EvaluateParameters.TargetTags TargetTags;float Vigor 0.f;GetCapturedAttributeMagnitude(VigorDef, Spec, EvaluateParameters, Vigor);Vigor FMath::Maxfloat(Vigor, 0.f);体力获取到了我们还需要获取它的等级获取等级需要此GE的应用源可以通过Spec.GetContext().GetSourceObject()去获取源对象当然你不设置是获取不到的这个需要在应用GE的时候设置源设置方法如下
void ACharacterBase::ApplyEffectToSelf(TSubclassOfUGameplayEffect GameplayEffectClass, float Level) const
{check(IsValid(GetAbilitySystemComponent()));check(GameplayEffectClass);FGameplayEffectContextHandle ContextHandle GetAbilitySystemComponent()-MakeEffectContext();ContextHandle.AddSourceObject(this); //设置源对象可以通过Spec.GetContext().GetSourceObject()去获取源对象const FGameplayEffectSpecHandle SpecHandle GetAbilitySystemComponent()-MakeOutgoingSpec(GameplayEffectClass, Level, ContextHandle);GetAbilitySystemComponent()-ApplyGameplayEffectSpecToTarget(*SpecHandle.Data.Get(), GetAbilitySystemComponent());
}能获取到应用GE的对象那么也就是玩家角色自身我们就可以从玩家角色身上调用GetPlayerLevel()函数获取等级 //获取等级ICombatInterface* CombatInterface CastICombatInterface(Spec.GetContext().GetSourceObject());const int32 Level CombatInterface-GetPlayerLevel();体力和等级我们都已经拿到那么应用我们自身的公式得出最大血量结果 //计算最大血量return 80.f Vigor * 2.5f Level * 10.f;以下为cpp的完整代码
// 版权归暮志未晚所有。#include AbilitySystem/ModMagCalc/MMC_MaxHealth.h#include AbilitySystem/AttributeSetBase.h
#include Interaction/CombatInterface.hUMMC_MaxHealth::UMMC_MaxHealth()
{VigorDef.AttributeToCapture UAttributeSetBase::GetVigorAttribute(); //设置需要获取的属性对象VigorDef.AttributeSource EGameplayEffectAttributeCaptureSource::Target; //设置拾取对象为GE的应用目标VigorDef.bSnapshot false;RelevantAttributesToCapture.Add(VigorDef); //添加到捕获属性数值只有添加到列表才会去获取属性值
}float UMMC_MaxHealth::CalculateBaseMagnitude_Implementation(const FGameplayEffectSpec Spec) const
{// 从 source 和 target 获取 Tagconst FGameplayTagContainer* SourceTags Spec.CapturedSourceTags.GetAggregatedTags();const FGameplayTagContainer* TargetTags Spec.CapturedTargetTags.GetAggregatedTags();FAggregatorEvaluateParameters EvaluateParameters;EvaluateParameters.SourceTags SourceTags;EvaluateParameters.TargetTags TargetTags;//获取体力值float Vigor 0.f;GetCapturedAttributeMagnitude(VigorDef, Spec, EvaluateParameters, Vigor);Vigor FMath::Maxfloat(Vigor, 0.f);//获取等级ICombatInterface* CombatInterface CastICombatInterface(Spec.GetContext().GetSourceObject());const int32 Level CombatInterface-GetPlayerLevel();//计算最大血量return 80.f Vigor * 2.5f Level * 10.f;
}
编写完成以后就可以进行编译进入UE中设置type修改为Custom Calculation ClassClass设置上我们刚才制作的MMC 完成以后运行查看效果我们等级现在设置的是1 Vigor是23 那么结果就应该是 80 23*2.5 1 * 10 147.5数值可以对起来 打个断点看一下值没有问题 这里我们就实现了对最大血量的设置。 接下来按照同样的道理我们再创建一个MMC_MaxMana用于处理最大蓝量这里我就不再赘述了和最大血量一个道理直接贴代码。
// 版权归暮志未晚所有。#pragma once#include CoreMinimal.h
#include GameplayModMagnitudeCalculation.h
#include MMC_MaxMana.generated.h/*** */
UCLASS()
class AURA_API UMMC_MaxMana : public UGameplayModMagnitudeCalculation
{GENERATED_BODY()public:UMMC_MaxMana();virtual float CalculateBaseMagnitude_Implementation(const FGameplayEffectSpec Spec) const override;private:FGameplayEffectAttributeCaptureDefinition IntelligenceDef;};
// 版权归暮志未晚所有。#include AbilitySystem/ModMagCalc/MMC_MaxMana.h#include AbilitySystem/AttributeSetBase.h
#include Interaction/CombatInterface.hUMMC_MaxMana::UMMC_MaxMana()
{IntelligenceDef.AttributeToCapture UAttributeSetBase::GetIntelligenceAttribute(); //设置需要获取的属性对象IntelligenceDef.AttributeSource EGameplayEffectAttributeCaptureSource::Target; //设置拾取对象为GE的应用目标IntelligenceDef.bSnapshot false;RelevantAttributesToCapture.Add(IntelligenceDef); //添加到捕获属性数值只有添加到列表才会去获取属性值
}float UMMC_MaxMana::CalculateBaseMagnitude_Implementation(const FGameplayEffectSpec Spec) const
{// 从 source 和 target 获取 Tagconst FGameplayTagContainer* SourceTags Spec.CapturedSourceTags.GetAggregatedTags();const FGameplayTagContainer* TargetTags Spec.CapturedTargetTags.GetAggregatedTags();FAggregatorEvaluateParameters EvaluateParameters;EvaluateParameters.SourceTags SourceTags;EvaluateParameters.TargetTags TargetTags;//获取体力值float Intelligence 0.f;GetCapturedAttributeMagnitude(IntelligenceDef, Spec, EvaluateParameters, Intelligence);Intelligence FMath::Maxfloat(Intelligence, 0.f);//获取等级ICombatInterface* CombatInterface CastICombatInterface(Spec.GetContext().GetSourceObject());const int32 Level CombatInterface-GetPlayerLevel();//计算最大血量return 50.f Intelligence * 2.5f Level * 15.f;
}
智力为21 等级为1 50 21 * 2.5f 15 117.5 数值正确。 上面就是MMC的创建。
初始化血量和蓝量
在设置完最大血量和最大蓝量后我们会发现血量和蓝量的都是不满的接下来我们要将药瓶填满。 在角色基类上面我们已经完成对PrimaryAttribute和SecondaryAttribute的初始化 我们在下面接着增加一个参数配置可以配置更多一个GE UPROPERTY(BlueprintReadOnly, EditAnywhere, CategoryAttributes)TSubclassOfUGameplayEffect DefaultVitalAttributes;在应用GE时也应用到自身注意顺序不能乱
void ACharacterBase::InitializeDefaultAttributes() const
{ApplyEffectToSelf(DefaultPrimaryAttributes, 1.f);ApplyEffectToSelf(DefaultSecondaryAttributes, 1.f);ApplyEffectToSelf(DefaultVitalAttributes, 1.f);
}接着创建第三个初始化GE我们这次使用Instant在设置完成最大血量和最大蓝量血量和蓝量填满即可 这里只需要使用Attribute Based 基于最大血量和蓝量一比一填入即可 将GE设置到角色身上 药瓶满了 可以看到最大血量和血量最大蓝量和蓝量数值相同了