第7章---GE的AttributeBase和CunstomCalculationClass

文章介绍了Unity游戏中的MMC_MaxHealth模块,详细解释了如何使用GameplayModMagnitudeCalculation进行基础健康值计算,包括属性捕捉、源和目标标签的处理,以及利用CombatInterface获取玩家等级等关键部分。
摘要由CSDN通过智能技术生成

总结

  1. 记住Coefficient, Pre Multiply以及Post Multiply的计算顺序: (属性值+Pre Multiply) * Coefficient + Post Multiply
  2. 在选择Custom Calculation Class 后,依然可以使用Coefficient, Pre Multiply以及Post Multiply三个值

C++文件有更新,但是部分基础的东西不写出来了

文件结构

Source

  • Private

    • AbilitySystem
      • ModMagCalc
        • MMC_MaxHealth.cpp
        • MMC_MaxMana.cpp
      • RPGAbilitySystemComponent.cpp
      • RPGAttributeSet.cpp
    • Character
      • PGGameCharacterBase.cpp
      • RPGGameEnemy.cpp
      • RPGGamePlayerCharacter.cpp
    • Game
      • RPGGameModeBase.cpp
    • Interaction
      • EnemyInterface.cpp
      • CombatInterface.cpp
    • Player
      • RPGPlayerController.cpp
      • RPGPlayerState.cpp
    • Actor
      • RPGEffectActor.cpp
    • UI
      • HUD
        • RPGHUD.cpp
      • WidgetController
        • OverlayWidgetController.cpp
        • RPGWidgetController.cpp
      • Widgets
        • RPGUserWidget.cpp
  • Public

    • AbilitySystem

      • ModMagCalc

        • MMC_MaxHealth.h
        • MMC_MaxMana.h
      • RPGAbilitySystemComponent.h

      • RPGAttributeSet.h

    • Character

      • RPGGameCharacterBase.h
      • RPGGameEnemy.h
      • RPGGamePlayerCharacter.h
    • Game

      • RPGGameModeBase.h
    • Interaction

      • EnemyInterface.h
      • CombatInterface.h
    • Player

      • RPGPlayerController.h
      • RPGPlayerState.h
    • Actor

      • RPGEffectActor.h
    • UI

      • HUD
        • RPGHUD.h
      • WidgetController
        • OverlayWidgetController.h
        • RPGWidgetController.h
      • Widgets
        • RPGUserWidget.h

文件概述

由于MMC_MaxMana与MMC_MaxHealth长得除了监视参数以外,所有逻辑一模一样,故此处只记录MMC_MaxHelath

MMC_MaxHealth

.h文件
// Copyright KimiLiu

#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 有两个属性, 一个是GameplayAttribute就是我们需要捕获的Attribute
     *    另一个是GameplayEffectAttributeCaptureSource就是我们需要捕获的Attribute的来源,例如Enemy的Attribute和
     *    PlayerCharacter的Attribute是不一样的
     */
    FGameplayEffectAttributeCaptureDefinition ViggorDef;
};
.cpp文件
// Copyright KimiLiu


#include "AbilitySytstem/ModMagCalc/MMC_MaxHealth.h"

#include "AbilitySytstem/RPGAttributeSet.h"
#include "Interaction/CombatInterface.h"

UMMC_MaxHealth::UMMC_MaxHealth()
{
    
    ViggorDef.AttributeToCapture = URPGAttributeSet::GetVigorAttribute();
    ViggorDef.AttributeSource = EGameplayEffectAttributeCaptureSource::Target;
    ViggorDef.bSnapshot = false;

    RelevantAttributesToCapture.Add(ViggorDef);
}

float UMMC_MaxHealth::CalculateBaseMagnitude_Implementation(const FGameplayEffectSpec& Spec) const
{
    // Gather tags from source and target
    const FGameplayTagContainer* SourceTags = Spec.CapturedSourceTags.GetAggregatedTags();
    const FGameplayTagContainer* TargetTags = Spec.CapturedTargetTags.GetAggregatedTags();

    // 暂时未知FAggregatorEvaluateParameters是拿来干嘛用的
    FAggregatorEvaluateParameters EvaluateParameters;
    EvaluateParameters.SourceTags = SourceTags;
    EvaluateParameters.TargetTags = TargetTags;

    // 获取Vigor大小
    float Vigor = 0.f;
    GetCapturedAttributeMagnitude(ViggorDef, Spec, EvaluateParameters, Vigor);
    Vigor = FMath::Max<float>(Vigor, 0.f);

    //获取Level值
    ICombatInterface* CombatInterface = Cast<ICombatInterface>(Spec.GetContext().GetSourceObject());
    const int32 PlayerLevel = CombatInterface->GetPlayerLevel();

    return 80.f + 2.5*Vigor + 10.f*PlayerLevel;
}

CombatInterface

定义了一个获取Level数值的接口,在CharacterBase中声明实现,在子类中具体实现(Enemy类和PlayerCharacter类),PlayerCharacter的Level存储在PlayerState里面,所以会通过获取到PlayerState再去取Level返回给接口

  • 20
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

楚江_wog1st

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值