Download

Why do I need "FObjectInitializer"?

Dear experts,

I made a class that extends UUserWidget

class TEST_API UInventorySlot : public UUserWidget
{
	GENERATED_BODY()

	public:

	//constructor
	UInventorySlot(const FObjectInitializer& ObjectInitializer);
}

I had trouble getting the Constructor right, this is what it looks in the .cpp file:

UInventorySlot::UInventorySlot(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
{
....
}

It is compiling and working but my question is: why do I need this “FObjectInitializer”? I can see in “UserWidget.h” it is using the same syntax:

UUserWidget(const FObjectInitializer& ObjectInitializer);

Why does it not just have a “normal” constructor?

Thank you, Peter

If you look at Gameplay Classes | Unreal Engine Documentation you’ll see they talk about the purpose of FObjectInitializer. I’ve used it previously when I wanted to override a base classes component with a child of that component.

You don’t have to use FObjectInitializer, and you can just use a normal constructor, though I prefer to use it because basically everywhere else in the engine does

I think for UUserWidget you have to use that FObjectInitializer. Thank you for the link. I will read through that