// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "UObject/Interface.h"
#include "LessonInterface.generated.h"
// This class does not need to be modified.
UINTERFACE(MinimalAPI)
class ULessonInterface : public UInterface
{
GENERATED_BODY()
};
/**
*
*/
class UNREALINTERFACE_API ILessonInterface
{
GENERATED_BODY()
// Add interface functions to this class. This is the class that will be inherited to implement this interface.
public:
virtual void DoLessson() = 0;
// or
//virtual void DoLessson() { UE_LOG(LogTemp, Warning, TEXT("수업에 입장합니다.")); }
};
7강: 언리얼 C++ 설계 I - 인터페이스
언리얼 C++ 인터페이스
인터페이스란 객체가 반드시 구현해야 할 행동을 지정하는데 활용되는 타입으로 다형성의 구현과 의존성이 분리된 설계에 유용하게 활용된다.
데이터가 아닌 행동으로 책임에 초점을 맞춘다.
언리얼 엔진에서 게임 콘텐츠를 구성하는 오브젝트의 설계 예시로 월드에 배치되는 모든 오브젝트 중 움직이는 오브젝트에 대해 반드시 움직이게 하고 싶다면
INavAgentInterface
를 상속받아 구현한다. (Pawn)예제를 위한 클래스 다이어그램
수업에 참여하는 사람과 참여하지 않는 사람을 구분하여 이를 인터페이스를 통해 구현해보자.
Student
,Teacher
이며,Staff
은 참여하지 않는다. 이 행동에 대해서ILessonInterface
로 나타낸다.언리얼 C++ 인터페이스 특징
언리얼 C++ 인터페이스는 생성하면 두 개의 클래스가 생성된다.
U
로 시작하는 타입 클래스로 타입의 정보를 제공하는 역할과I
로 시작하는 인터페이스 클래스로 실절적인 설계 및 구현을 담당한다.U
타입 클래스 정보는 런타임에서 인터페이스의 구현 여부를 파악하는 용도로 사용되고, 실제로U
타입에서 작업할 일은 없다. 인터페이스에 관련된 구성 및 구현은I
인터페이스 클래스에서 진행된다.언리얼 C++
은 다른 객체지향 언어와 달리 인터페이스에도 구현이 가능하다.C#
만 써서 과연 인터페이스에도 구현을 하는 것이 좋은가? 라는 생각이 든다. 구현이 들어가는 순간 추상클래스로 전락하고 자율성이 떨어지는 것이 아닌가?class ULessonInterface : public UInterface
이 부분은U
타입 클래스 정보를 제공하는 역할을 한다. 자동으로 생성되는 코드이다. (건드리지 않는다.)ILessonInterface
실제 정의할 행동은I
타입 클래스에서 정의한다.virtual void DoLessson() = 0;
을 통해 순수 가상함수로 만들어 인터페이스를 구현하는 클래스에서 반드시 구현해야 하는 함수임을 명시한다.Super
가 아닌 해당 인터페이스에 직접 접근하여 호출한다.ILessonInterface::DoLessson();
ILessonInterface* LessonInterface = Cast<ILessonInterface>(Person);
캐스팅을 통해 안전하게 인터페이스를 사용할 수 있다. (캐스팅 실패시nullptr
를 반환한다.)정리
Tips
Refresh
를 사용하자.FORCEINLINE
매크로는 인라인 함수를 강제로 만들어주는 매크로이다. 인라인 함수는 함수 호출을 줄여 성능을 향상시키는데, 이 매크로는 인라인 함수로 만들어주는 역할을 한다.#include "person.generated.h"
파일은 항상 가장 밑에 위치해야 한다. 그렇지 않으면 컴파일 에러가 발생한다.