본문 바로가기
Unreal/Manual

Unreal C++ Component BeginOverlap, EndOverlap 구현

by Dev_카페인 2023. 12. 2.
반응형

[Unreal/C++] Component BeginOverlap, EndOverlap 구현

 

Component에는 OnComponentBeginOverlap과 OnComponentEndOverlap이 구현되어 있습니다.

이는 UPrimitiveComponent를 상속받는 오브젝트에서 사용할 수 있으며, 구현 과정이 까다롭지 않습니다.

 

먼저 UPrimitiveComponent의 구현된 함수내용을 확인해보면

FComponentBeginOverlapSignature OnComponentBeginOverlap;

FComponentEndOverlapSignature OnComponentEndOverlap;

가 구현되어 있습니다.

// PrimitiveComponent.h
	/** 
	 *	Event called when something starts to overlaps this component, for example a player walking into a trigger.
	 *	For events when objects have a blocking collision, for example a player hitting a wall, see 'Hit' events.
	 *
	 *	@note Both this component and the other one must have GetGenerateOverlapEvents() set to true to generate overlap events.
	 *	@note When receiving an overlap from another object's movement, the directions of 'Hit.Normal' and 'Hit.ImpactNormal'
	 *	will be adjusted to indicate force from the other object against this object.
	 */
	UPROPERTY(BlueprintAssignable, Category="Collision")
	FComponentBeginOverlapSignature OnComponentBeginOverlap;

	/** 
	 *	Event called when something stops overlapping this component 
	 *	@note Both this component and the other one must have GetGenerateOverlapEvents() set to true to generate overlap events.
	 */
	UPROPERTY(BlueprintAssignable, Category="Collision")
	FComponentEndOverlapSignature OnComponentEndOverlap;

이 시그니쳐를 확인해보면 아래와 같이 Dynamic Multicast 델리게이트로 구현되어 있는데

BeginOverlap은 6개의 파라미터, EndOverlap은 4개의 파라미터를 사용하고 있는 것을 확인할 수 있습니다.

// PrimitiveComponent.h

/** Delegate for notification of start of overlap with a specific component */
DECLARE_DYNAMIC_MULTICAST_SPARSE_DELEGATE_SixParams( FComponentBeginOverlapSignature, UPrimitiveComponent, OnComponentBeginOverlap, UPrimitiveComponent*, OverlappedComponent, AActor*, OtherActor, UPrimitiveComponent*, OtherComp, int32, OtherBodyIndex, bool, bFromSweep, const FHitResult &, SweepResult);
/** Delegate for notification of end of overlap with a specific component */
DECLARE_DYNAMIC_MULTICAST_SPARSE_DELEGATE_FourParams( FComponentEndOverlapSignature, UPrimitiveComponent, OnComponentEndOverlap, UPrimitiveComponent*, OverlappedComponent, AActor*, OtherActor, UPrimitiveComponent*, OtherComp, int32, OtherBodyIndex);

 

 

델리게이트 이벤트를 수신하려면 위 함수의 파라미터를 그대로 사용하는 것이 좋습니다.

시그니쳐 이름(FComponentBeginOverlapSignature), 클래스이름(UPrimitiveComponent), 함수 이름(OnComponentBeginOverlap)을 제외하고 나머지 인자를 복사해서 클래스에 수신할 함수를 선언합니다.

 

위 파라미터를 복사할 때 Type과 변수 사이에 있는 콤마(',')는 제거해야 합니다.

// MyClass.h
private :
	UFUNCTION()
		void OnBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);

	UFUNCTION()
		void OnEndOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex);
        
        
private :
	UPROPERTY(VisibleDefaultsOnly)
		class USphereComponent* SphereCollision;	// 컴포넌트 이벤트 연결 주체용 SphereComponent

선언을 마친 후 생성자나 BeginPlay에서 델리게이트를 연결해줍니다.

연결 시 AddDynamic은 인텔리센스가 작동하지 않을 수 있습니다.

// MyClass.cpp

MyClass::MyClass()
{
	SphereCollision = CreateDefaultSubobject<USphereComponent>(TEXT("SphereCollision"));
	RootComponent = SphereCollision;
    
	SphereCollision->OnComponentBeginOverlap.AddDynamic(this, &MyClass::OnBeginOverlap);
	SphereCollision->OnComponentEndOverlap.AddDynamic(this, &MyClass::OnEndOverlap);
}


void MyClass::OnBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
	// 구현
}

void MyClass::OnEndOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
	// 구현
}

 

Overlap이벤트가 작동하지 않는 경우 주체와 대상 콜리전 옵션 중 오버랩 이벤트 생성 항목에 체크되어 있는지 확인합니다.

반응형