When exactly does UMG Widget OnInitialized get called?

I’m struggling to find this information in the documentation.

I have the following code:

widgetInstance = CreateWidget<UMyWidget>(this, wMyWidgetType);
widgetInstance->SetCustomProperty(value);

I want to be able to ensure that SetCustomProperty runs before OnInitialized runs in the blueprint layer. I have tested that this works currently, and it can be verified with log statements, but is it actually a safe expectation to hold? I’m not seeing where the specific behavior is documented.

Thanks!

In 4.21, the static method CreateWidget<>() in UserWidget.h calls the BlueprintImplementableEvent OnInitialized() by calling UUserWidget::Initialize().

The exact “call chain” is (everything in UUserWidget):

CreateWidget() → CreateWidgetInstance() → CreateInstanceInternal() → Initialize() → NativeOnInitialized() → OnInitialized()

So no, OnInitialized() should be executed “at the same time” than CreateWidget<>() is called. So the behavior you see seems kind of strange. However, I’m not an expert and just looked through the code, I may not understand everything correctly (for instance I don’t know if the BlueprintImplementableEvents are delayed for next frame, which should explain the behavior you got).

Keep in mind, however, that the call to OnInitialized() in Initialize() is guarded by this line:

if (!IsDesignTime() && PlayerContext.IsValid())

So depending of the context surrounding your code, it may or may not be called.
The Initialize() method is also called by: UUserWidget::PostDuplicate() && UUserWidget::RebuildWidget()

1 Like

Hey, thanks for the response. I’m seeing the same call chain as you. I agree that it doesn’t make sense. I was hoping that one of those actions had something going on I didn’t realize…perhaps, actually just being queued up for after the current execution context or something along those lines.