Loading screen c++

Hi,

how can i make a loading screen in c++ and show it by a blueprint function?

thanks in advance,

Hi. You have to get a GameViewportClient instance first - it is accessible within the GameInstance or from an ULocalPlayer. Then you can use GameViewportClient->AddViewportWidgetContent(SlateWidget, 0); to add a global widget with your loading screen over viewport. To hide a screen you can use RemoveViewportWidgetContent. If you needed to hide a world while loading you can set bDisableWorldRendering on a viewport client - it will completely inhibit world rendering.

im mostly using blueprints im not really familiar with C++ should i create a new c++ class of GameViewportClient or can i get that from a blueprint Game instance?

You can use the same things in blueprint, but node required to add a widget to viewport is related to the widget itself.

230822-wv.png

Also remove widget node is different and named Remove from parent (related to a widget too)

the problem in blueprint is when you show a widget and then do open level it will be removed automatically but it need to stay until i want to remove it

So… it seems I was wrong about blueprints, sorry. You can create your own class of UGameInstance (inside UE it called GameInstance without U prefix). Actually you can try to attach these two delegates:
FCoreUObjectDelegates::PreLoadMap
FCoreUObjectDelegates::PostLoadMap
to track when map is loaded and unloaded.
To hold your code you can subclass a GameInstance (and then change active game instance to yours and restart the editor) and attach both these delegates to it. Game viewport is accessible from the game instance all time it’s existing, even between level loadings. This class represents a local application running on your computer actually.

You have to do something like this:

void UMyGameInstance::Init()
{
    Super::Init();
    FCoreUObjectDelegates::PreLoadMap.AddUObject(this, &UMyGameInstance::SetupLoadingScreen)
    FCoreUObjectDelegates::PostLoadMap(this, &UMyGameInstance::RemoveLoadingScreen)
}

void UMyGameInstance::SetupLoadingScreen()
{
    if (UGameViewportClient* ViewportClient = GetGameViewportClient())
    {
        SlateWidget = ...Spawn some widget...
        ViewportClient->AddViewportWidgetContent(SlateWidget, 0);
    }
}

void UMyGameInstance::RemoveLoadingScreen()
{
    if (UGameViewportClient* ViewportClient = GetGameViewportClient())
    {
        SlateWidget = ...Get reference to added widget here...
        ViewportClient->RemoveViewportWidgetContent(SlateWidget);
    }
}

Actually you can use UUserWidget class to spawn an UMG widget. To convert it to a slate widget just use it’s TakeWidget() function.

Hi,

isn’t it possible to pass a UMG widget to these GameInstance functions

It’s possible of course. It’s handy to use existing UE configuration system. To do that you have to specify following arguments for UCLASS macro of your newly created instance
UCLASS(config = EditorPerProjectUserSettings, defaultconfig)
Then, in the instance class you can create a property that will hold a class of UMG widget you need. It declared like this:

	UPROPERTY(config, EditAnywhere, Category = Game)
	TSubclassOf<class UUserWidget> LoadingScreenAsset;

“config” flag informs engine that defaults for this property should be loaded from an ini file. If you want to have an access to setup of your newly created properties from project settings you also need a simple editor module. In this module you just need to run following code to add a section associated with your configs

	virtual void StartupModule() override
	{
		if (ISettingsModule* SettingsModule = FModuleManager::GetModulePtr<ISettingsModule>("Settings"))
		{
			SettingsModule->RegisterSettings("Project", "Game", "Game setup",
				LOCTEXT("EditorInstanceSettingsName", "Game setup"),
				LOCTEXT("EditorInstanceSettingsDescription", "Game instance specific settings."),
				GetMutableDefault<UMyOwnGameInstance>());
		}
	}

	virtual void ShutdownModule() override
	{
		if (ISettingsModule* SettingsModule = FModuleManager::GetModulePtr<ISettingsModule>("Settings"))
		{
			SettingsModule->UnregisterSettings("Project", "Game", "Game setup");
		}
	}

After adding this and restarting your editor (it’s recommended to compile offline this things rather than using a compile button within editor) you will get a new entry inside project settings window. And this entry will contain all properties of your instance marked as “config”

Then you can just spawn a new object of given class using NewObject function