UCLASS(Abstract, EditInlineNew)
class MYGAME_API UBaseClassObject : public UObject
{
GENERATED_BODY()
public:
UBaseClassObject();
}
I have a concrete sub-class:
UCLASS()
class MYGAME_API UConcreteClass : public UBaseClassObject
{
GENERATED_BODY()
public:
UConcreteClass();
};
Here’s an actor that has a pointer to the base class:
UCLASS()
class MYGAME_API AUserActor : public AActor
{
GENERATED_BODY()
public:
UPROPERTY(EditAnywhere, Instanced)
UBaseClassObject * PolymorphicObject;
AUserActor();
};
In the editor I can see that I have a drop down of None or my UConcreteClass as choice. I can successfully chose the sub-class but the problem is that during the constructor of the AUserActor, the base class pointer becomes null even though I have set it to the sub-class during editing time. Why is this happening?
AProceduralMeshActor::AProceduralMeshActor()
{
if (PolymorphicObject != nullptr) //this line is always false ... PolymorphicObject is always null
{
//Do something with PolymorphicObject
}
}
What if you tried with the FObjectInitializer constructor variant? It seems expected to have it as null in case of default constructor as there’s nothing there to set it.
Very good suggestion! Unfortunately it did not work. At first I thought this must be it but now I’m noticing even after I save the object with the concrete sub-class, upon reopening the editor, it is also left to nullptr! Why is this happening?
I checked the AActor constructor and it seems like it couldn’t have helped anyway:
AActor::AActor()
{
InitializeDefaults();
}
AActor::AActor(const FObjectInitializer& ObjectInitializer)
{
// Forward to default constructor (we don't use ObjectInitializer for anything, this is for compatibility with inherited classes that call Super( ObjectInitializer )
InitializeDefaults();
}
That’s because the C++ constructor is the very first thing that is executed when an instance is created, it’s before any of the property system code can kick in. You shouldn’t be putting anything in your constructor except assigning default values and maybe creating default subobjects.
Any logic dependent on property values needs to be executed later, probably in either OnConstruction (if you need your class to react immediately to changes in-editor) or in BeginPlay (if it’s just something that needs executing when the game starts).
Yeah BeginPlay is basically the last initialization function called, so all components should have been initialized by that stage. OnConstruction is more of an in-editor procedural setup thing, it gets triggered anytime you change any property of an actor in the editor.
Don’t know about the physics, can only suggest you check that all physics related properties have been initialized as you want them.