본문 바로가기

강동새싹언리얼수업/언리얼

231212 수업 Interface

https://docs.unrealengine.com/5.3/ko/interfaces-in-unreal-engine/

 

인터페이스

인터페이스 생성 및 구현에 대한 레퍼런스입니다.

docs.unrealengine.com

https://docs.unrealengine.com/5.3/ko/blueprint-interface-in-unreal-engine/

 

블루프린트 인터페이스

블루프린트 간 인터페이스를 정의하기 위한 함수를 선언하는 블루프린트입니다.

docs.unrealengine.com

https://blog.naver.com/dami2you/223235747629

 

Day.27[Unreal Engine](인프런)인터페이스

다중상속이란? 두 가지 이상의 속성을 물려받음 단점, 대상 BP가 두 기능 중 어떤 걸 실행할지 헷갈려 함...

blog.naver.com

 

언리얼 인터페이스를 상속받아 클래스를 만든다. 언리얼에디터에 나타날 UInteractable클래스와 실제 사용할 IIteractable클래스 2개가 생긴다. 여기에 메쏘드 Interact()를 선언만해 주고 구현은 상속받은 쪽에서  _Implementation 붙여 해준다.

그리고 UHT(Unreal Header Tool)에 의해서 Generated된 네트워크 Function들은 _Implementation을 붙인 함수를 통해 구현부를 작성해야 하며, 호출할 때에는 원형을 호출해야 한다.

    //상호작용 함수 선언
UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = "Interactable")
void Interact(AActor* Instigator)

 

Interactable.h

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

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

// This class does not need to be modified.
UINTERFACE(MinimalAPI)
class UInteractable : public UInterface
{
	GENERATED_BODY()
};

/**
 *
 */
class THIRDPERSONTEMPLATE_API IInteractable
{
	GENERATED_BODY()

	// Add interface functions to this class. This is the class that will be inherited to implement this interface.
public:

    //상호작용 함수 선언
	UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = "Interactable")
	void Interact(AActor* Instigator);

};

Interactable.cpp 은 아무것도 입력하지 않는다

// Fill out your copyright notice in the Description page of Project Settings.


#include "Interactable.h"

// Add default functionality here for any IInteractable functions that are not pure virtual.

Actor클래스를 상속시켜

InteractableActor.h를 만들고 IInteractable을 수동으로 상속시켜준다.

아까 선언해준 Interact()가 아닌 Interact_Implementation()을 선언해줘야한다.

https://blog.naver.com/sjyfantasy/222998584892

 

언리얼 엔진 - 리플리케이션(Replication) 2

지난 본문에 이어서 리플리케이션에 대해서 알아보자. 이전 게시물에서는 이른바 NetRole이라는 네트워크...

blog.naver.com

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Interactable.h"
#include "InteractableActor.generated.h"

UCLASS()
class THIRDPERSONTEMPLATE_API AInteractableActor : public AActor, public IInteractable
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	AInteractableActor();

	// IInteractable에서 상속된 함수 구현
	virtual void Interact_Implementation(AActor* OtherActor) override;

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

};

InteractableActor.cpp

// Fill out your copyright notice in the Description page of Project Settings.
#include "InteractableActor.h"

// Sets default values
AInteractableActor::AInteractableActor()
{
	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

}


// Called when the game starts or when spawned
void AInteractableActor::BeginPlay()
{
	Super::BeginPlay();
}

// Called every frame
void AInteractableActor::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
}

//구현
void AInteractableActor::Interact_Implementation(AActor* OtherActor)
{
	// 상호작용 시 행동하는 코드 작성
	// 예: 물체와 상호작용하는 로직
	GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Green, (TEXT("Interface ,%s"), *OtherActor->GetName()));
}

 

이름을 BP_InteractableActor로 하고 간단한 스테틱 메시를 넣어준다. 아무것도 안해도 된다. 이미 C++클래스에서 상속한 Interact_Implementation()이 실행되기 때문이다.

플레이어에 키보드 이벤트를 라이트레이스를에 연결하고 HitActor를 추가한 InterActor와 연결하고 self를 Instigator와 연결해주자 

TPSPlayer 블루프린트

플레이해서 Q를 눌러 레이저를 맞춰보면 Interact_Implementation()이 실행된다.

 

이번에는 블루프린트를 이용해서  interface를 구현해보자

 

 

여기서 부터는 블루프린트 인터페이스를 이용해서 인터페이스를 실현하는 방법이다.

컨텐트드로워에서 우클릭후 블루프린트인터페이스를 선택하고 적당히 BPI_Interface로 만든다

열면 아무것도 없지만 함수를 추가할 수 있다.  OnInteracter를 추가하자 인풋도 설정한다.

IsInteractable도 추가하자 이번에는 Output을 하나 설정해보자

컴파일후 저장한다 이걸로 준비가 끝났다.

아무 액터나 캐릭터에 추가할수 있다. 블루프린트를 열고 Class Settings를 클릭하자. 오른쪽 디테일 Interfaces카테고리 Add를 풀다운메뉴에서 방금만든 인터페이스블프를 골라준다.

 

그럼 바로 밑에 인터페이스가 밑에 추가되고  왼쪽에도 Interfaces에 사용가능한 이벤트들이 보이게 된다. 인터페이스이름에 관련없이 이벤트이름만 보이게 되니 이벤트이름을 유니크하게 사용해야할것 같다.

일단 컴파일후 함수위를 우클릭해 Implement Event를 추가한다.

아래는 같은 그림이다. 수업중 카피한것임

   

 

 우클릭해 Implement event를 선택해서 이벤트를 추가해준다

이렇게하면 이벤트 수신시 프린트가 된다. 이제 아래에서 이벤트를 불러보자

플레이어에 라인트레이스 실행후 OnInteract이벤트를 추가해보자 입력으로 Actor를 연결해준다.

 

'강동새싹언리얼수업 > 언리얼' 카테고리의 다른 글

디자인패턴  (0) 2023.12.13
231212 Observer  (0) 2023.12.12
231211 Design Pattern - 컴포넌트  (0) 2023.12.11
시계만들기  (0) 2023.12.04
수업  (0) 2023.12.04