언리얼 C++ 인터페이스의 선언과 활용
- 언리얼 C++ 인터페이스 클래스를 사용해 안정적으로 클래스를 설계.
언리얼 C++ 인터페이스
- 설계 예시
- Actor: 월드에 배치되는 모든 오브젝트.(안 움직이는 오브젝트 포함)
- Pawn: 움직이는 오브젝트
- INavAgentInterface 인터페이스를 구현한 Pawn: 길찾기 시스템을 반드시 사용하면서 움직이는 오브젝트
#include "MyGameInstance.h"
#include "Student.h"
#include "Teacher.h"
#include "Staff.h"
UMyGameInstance::UMyGameInstance()
{
SchoolName = TEXT("기본학교");
}
void UMyGameInstance::Init()
{
Super::Init();
UE_LOG(LogTemp, Log, TEXT("============================"));
TArray<UPerson*> Persons = { NewObject<UStudent>(), NewObject<UTeacher>(), NewObject<UStaff>() };
for (const auto Person : Persons)
{
UE_LOG(LogTemp, Log, TEXT("구성원 이름: %s"), *Person->GetName());
}
UE_LOG(LogTemp, Log, TEXT("============================"));
for (const auto Person : Persons)
{
ILessonInterface* LessonInterface = Cast<ILessonInterface>(Person);
if (LessonInterface)
{
UE_LOG(LogTemp, Log, TEXT("%s님은 수업에 참여할 수 있습니다."), *Person->GetName());
LessonInterface->DoLesson();
}
else
{
UE_LOG(LogTemp, Log, TEXT("%s님은 수업에 참여할 수 없습니다."), *Person->GetName());
}
}
UE_LOG(LogTemp, Log, TEXT("============================"));
}
![업로드중..]()
정리
- 언리얼 C++ 인터페이스는 클래스가 반드시 구현해야 하는 기능을 지정하는데 사용함.
- 언리얼 C++ 인터페이스는 두 개의 클래스를 생성한다.
- 언리얼 C++ 인터페이스는 추상 타입으로 강제되지 않고, 내부에 기본 함수를 구현할 수 있다.
- 언리얼 C++ 인터페이스를 사용하면, 클래스가 수행해야 할 의무를 명시적으로 지정할 수 있어 좋은 객체 설계를 만드는데 도움을 줄 수 있다.