ryuikuya
(ryuikuya)
September 25, 2014, 6:14am
1
I want to dynamic create following object:
TSubobjectPtr<USceneComponent> EmptySceneCom = aPCIP->CreateDefaultSubobject<USceneComponent>(this, newName);
But if I did not create the object in init function like following:
APositionIndicator::APositionIndicator(const FPostConstructInitializeProperties& PCIP)
: Super(PCIP)
{
}
It will be crashed. does anyway to create the object in other’s function?
Heroico
(Heroico)
March 26, 2015, 2:54pm
2
I would recommend you to take a look at this:
Object Instance Creation
The thing is that the “DefaultSubobject” is a method with some extra functionality related to the Editor property setup functionality (at least that’s what I use it for). That is: use that method if you want to get the settings you entered at the editor entity inspector.
I usually create my ordinary objects dynamically with something like this:
USceneComponent *Component = ConstructObject<USceneComponent>(USceneComponent::StaticClass(), this, NewName);
Heroico
(Heroico)
June 13, 2015, 8:32pm
3
Update: in UE 4.8,
ConstructObject has been deprecated in favor of NewObject
SDuval
(SDuval)
September 30, 2015, 9:07pm
4
CreateDefaultSubobject can only be called within the UObject constructor.
darkgaze
(darkgaze)
August 19, 2017, 11:48am
5
Never use CreateDefaultSubobject outside a constructor. It will crash the editor (4.17…)
Don’t use: ConstructObject ← Deprecated from 4.9
Use NewObject. parent can be “this” if you are creating the object underneath this one.
How to use :
NewObject < classToReturn > (parent, ComponentClassToSpawn*)
Example:
USceneComponent* var = NewObject<USceneComponent>(this,USphereComponent::StaticClass());
Note USceneC is parent type of USphereC.
Other answers here:
Hi guys, I have a UObject-inheriting class here: As you can see, I have an implementable event there. My idea is that I inherit several classes from this one using blueprints, and implement this event differently in each of them. So, I created a...
Reading time: 2 mins 🕑
Likes: 1 ❤
NewObject docs:
About objects creation: