[UE5.1, Bug Report] UUserWidgetExtension missing initalization.

UUserWidgetExtension is supposed to be initialized by the UUserWidget.
It can be initalized by it during two methods:

  1. UUserWidget::AddExtension.
  2. UUserWidget::NativeOnInitialized.

However, there is a situation in which an extension is never initialized.

void UYourWidget::NativeOnInitialized() {
	Super::NativeOnInitialized();
	
	UYourExtension* Ext = AddExtension<UYourExtension>();
}

UUserWidget::NativeOnInitialized does not initalize the extention because it has not been added yet. UUserWidget::AddExtension does not initialize the extention because bInitialized is set after NativeOnInitialized has completed. Broken! Adding the extension before call to Super::NativeOnInitialized smells.

As a workaround the UUserWidgetExtension::Construct method is called when the extension is added from within UUserWidget::NativeInitialze.

I think you misread the post. It is not about Construct but UUserWidgetExtension::Initialize, which is not called in the above situation. A workaround is this:

void UYourUserWidget::NativeOnInitialized() {
	UYourExtention* Ext = AddExtension<UYourExtention>();
	
	// Super
	Super::NativeOnInitialized();
}

which stinks.

No no, I think I got what you meant.

void UYourWidget::NativeOnInitialized() {
	Super::NativeOnInitialized();
	
	UYourExtension* Ext = AddExtension<UYourExtension>(); // <-- UYourExtension::Construct() is called here
}

So I did my initialization in UYourExtension::Construct(). But that’s much worse than your workaround. Thanks for pointing that out, I had somehow missed that at the time.

But to be honest, I also still didn’t fully understand the difference between the Construct and Initialize events for widgets yet :wink:

Construct is not called there. The Construct method is not the same as the constructor. Different concepts. In the widgets Construct and Initialize have again very different meanings.

It’s ok I wasn’t looking for an answer this post is a bug report :slight_smile: . I draw some attention to issues on the forums cause if I write a report on the bug tracker for every thing I find I’ll be 80 before I’m done.

AddExtension calls both, the extensions Initialize() and its Construct() method. It also does that in this exact order which is not what I would expect.

.