[UE4] Static/Dynamic Object 생성 방법.


* SkeletalMesh를 이미 UE4 Editor를 통해 만들었을 때를 기준으로 함


튜토리얼 및 대부분의 정보는 AActor클래스를 상속받은 액터 클래스 내부에서 ConstructorHelper::FObjectFinder를 통하여 생성된 Mesh를 읽어들이게 가이드 하고 있다.

  1. {  
  2.     static ConstructorHelpers::FObjectFinder< USkeletalMesh > NewMesh( TEXT( "/Game/SkeletalMeshPath" ) );  
  3.     if( NewMesh.Succeeded() )  
  4.     {  
  5.         /* below code is using if this block is class of component */  
  6.         SetSkeletalMesh( NewMesh.Object );  
  7.         /* If this code block is class of actor, should use like below code. 
  8.             Component->SetSkeletalMesh( NewMesh.Object ); 
  9.         */  
  10.     }  
  11. }  


문제는 모든 메시별 클래스를 만들어줄 것인가? 에서 부터 시작되었는데, 만들어진 Mesh Path의 문자열만 가지고 Component object를 위한 Skeletal Mesh를 Actor loading단계에서 불러오고 싶은데, ConstructorHelper::FObjectFinder를 사용하게 되면 다음의 Crash 에 직면하게 된다.

(이런건 좀 튜토리얼 레벨에서 알려주면 안되나...)

  1. void ConstructorHelpers::CheckIfIsInConstructor(const TCHAR* ObjectToFind)  
  2. {  
  3.     auto& ThreadContext = FUObjectThreadContext::Get();  
  4.     UE_CLOG(!ThreadContext.IsInConstructor, LogUObjectGlobals, Fatal, TEXT("FObjectFinders can't be used outside of constructors to find %s"), ObjectToFind);  
  5. }  


해결방법은 다음의 소스코드를 사용하면 해결된다. (삽질한 시간대비 참 허무한 결과...)

  1. void UEquipment::SetContentMesh( const TCHAR* ContentPath )  
  2. {  
  3.     USkeletalMesh* NewMesh = Cast< USkeletalMesh >( StaticLoadObject( USkeletalMesh::StaticClass(), NULL, ContentPath ) );  
  4.     if( NewMesh )  
  5.     {  
  6.         SetSkeletalMesh( NewMesh );  
  7.     }  
  8. }  



참고자료 : https://wiki.unrealengine.com/Dynamic_Load_Object