How do you convert a UUserWidget to a SWidget? Needed for a loadingscreen.

Hey, i have been working on getting a loading screen for my game and have gotten the basic loading screen working by using this.

void UHomeInvasionGameInstance::Init()
{
	UGameInstance::Init();

	FCoreUObjectDelegates::PreLoadMap.AddUObject(this, &UHomeInvasionGameInstance::BeginLoadingScreen);
	FCoreUObjectDelegates::PostLoadMap.AddUObject(this, &UHomeInvasionGameInstance::EndLoadingScreen);
}

void UHomeInvasionGameInstance::BeginLoadingScreen()
{
	FLoadingScreenAttributes LoadingScreen;
	LoadingScreen.bAutoCompleteWhenLoadingCompletes = false;
	LoadingScreen.MinimumLoadingScreenDisplayTime = MinLoadTime;
	LoadingScreen.WidgetLoadingScreen = FLoadingScreenAttributes::NewTestLoadingScreenWidget();

	GetMoviePlayer()->SetupLoadingScreen(LoadingScreen);
}

void UHomeInvasionGameInstance::EndLoadingScreen()
{

}

But i have gotten a bit confused on how you would go about setting your own custom widget to display when loading the maps. I understand that it is this variable that should be set to the wanted widget, but i don’t really understand how i would convert a UserWidget that i have created to a SWidget.

	/** The widget to be displayed on top of the movie or simply standalone if there is no movie. */
	TSharedPtr<class SWidget> WidgetLoadingScreen;

Some help would be appreciated.

1 Like

So let me say this about UMG and Slate, UMG is UObject-compatible frontend of Slate, created to make Slate usable in blueprints (as all Slate classes are non-UObject and invisible to reflection system there for can’t be used in blueprints) and all UMG widgets (with prefix U) are containers for Slate widgets (with S prefix). So slate practiclly have same widgets… even more widgets them UMG has

To get underling SWidget of UMG widget you use this function:

Also alternatively you can easily construct Slate widgets, it really super easy which makes Slate very easy to use in C++, here examples:

But i understand you want to use UMG editor to declere Slate widgets insted ;]

3 Likes

That is perfect, works just like i want it. Thanks!

I don’t mean to hijack the post, but I figured that I would be able to get get some pointers on getting my loading screen setup as you two seem to have it figured out.

Below are screens of my header and cpp file, but there seem to be some errors that I’m running into.

http://puu.sh/mskXm/3c72dbe928.png

http://puu.sh/mskZx/1e9cac8623.png

What errors are you getting?
Also, have you included the “MoviePlayer” module in build.CS file?

Yes I added MoviePlayer into my LostEchoes.Build.cs file.
My errors are pretty funky like “missing ;” but my whole game compiles and runs fine. Just that the red underlines never go away, and I don’t seem to have a load screen running when I start my game.

Oh, looks like errors have left after a full clean and rebuild. Sorry and thanks!

I’m curious what you did to get it working with your UMG widgets. I attempted to essentially get a UUserWidget to the SWidget needed and when debugging it has a valid object. However I then receive a Rendering Thread exception. It works with the base load screen from FLoadingScreenAttributes::NewTestLoadingScreenWidget(). The relevant part of my Game instance is below. the wLoading essentially is where the LoadingWidget is set and then it constantly gets casted or built into an SWidget to be placed inside the movie player loading screen.

  UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Widgets")
    TSubclassOf<class UUserWidget> wLoading;

  UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Widgets")
  UUserWidget* LoadingWidget;

private:
  TSharedPtr < class SWidget > loadswidget;


void UCustomGameInstance::Init()
{
  UGameInstance::Init();

  FCoreUObjectDelegates::PreLoadMap.AddUObject(this, &UCustomGameInstance::BeginLoadingScreen);

  FCoreUObjectDelegates::PostLoadMap.AddUObject(this, &UCustomGameInstance::EndLoadingScreen);

  LoadingWidget = CreateWidget(this, wLoading);
  loadswidget = LoadingWidget->TakeWidget();
}

void UCustomGameInstance::BeginLoadingScreen(const FString& MapName)
{
    FLoadingScreenAttributes LoadingScreen;

    LoadingScreen.bAutoCompleteWhenLoadingCompletes = true;
    LoadingScreen.WidgetLoadingScreen = loadswidget;

    GetMoviePlayer()->SetupLoadingScreen(LoadingScreen);


}
1 Like

Hello, thanks in advance for your code.
I referred to it, and I also met a rendering exception, not sure whether the same as you met, when I load map for the second time.

I also found LoadingWidget became unusable for the second time as I play in viewport, and that widget is destroyed on loading map.

Then I put LoadingWidget = CreateWidget(this, wLoading); in BeginLoadingScreen, and no crash occurs either in PlayInViewport or in Standalone. It works.

PS. My MoviePlayer only works in Standalone (screen stuck in PlayInViewport until map is loaded). Are yours the same?

1 Like