Skip to content

Instantly share code, notes, and snippets.

@looterz
Last active Mar 27, 2021
Embed
What would you like to do?
UE4 Loading Screen with Progress

This will only work in cooked builds with Event Driven Loader enabled (enabled by default). Currently this only shows the specified packages loading progress, but not dependant packages, which is an annoying issue since that is where most of the loading time is for maps.

#include "TestGameInstance.h"
#include "MoviePlayer/Public/MoviePlayer.h"
#include "Widgets/SCompoundWidget.h"
#include "Widgets/Text/STextBlock.h"
#include "Widgets/SBoxPanel.h"
class STestLoadingScreen : public SCompoundWidget
{
public:
SLATE_BEGIN_ARGS(STestLoadingScreen)
: _InMap()
{}
SLATE_ARGUMENT(FName, InMap)
SLATE_END_ARGS()
void Construct(const FArguments& InArgs)
{
InMap = InArgs._InMap;
ChildSlot
[
SNew(SVerticalBox)
+ SVerticalBox::Slot()
.VAlign(VAlign_Center)
.HAlign(HAlign_Center)
[
SNew(STextBlock)
.Font(FCoreStyle::GetDefaultFontStyle("Regular", 64))
.Text(this, &STestLoadingScreen::GetMessage)
.Visibility(this, &STestLoadingScreen::GetMessageIndicatorVisibility)
]
];
}
private:
FName InMap;
FText GetMessage() const
{
float LoadPercentage = GetAsyncLoadPercentage(InMap);
if (LoadPercentage < 0)
{
LoadPercentage = 100.f;
}
return FText::FromString(FString::Printf(TEXT("%d%%"), FMath::TruncToInt(LoadPercentage)));
}
EVisibility GetMessageIndicatorVisibility() const
{
return EVisibility::Visible;
}
};
void UTestGameInstance::Init()
{
Super::Init();
FCoreUObjectDelegates::PreLoadMap.AddUObject(this, &UTestGameInstance::BeginLoadingScreen);
}
void UTestGameInstance::BeginLoadingScreen(const FString& MapName)
{
if (IsRunningDedicatedServer())
{
return;
}
FLoadingScreenAttributes LoadingScreen;
LoadingScreen.bAutoCompleteWhenLoadingCompletes = false;
LoadingScreen.bWaitForManualStop = false;
LoadingScreen.bMoviesAreSkippable = false;
LoadingScreen.WidgetLoadingScreen = SNew(STestLoadingScreen).InMap(FName(*MapName));
LoadingScreen.PlaybackType = EMoviePlaybackType::MT_LoadingLoop;
GetMoviePlayer()->SetupLoadingScreen(LoadingScreen);
GetMoviePlayer()->PlayMovie();
}
#pragma once
#include "Test.h"
#include "CoreMinimal.h"
#include "Engine/GameInstance.h"
#include "TestGameInstance.generated.h"
UCLASS(config=Game)
class TEST_API UTestGameInstance : public UGameInstance
{
GENERATED_BODY()
public:
virtual void Init() override;
void BeginLoadingScreen(const FString& MapName);
};
@Kolbasa08

This comment has been minimized.

Copy link

@Kolbasa08 Kolbasa08 commented Nov 27, 2020

How can I make this work in my project?

@looterz

This comment has been minimized.

Copy link
Owner Author

@looterz looterz commented Nov 27, 2020

How can I make this work in my project?

Implement your own GameInstance class in the project and set it up like the one in this gist. I ended up moving away from trying to display load progress because of issues related to tracking dependent package loading progress with this method, so it's not accurate enough to be really useful. I haven't tested this in a couple of years though, could be fixed, as long as they updated GetAsyncLoadProgress to track dependent package loading progress too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment