[UE5] FindComponentByClass() & nullptr

ChangJin·2023년 10월 4일
0

Unreal Engine5

목록 보기
13/102
post-thumbnail

FindComponentByClass() & nullptr

https://docs.unrealengine.com/5.2/en-US/API/Runtime/Engine/PhysicsEngine/UPhysicsHandleComponent/

  • PhysicsHandle 이라는 컴포넌트가 있는데, 이 컴포넌트는 실제로 물체를 잡고 게임내에서 원하는 위치를 알려주되 물리적 제약을 인식하여 벽을 통과하는 일이 없도록 해줍니다.
  • 그리고 물체는 우리가 지정한 특정 지점을 곧이곧대로 따라가기보다 마치 용수철 위에 있는 것처럼 움직이게 됩니다.
  • blueprint에서 +Add를 눌러서 PhysicsHandle을 추가하면 됩니다.

  • 이건 씬 컴포넌트가 아니라 액터 컴포넌트입니다.
UPhysicsHandleComponent* PhysicsHandle = GetOwner()->FindComponentByClass<UPhysicsHandleComponent>();
	// 충돌 일으키기
	PhysicsHandle = nullptr;
	if (PhysicsHandle != nullptr)
	{
		PhysicsHandle->GetName();
	}
  • 포인터가 null 값이 되면 충돌로 이어질 수 있기 때문에 위험합니다.

  • 컴포넌트 이름을 출력하기

// Called when the game starts
void UGrabber::BeginPlay()
{
	Super::BeginPlay();

	UPhysicsHandleComponent* PhysicsHandle = GetOwner()->FindComponentByClass<UPhysicsHandleComponent>();

	if (PhysicsHandle != nullptr)
	{
		UE_LOG(LogTemp, Display, TEXT("Got Physics Handle: %s"), *PhysicsHandle->GetName());
		PhysicsHandle->GetName();
	}
	else
	{
		UE_LOG(LogTemp, Warning, TEXT("No Physics Handle Found!"));
		// PhysicsHandle->DestroyComponent();
	}
}
  • blueprint에서 PhysicsHandle을 제거하면 Output log에서 No Physics Handle Found! 라는 경고 문구를 확인할 수 있습니다



profile
Unreal Engine 클라이언트 개발자

0개의 댓글