관리 메뉴

Mini

[UE5] 커스텀 속성계산2, null 객체생성방법, mmc클래스 본문

UE5/RPG Attributes

[UE5] 커스텀 속성계산2, null 객체생성방법, mmc클래스

Mini_96 2024. 6. 7. 04:48

1. mmc 클래스생성

 

2.

 */
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;
};
// Fill out your copyright notice in the Description page of Project Settings.


#include "AbilitySystem/ModMagCalc/MMC_MaxHealth.h"

#include "AbilitySystem/AuraAttributeSet.h"
#include "Interaction/CombatInterface.h"

UMMC_MaxHealth::UMMC_MaxHealth()
{
    //VigorDef 채우기
    VigorDef.AttributeToCapture = UAuraAttributeSet::GetVigorAttribute(); //AS클래스에서 getter매크로 사용
    VigorDef.AttributeSource=EGameplayEffectAttributeCaptureSource::Target; //타겟vs소스
    VigorDef.bSnapshot = false; //타이밍
    
    RelevantAttributesToCapture.Add(VigorDef);
}

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 EvaluateParameters;
    EvaluateParameters.TargetTags = TargetTags;
    EvaluateParameters.SourceTags = SourceTags;

    //* vigor과 level을 얻은후, 계산해서 리턴하면됨
    float Vigor=0.f;
    GetCapturedAttributeMagnitude(VigorDef,Spec,EvaluateParameters,Vigor); //Vigor에 Vigor값 넣어줘
    Vigor=FMath::Max<float>(Vigor,0.f);
    
    ICombatInterface* CombatInterface = Cast<ICombatInterface>(Spec.GetContext().GetSourceObject());
    const int32 PlayerLevel = CombatInterface->GetPlayerLevel();

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

 

3. GE에서 커스텀으로 수정

소스도 캡쳐했음

*문제 : IcombatInterface가 널임

* 해결 : 객체생성

캐릭베이스 > ApplyEffectToself함수에서 소스객체 설정하는 부분이 있었음

EffectActor에도 객체생성하는 코드가 있었음

FGameplayEffectContextHandle ContextHandle = GetAbilitySystemComponent()->MakeEffectContext();
ContextHandle.AddSourceObject(this); //캐릭이면 캐릭자체, 적이면 적이 소스임

4. 결과

80 + 2.5*9 + 10*1

 

5. 최대마나설정 실습

결과

 

* 의문 : 최대체력, 마나는 int, vigor, 레벨에 의존성임

아래처럼 하드코딩 안하고 제대로 설정하는 방법? -> 다음시간에

UAuraAttributeSet::UAuraAttributeSet()
{
    //Setter 사용
    InitHealth(10.f);
    //InitMaxHealth(100.f);
    InitMana(10.f);
    //InitMaxMana(100.f);
}

6. 의문해결 실습

i) 캐릭베이스에 추가

UPROPERTY(BlueprintReadOnly, EditAnywhere, Category = "Attributes")
TSubclassOf<UGameplayEffect> DefaultVitalAttributes;

when? : 보조속성 set 후

void AAuraCharacterBase::InitializeDefaultAttributes() const
{
    ApplyEffectToSelf(DefaultPrimaryAttributes, 1.f);
    ApplyEffectToSelf(DefaultSecondaryAttributes, 1.f);
    ApplyEffectToSelf(DefaultVitalAttributes, 1.f);
}

ii) GE_Vital 만들고,

Health = maxHp로 설정

마나도 동일

iii) BP_Aura에서

VitalAttribute의존성을 내가만든 GE로 주입

정상작동

 

https://github.com/DongHoonYu96/GameplayAbilitySystem_Aura/commit/320f2c58552c171dc40af8157f7c4a64a0dfa5e7

 

feat : 커스텀 속성계산 구현, 마나=최대마나로 설정 · DongHoonYu96/GameplayAbilitySystem_Aura@320f2c5

Bisu96 committed Jun 6, 2024

github.com