게임 엔진/Unreal

[Unreal 4] C++을 이용한 오브젝트 생성 정리

niamdank 2022. 6. 9. 21:58

  언리얼 C++를 이용한 오브젝트 생성 

생성자에서 오브젝트 생성

CreateDefaultSubobject를 사용하면 된다.


USphereComponent* SphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("Root"));



※ 생성자 외부에서 이 함수를 사용하면 컴파일 에러가 발생한다.

생성자 외부에서 오브젝트 생성

NewObject로 생성하면 된다. 컴포넌트인 경우 RegisterComponent를 호출해 등록해줘야 한다.


USphereComponent* SphereComponent = NewObject<USphereComponent>(this, TEXT("Root"));
SphereComponent->RegisterComponent();



※ 컴포넌트에 설정은 RegisterComponent 전에 해야 하며 RegisterComponent 이후 설정하려고 하면 에러가 발생한다.

 

더보기

참고문헌

NewObject or CreateDefaultSubobject - Unreal Engine / Programming & Scripting - Unreal Engine Forums

 

NewObject or CreateDefaultSubobject

Hi all, I’d like to know what is the difference between the 2 methods stated in the title. Example: USphereComponent* Component = NewObject<USphereComponent>(this); or USphereComponent* SphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("Roo

forums.unrealengine.com

c++ - Unreal Engine 4. Different ways to instantiate the object - Stack Overflow

 

Unreal Engine 4. Different ways to instantiate the object

I found about four different ways to instantiate the object, but not sure if my understanding is clear. NewObject<T>() function used when we want to make at the instance of UObject. For exam...

stackoverflow.com

Component created with NewObject is invisible - Programming & Scripting / C++ - Unreal Engine Forums

 

Component created with NewObject is invisible

I was creating a sub-object inside my actor’s constructor the usual way: AMyActor::AMyActor(const FObjectInitializer &init) : Super(init) { text_render = CreateDefaultSubobject<UTextRenderComponent>(TEXT("info_text")); } Worked fine. Then I needed to mov

forums.unrealengine.com

When should I call RegisterComponent() and ReregisterComponent()? Should I ever? - Unreal Engine / Programming & Scripting - Unreal Engine Forums

 

When should I call RegisterComponent() and ReregisterComponent()? Should I ever?

I’ve seen several snippets/examples where, after attaching components, NewlyAttachedComponent->RegisterComponent() is called. In my experience, this has caused issues and has in some cases triggered breakpoints. Can I get a definitive answer on whether I

forums.unrealengine.com