본문 바로가기

Unreal Engine 4/C++

<Unreal C++> 14 - Component Begin & End Overlap

 

필요한 개념


- ActorBeginOverlap과 차이는 거의 없다.

- 그런데 함수 파라미터가 좀 많다.

 

BP 함수와 같음

 

 

 

C02_ComponentBeginOverlap.h


더보기
#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "C02_ComponentBeginOverlap.generated.h"

UCLASS()
class UONLINE_03_CPP_API AC02_ComponentBeginOverlap : public AActor
{
	GENERATED_BODY()

private:
	// 컴포넌트를 3개 넣어놈

	// USceneComponent : 부모 자식 관계를 정의할 수 있는 Transform만 소유한 컴포넌트
	UPROPERTY(VisibleDefaultsOnly)
		class USceneComponent* Scene;
	
	
	// 충돌체를 정의할
	UPROPERTY(VisibleDefaultsOnly)
		class UBoxComponent* Box;


	// 텍스트 렌더링하는 역할 컴포넌트
	UPROPERTY(VisibleDefaultsOnly)
		class UTextRenderComponent* Text;


	// Overlap 주면 포인트라이트 효과줄려고
	UPROPERTY(VisibleDefaultsOnly)
		class UPointLightComponent* PointLight;


public:	
	AC02_ComponentBeginOverlap();

protected:
	virtual void BeginPlay() override;


private:
	// 블루프린트의 OnComponentBeginOverlap도 C++에 정의되어 있는 것을 사용하므로
	// 파라미터는 C++과 동일하다.
	UFUNCTION()
		void ComponentBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
	UFUNCTION()
		void ComponentEndOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex);
};

 

 

 

 

 

 

 

C02_ComponentBeginOverlap.cpp


더보기
#include "C02_ComponentBeginOverlap.h"
#include "Global.h"
#include "Components/BoxComponent.h" // SceneComponent는 Box에 대한 부모여서 헤더를 물고들어와서 인클루드 안해도됨
#include "Components/TextRenderComponent.h" 
#include "Components/PointLightComponent.h"

AC02_ComponentBeginOverlap::AC02_ComponentBeginOverlap()
{
	CHelpers::CreateComponent<USceneComponent>(this, &Scene, "Scene");
	CHelpers::CreateComponent<UBoxComponent>(this, &Box, "Box", Scene);
	CHelpers::CreateComponent<UTextRenderComponent>(this, &Text, "Text", Scene);
	CHelpers::CreateComponent<UPointLightComponent>(this, &PointLight, "PointLight", Scene);

	Box->SetRelativeScale3D(FVector(3));
	Box->bHiddenInGame = false;
	
	Text->SetRelativeLocation(FVector(0, 0, 100));
	Text->SetRelativeRotation(FRotator(0, 180, 0));
	Text->SetRelativeScale3D(FVector(2));
	Text->TextRenderColor = FColor::Red;
	Text->HorizontalAlignment = EHorizTextAligment::EHTA_Center;
	Text->Text = FText::FromString(GetName());

	PointLight->SetLightColor(FLinearColor::Red);

}

void AC02_ComponentBeginOverlap::BeginPlay()
{
	Super::BeginPlay();

	PointLight->SetVisibility(false);
	
	// 델리게이트는 C에서는 함수앞에 On이 붙는다.
	// OnComponentBeginOverlap() 파고 들어가면
	//UPROPERTY(BlueprintAssignable, Category = "Collision")
	//	FComponentBeginOverlapSignature OnComponentBeginOverlap;	
	// FComponentBeginOverlapSignature로 정의되어있다.
	// 1 : 자료형 2 : 어떤 이름으로 쓸지 3 ~ : (우리가 가져다 쓸)뒤가 파라미터들
	// FComponentBeginOverlapSignature, UPrimitiveComponent, OnComponentBeginOverlap 이 부분 뒤부터 우리가 가져다쓸 파라미터 영역
	// DECLARE_DYNAMIC_MULTICAST_SPARSE_DELEGATE_SixParams( FComponentBeginOverlapSignature, UPrimitiveComponent, OnComponentBeginOverlap, UPrimitiveComponent*, OverlappedComponent, AActor*, OtherActor, UPrimitiveComponent*, OtherComp, int32, OtherBodyIndex, bool, bFromSweep, const FHitResult &, SweepResult);
	
	Box->OnComponentBeginOverlap.AddDynamic(this, &AC02_ComponentBeginOverlap::ComponentBeginOverlap);
	
	// End도 파라미터 좀 다름(들어가서 꺼내오면 된다.)
	Box->OnComponentEndOverlap.AddDynamic(this, &AC02_ComponentBeginOverlap::ComponentEndOverlap);
}

void AC02_ComponentBeginOverlap::ComponentBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
	PointLight->SetVisibility(true);
}

void AC02_ComponentBeginOverlap::ComponentEndOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
	PointLight->SetVisibility(false);
}

 

 

 

 

 

결과


들어가면 PointLight가 켜지고 나가면 꺼지는 것을 볼 수 있다.