-
[Unreal Engine] IsA : Object type check in C++Unreal Engine 5._ 2023. 2. 17. 14:23
오브젝트에 대한 타입 체크가 필요한 경우, 블루프린트는 아래의 방법을 사용할 수 있습니다.
첫 번째는 위쪽의 Cast노드이고, 두 번째는 아래의 =(Equal) 노드입니다.
이번 글에서는 C++에서 오브젝트의 타입을 체크할 수 있는 기능에 대하여 살펴보도록 하겠습니다.
1. BP to C++
더보기C++또한 위 블루프린트 노드와 유사한 코드를 작성할 수 있습니다.
/* Get actor code, stored in myActor variable */ // Case1 : Cast auto tmp = Cast<AMyObject>(myActor); if(IsValid(tmp)) { } // Case2 : if(myActor == Cast<AMyObject>(AMyObject::StaticClass())) { }
위와 같이 블루프린트의 상황을 흉내낼 수는 있으나, 위 코드는 실행 환경 혹은 오브젝트의 타입에 따라 오작동 할 수 있습니다.
2. IsA
더보기IsA함수는 UObject에 있는 함수입니다.
IsA | Unreal Engine Documentation
인자의 여부에 따라서, 해당 객체의 템플릿 여부를 반환합니다.
구체적으로, 아래와 같은 상황에서 사용할 수 있습니다.
아래 예시의 클래스 A, B, C는 언리얼 Actor타입을 상속받은 클래스라 가정합니다. (언리얼 네이밍 컨벤션은 무시합니다.)
- class A가 있습니다.
- A를 상속받은 B와 C가 있습니다. (class B : public A, class C : public A)
- A를 인자로 받는 특정 함수에서, 해당 인스턴스가 B인지 C인지 확인하려 합니다.
void Function(A* ObjA) { if(A->IsA(B::StaticClass())) { // Logic ( B : A ) }else{ // Logic ( C : A ) } }
위 코드를 통해 상속 구조에서 클래스의 포함 여부를 확인할 수 있습니다.
또한 위와 같은 구조 외에도, 언리얼의 기본 타입인 UObject를 상속받는 클래스에 대하여, 해당 클래스의 구체적인 타입을 확인해야 하는 상황에서도 사용할 수 있습니다.
감사합니다.
'Unreal Engine 5._' 카테고리의 다른 글
[Unreal Engine] Control game speed (Time Dilation) (0) 2023.04.06 [Unreal Engine] SetTimer() : Passing parameters (0) 2023.02.24 [Unreal Engine] UFUNCTION override (BlueprintNativeEvent) (0) 2023.02.07 [Unreal Engine] UFUNCTION UPARAM : Rename, Relocate params (0) 2023.01.31 [Unreal Engine] UFUNCTION override (C++) (0) 2023.01.31