上一篇,我们实现了玩家角色和敌人的等级的获取,使用MMC的提前工作已经准备完成,那么,这一篇讲一下,如何使用MMC,通过角色等级和体力值设置角色的最大血量。
MMC 全称 Mod Magnitude Calculation,为自定义计算数值的类,可以使用蓝图继承,也可以使用c++继承,在这个项目里面我们使用的c++实现。
它需要我们覆写父类的函数CalculateBaseMagnitude_Implementation(),在内部实现计算并返回,接下来,我们将先实现对等级属性和在AS里的体力属性的获取,然后计算并返回。
创建MMC

1.定义需要计算的属性:
private:
UPROPERTY()
FGameplayEffectAttributeCaptureDefinition VigorDef; //游戏玩法效果属性捕捉定义
2.在构造函数中,初始化VigorDef:
(1) VigorDef是什么属性?
VigorDef.AttributeToCapture = UCC_AttributeSet::GetVigorAttribute(); //设置需要计算的属性对象
(2)谁的属性?
VigorDef.AttributeSource = EGameplayEffectAttributeCaptureSource::Target; //设置拾取对象为GE的应用目标
就如下面的代码:
Source/CC_Aura/Private/AbilitySystem/ModMagCalculation/MMC_MaxHealth.cpp:
UMMC_MaxHealth::UMMC_MaxHealth()
{
VigorDef.AttributeToCapture = UCC_AttributeSet::GetVigorAttribute(); //设置需要计算的属性对象
VigorDef.AttributeSource = EGameplayEffectAttributeCaptureSource::Target; //设置拾取对象为GE的应用目标
VigorDef.bSnapshot = false; //是否应用快照
RelevantAttributesToCapture.Add(VigorDef); //添加到捕获属性数值,只有添加到列表,才会去获取属性值
}
3.在CalculateBaseMagnitude_Implementation函数里计算MaxHealth:
virtual float CalculateBaseMagnitude_Implementation(const FGameplayEffectSpec& Spec) const override;
float UMMC_MaxHealth::CalculateBaseMagnitude_Implementation(const FGameplayEffectSpec& Spec) const
{
/*1. 获取Vigor */
const 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::Max<float>(Vigor, 0.f);
/*2. 获取Level */
ICC_CombatInterface* CombatInterface = Cast<ICC_CombatInterface>(Spec.GetContext().GetSourceObject());
int32 CharacterLevel = CombatInterface->GetCharacterLevel();
/*3. 计算公式*/
return (80.f + Vigor * 2.5f + CharacterLevel * 10.f);
}
486

被折叠的 条评论
为什么被折叠?



