In UE5, I can use CreateDefaultSubobject to create a subobject within the constructor of…
- User
In UE5, I can use CreateDefaultSubobject to create a subobject within the constructor of e.g. an actor. But, how do I create an object later, for example in PreInitializeComponents?
Assistant
You have a few options to create objects later on in an actor's lifetime in UE5:
- Use
NewObject<>()- This allows you to create a new UObject dynamically at runtime. For example:
UMyComponent* MyComp = NewObject<UMyComponent>(this); MyComp->RegisterComponent();- Use
CreateDefaultSubobject()- You can actually call this multiple times, not just in the constructor. So you can do:
CreateDefaultSubobject<UMyComponent>(TEXT("MyComp"));- Manually construct and initialize - You can use and create objects manually, though this requires manually initializing and cleaning them up:
- Use
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.