[Repro] Adding UUserWidget To Viewport on BeginPlay Causes Crash On Clients

This occurs in Older builds of the engine as well, as far back as when UUserWidget was even a thing (4.5 I believe)

On BeginPlay in my HUD, I’m creating some widgets and attempting to add them to the Viewport. If I do this, any client that I run in the editor AND in a standalone game causes a crash at Actor->GetWorld(). I previously did this in Blueprint, and had to use a Delay node to prevent this crash from happening.

I suspect this is because it’s trying to get a World from the PlayerController I’m passing in, whereby the World may not exist yet.

In C++, I’m now forced to create a timer to delay the creation of the widgets, to make sure there is enough time for the World to be created. This seems to be a fundamental issue with the way in which UserWidgets are never bound to a world (indeed, they remain in the viewport even if the game has moved to a different level entirely).

I would have thought this is a pretty regular place for people to want to create Widgets, at the start of the game - so this has to be a bug right? Anything I need to do at the start of the game for my widgets ALSO has to be delayed, so this isn’t a very nice workflow.

This bug report is also linked to previous reports I have made, which have gone unresolved for now (I now have a reliable repro).

Here is the code I’m using (In my HUD class) to spawn my Widgets. The same behaviour can be achieved using Blueprints too. Simply create a user widget (pass in Owning Playing) and add it to viewport on Begin Play in the HUD - then start a multiplayer game in the editor.

void ABZGame_InGameHUD::BeginPlay()
{
	Super::BeginPlay();

	OwningBZPlayer = Cast<ABZGame_PlayerController>(GetOwningPlayerController());
	CreateGameWidgets();
}

void ABZGame_InGameHUD::CreateGameWidgets()
{
	if (OwningBZPlayer)
	{
		ActiveGameWidget = CreateWidget<UBZGame_InGameWidget>(OwningBZPlayer, GameWidget);
		if (ActiveGameWidget)
		{
			ActiveGameWidget->SetHUDReference(this);
			ActiveGameWidget->AddToViewport(0);
		}
	}
}

Hello TheJamsh,

I was unable to reproduce this issue in blueprints with the information provided. I would like to get a set of steps with blueprints only if possible (there is a current issue dealing with widgets made in C++) to eliminate any extraneous factors.

If this is not possible I will be more than happy to try and reproduce this using code.

Quick questions:

  1. Can you reproduce this issue in a clean project?
  2. If so, could you provide a more detailed list of steps in order to reproduce this issue on our end?
  3. Could you provide screen shots of the blueprints involved?
  4. Could you provided the crash that you get?

Hello TheJamsh,

We have not heard back from you in a few days, so we are marking this post as Resolved for tracking purposes. If you are still experiencing the issue you reported, please respond to this message with additional information and we will offer further assistance.

Thank you.