Overview
The FObjectInitializer class helps a lot within Unreal C++ constructors and initializing the properties within it. The class carries information that allows further subclassed constructors to override and/or pass through data to change parent constructor behavior.
This post will go over how to work with this struct and how I found use with it in my projects.
Definition
FObjectInitializer is defined in UObjectGlobals.h as a class but its uses are inline of what Unreal uses for structs in C++ which is why they named it as such.
Unreal generated documentation: https://docs.unrealengine.com/4.27/en-US/API/Runtime/CoreUObject/UObject/FObjectInitializer/
The main use of FObjectInitializers is to control the class or creation of default sub objects that are created in constructors. This can be done in multiple ways through calls to the ObjectInitializer object itself.
Using the ObjectInitializer is very easy and below I will show some examples of how I use this class for project structure.
Stop default subobject from being created in parent constructor:
// Base class constructor UBaseClass::UBaseClass(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer) { ObjectPointer = ObjectInitializer.CreateDefaultSubobject<UBaseObjectType>(this, TEXT("ObjectName")); }
// Derived class constructor UDerivedClass::UDerivedClass(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer .DoNotCreateDefaultSubobject(TEXT("ObjectName"))) { }
Derived class needs to call the DoNotCreateDefaultSubobject function with the incoming ObjectInitializer. This simply stops the "ObjectName" default subobject from being created.
Change class of default subobject made in parent constructor:
One way is to take the above method and simply recreate with your specific class in CreateDefaultSubobject class template parameter.
// Base class constructor UBaseClass::UBaseClass(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer) { ObjectPointer = ObjectInitializer.CreateDefaultSubobject<UBaseObjectType>(this, TEXT("ObjectName")); }
// Derived class constructor UDerivedClass::UDerivedClass(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer .DoNotCreateDefaultSubobject(TEXT("ObjectName"))) { ObjectPointer = ObjectInitializer.CreateDefaultSubobject<UDerivedObjectType>(this, TEXT("NewObjectName")); }
Another way is to call the SetNestedDefaultSubobjectClass function with the incoming ObjectInitializer.
// Derived class constructor UDerivedClass::UDerivedClass(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer .SetNestedDefaultSubobjectClass<UDerivedObjectType>(TEXT("ObjectName")) { }
This will override the base class subobject type to the new derived one.
Example
For my projects, I use the FObjectInitializer when setting up my ability system components and attribute sets.
I have a base attribute set created within the base playerstate and the class of the attribute set gets derived and replaced in my derived playerstate class.


Conclusion
That's pretty much it for ObjectInitializers. They are very straightforward in how they work but it is mostly up to you in how you use it for making your game easier to build on.
Thanks for reading!
Brief overview of the uses of the FObjectInitializer struct in Unreal C++ constructors.