UWidgetComponent::GetUserWidgetObject() returns 0

I have Actor with UActorComponent and UWidgetComponent.

In BeginPlay() within my ActorComponent I want to get UUserWidget which I setted in UWidgetComponent:

	AActor* owner = GetOwner();
	if (owner)
	{
		UWidgetComponent* w = Cast<UWidgetComponent>(owner->GetComponentByClass(UWidgetComponent::StaticClass()));
		if (w)
			screen_w = Cast<UMyScreenWidget>(w->GetUserWidgetObject());
	}

	if (!screen_w )
		UE_LOG(LogTemp, Error, TEXT("cannot get screen_widget"))

w is non zero and w-GetUserWidgetObject() is 0. It works fine in 4.19. Widget is setted, I can see him in editor.

There’s a typo, right? Your actual code reads screen_w = Cast<UMyScreenWidget>(w->GetUserWidgetObject()) with the -> being typed correctly?

The way you have it pasted, it looks like you’re subtracting instead of de-referencing. Though, I’m not sure that would even compile, so I doubt that’s the issue.

of course its typo, thank you)

Nothing obvious is wrong with the snippet you’ve shared. So, where is this being called from? Maybe the problem is that GetUserWidgetObject() is being called before the widget is fully initialized.

Is it being called during BeginPlay() by chance?

Yes, this is in BeginPlay().
I checked sequence and it turned out that first called this BeginPlay()
then Initialize() and NativeConstruct() of my widget. That is the problem.
How can I fix this?
I have two components and one depends on another and must wait for him.
Is there any build in sync methods? Or I just use Tick() function until widget is ready?

Thats worked! Thank you!

Can you promote your comment into answer, so I can set it accepted?

I haven’t tested this, but why not give UWidgetComponent::InitWidget() a try?

I looked at the engine code for that function, and it basically just calls CreateWidget(...) if the UWidgetComponent’s user widget object is still null. This seems to be exactly what you need.

So, try doing…

UWidgetComponent* w = Cast<UWidgetComponent>(owner->GetComponentByClass(UWidgetComponent::StaticClass()));
if (w)
{
    w->InitWidget();
    screen_w = Cast<UMyScreenWidget>(w->GetUserWidgetObject());
}

…and see if that works?