UObject is still nullptr after being initialized in constructor

shue_it
Mon, 20 Nov 2023 09:50:21 +0000
C++

Hello o/

I have two C++ classes (let say A and B class) which inherit from the same C++ parent class (let say AZ), which contains a custom Data variable (UMyData, which just inherit from UObject)

In each constructor of those 2 C++ classes, I’m making a new instance of this Data by calling NewObject function, like below.

AA::AA()
{
   Data = NewObject<UMyData>();
   //...
}

AB::AB()
{
   Data = NewObject<UMyData>();
   //...
}

AA class doesn’t have any BP inheriting from it. It’s being spawned in the Scene in full C++ with SpawnActor function, and it works perfectly.

Now, AB class has a BP child class (let say BP_AB), and this BP child class is being spawned in the world from another BP with the SpawnActor node. And… my Data variable is nullptr/not valid even just after being spawned.
I just don’t understand why. The C++ constructor is called, the BP constructor script is empty, and Data is NOT nullptr in the C++ constructor function. But once we leave the constructor function, it’s back to nullptr and I don’t have any clue why.

BP spawn call

C++ constructor

Result :

AB is an already existing BP class I reparent from AActor to my C++ AZ class (which inherit at some point from AActor, so no problem here), and there was no conflict what so ever.

For now I dodge the problem by setting the Data variable in the BeginPlay function, but it makes no sense from my pov that it works for AA C++ only class and doesn’t for BP_AB (BP_SplineActor in the screenshots)

If anyone has a clue regarding why it behaves like that…

4 posts - 3 participants