게임 개발 공부/Unreal Engine

리플렉션 등록과 변수 리플렉션

Vetenir 2025. 1. 21. 21:05

리플렉션

  • C++ 클래스의 변수와 함수 정보를 에디터나 블루프린트에서 활용할 수 있게 만들어 주는 기능.
  • 리플렉션 기능으로 코드를 직접 다루지 않아 다른 팀원들이 직관적으로 조정 가능.
  • 에디터와 블루프린트에서 변경이 가능해서 테스트를 빠르게 할 수 있습니다.
  • 개발 효율과 협업 효과를 극대화 할 수 있습니다.

 

사용 예시 코드

Item.h

#pragma once

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

UCLASS()
class SPARTAPROJECT_API AItem : public AActor
{
	GENERATED_BODY()
	
public:	
	AItem();
		
protected:
	// Root Scene Component, 에디터에서 볼 수만 있고 수정 불가
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category="Item|Components")
	USceneComponent* SceneRoot;	
	// Static Mesh, 에디터와 Blueprint에서 수정 가능
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Item|Components")
	UStaticMeshComponent* StaticMeshComp;

	// 회전 속도, 클래스 기본값만 수정 가능
	UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="Item|Properties")
	float RotationSpeed;

	virtual void BeginPlay() override;
	virtual void Tick(float DeltaTime) override;
};

 

리플렉션에 필요한 코드

#include "Item.generated.h"
  • 언리얼 엔진이 자동 생성하는 헤더 파일, 클래스의 리플렉션 및 엔진 통합에 필요한 코드가 들어있습니다.
  • 헤더 파일의 가장 마지막 #include 구문 아래에 위치해야 합니다. (다른 #include 보다 아래에 오지 않으면 빌드 에러 위험.

 

UCLASS()
  • 해당 클래스를 언리얼 엔진의 리플렉션 시스템에 등록한다는 의미
  • 이 매크로가 있어야 블루프린트 등 에디터 차원에서 이 클래스를 인식하고 사용할 수 있습니다.

UCLASS()의 옵션

UCLASS(Blueprintable, BlueprintType)
  • = UCLASS()
  • 블루프린트에서 상속 가능한 클래스로 만듭니다.
UCLASS(NotBlueprintable, BlueprintType)
  • 블루프린트에서 이 클래스를 상속할 수 없도록 합니다.
UCLASS(BlueprintType)
  • 블루프린트에서 변수나 참조로 사용할 수 있게 합니다.
  • 상속은 불가능.

 

GENERATED_BODY()
  • 언리얼의 코드 생성 도구가 사용하는 코드를 삽입하는 역할을 합니다.
  • 클래스 내부에 필요한 리플렉션 정보를 자동으로 생성해 줍니다.

 

변수 리플렉션

UPROPERTY() 매크로

UPROPERTY(편집 가능 범위 지정자, Blueprint 접근성 지정자, Category 지정자)
  • 변수의 앞에 UPROPERTY() 작성하여 설정
	// Root Scene Component, 에디터에서 볼 수만 있고 수정 불가
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category="Item|Components")
	USceneComponent* SceneRoot;	
	// Static Mesh, 에디터와 Blueprint에서 수정 가능
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Item|Components")
	UStaticMeshComponent* StaticMeshComp;

	// 회전 속도, 클래스 기본값만 수정 가능
	UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Item|Properties", meta = (ClampMin = "0.0", ClampMax = "90.0"))
	float RotationSpeed;

 

추가로 최솟값과 최댓값을 제한할 수도 있습니다.

UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Item|Properties", meta = (ClampMin = "0.0", ClampMax = "90.0"))

최솟값, 최댓값을 제한

 

참고

 

https://dev.epicgames.com/documentation/en-us/unreal-engine/API/Plugins/UVEditorTools/UUVEditorSeamToolProperties/ClampMin