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);
}
}
}