0. 개요

캐릭터 레벨을 Get하여 가져오는 함수를 블루프린트 Native 이벤트 함수로 만들고 코드를 수정한다.

 

 

1. 코드 리펙토링

지금까지 캐릭터 레벨을 가져와 게임플레이 어빌리티 혹은 경험치 계산에 사용했다.

그러나 전투 인터페이스를 캐스팅하여 가져와서 체크하고 사용하는 등 일련의 복잡성이 있다.

또한 해당 기능들에게 전투 인터페이스에 대한 의존성을 가지게 한다.

이런 문제를 해결하기 위해 CombatInterface의 GetCharacterLevel을 블루프린트 Native 이벤트로 변경하여 인터페이스를 캐스팅하지 않고 바로 실행하게 한다.

 

(1) CombatInterface의 GetCharacterLevel 함수를 블루프린트 Native 이벤트로 변경

public:
	UFUNCTION(BlueprintNativeEvent)
	int32 GetCharacterLevel();

블루프린트 Native 이벤트 함수의 조건은 다음과 같다.

- virtual로 선언하지 않을 것.

- cpp 파일에서 함수가 구현되지 않아야 함.

	// 전투 인터페이스
	virtual int32 GetCharacterLevel_Implementation() override;
	// 전투 인터페이스 끝
int32 AAuraCharacter::GetCharacterLevel_Implementation()
{
    const AAuraPlayerState* AuraPlayerState = GetPlayerState<AAuraPlayerState>();
    check(AuraPlayerState);

    return AuraPlayerState->GetCharacterLevel();
}

- 반드시 오버라이드하여 사용해야 함.

-> 오버라이드할 때에는 함수 이름 끝에 _Implementation을 붙여야 한다.

 

블루프린트 Native 이벤트 함수를 호출할 때에는 아래와 같이 사용한다.

	int32 SourcePlayerLevel = 1;
	if (SourceAvatar->Implements<UCombatInterface>())
	{
		SourcePlayerLevel = ICombatInterface::Execute_GetCharacterLevel(SourceAvatar);
	}

로컬 변수를 만들어 초기화하고, 해당 캐릭터가 인터페이스를 구현했는지 확인한다.

ICombatInterface를 구현한 객체라면 해당 객체의 GetCharacterLevel을 실행(Execute)한다.

 

이제 컴파일하고 오류가 발생한 모든 부분을 위와 같이 수정한다.