For anyone who comes to this I had to edit @Vanoric’s answer a bit.
Source/MyProject/Public/DynamicFunctionLibrary.h
#pragma once
#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "DynamicFunctionLibrary.generated.h"
class MyProject UDynamicFunctionLibrary : public UBlueprintFunctionLibrary
{
GENERATED_BODY()
public:
UFUNCTION(BlueprintCallable)
static void AddWidgetToViewport(UGameInstance* GameInstance, UUserWidget* Widget, int32 ZOrder);
UFUNCTION(BlueprintCallable)
static void RemoveWidgetFromViewport(UGameInstance* GameInstance, UUserWidget* Widget);
}
Source/MyProject/Private/DynamicFunctionLibrary.cpp
#include "DynamicFunctionLibrary.h"
#include "Blueprint/UserWidget.h"
#include "Engine/GameInstance.h"
void UDynamicFunctionLibrary::AddWidgetToViewport(UGameInstance* GameInstance, UUserWidget* Widget, int32 ZOrder)
{
UGameViewportClient* client = GameInstance->GetGameViewportClient();
client->AddViewportWidgetContent(Widget->TakeWidget(), ZOrder);
}
void UDynamicFunctionLibrary::RemoveWidgetFromViewport(UGameInstance* GameInstance, UUserWidget* Widget)
{
UGameViewportClient* client = GameInstance->GetGameViewportClient();
client->RemoveViewportWidgetContent(Widget->TakeWidget());
}
I made a function library which has two functions for adding and removing a widget from the viewport. This version correctly passes the ZOrder
argument and allows you to use it in more places, if you want. My loading screen persists between server travel now by simply swapping the AddToViewport
and RemoveFromParent
calls with the respective functions written above.