Unreal/언리얼 엔진 5로 개발하는 멀티플레이어 게임(Book)

[Unreal] US_Interactable.h 코드 분석(인터페이스)

suppresswisely 2025. 3. 29. 14:35

US_Interactable.h

#include "CoreMinimal.h"
#include "UObject/Interface.h"
#include "US_Interactable.generated.h"

UINTERFACE(MinimalAPI, Blueprintable)
class UUS_Interactable : public UInterface
{
	GENERATED_BODY()
};

class UNREALSHADOWS_LOTL_API IUS_Interactable
{
	GENERATED_BODY()

public:
	UFUNCTION(BlueprintCallable, BlueprintNativeEvent, Category = "Interaction", meta = (DisplayName = "Interact"))
	void Interact(class AUS_Character* CharacterInstigator);

	UFUNCTION(BlueprintCallable, BlueprintNativeEvent, Category = "Interaction", meta = (DisplayName = "Can Interact"))
	bool CanInteract(AUS_Character* CharacterInstigator) const;
};

 

언리얼 엔진에서는 인터페이스를 올바르게 선언하려면 2개의 클래스를 선언해야 한다.

  • U 접두사가 있고 UInterface를 확장하는 클래스: 실제 인터페이스는 아니지만, 언리얼 엔진 시스템에서 클래스를 볼 수 있게 하는 빈 클래스다.
  • I 접두사가 있는 클래스: 실제 인터페이스로 인터페이스 멤버 함수의 정의를 모두 포함한다.

보다시피 U 접두사가 붙은 클래스는 UINTERFACE() 매크로가 지정돼 있으며, Blueprintable 어트리뷰트 때문에 블루프린트에서 이 인터페이스를 구현할 수 있다.

 

마지막으로, Interact()와 CanInteract()라는 2개의 멤버 함수를 각각 선언한다. 이 두 함수는 BlueprintCallable과  BlueprintNativeEvent 어트리뷰트 덕분에 블루프린트에서 호출하고 구현할 수 있다.