The CommonLoadingScreen Plugin in UE5

Words: 713

This article is based on UE5.6 version of Lyra’s CommonLoadingScreen.

Basic Concepts

CommonLoadingScreen is a plugin for quickly creating a Loading screen, released in the Lyra project.

After the plugin is added, it comes with a ULoadingScreenManager UGameInstanceSubsystem, which is initialized along with GameInstance.

ShouldCreateSubsystem checks whether it is running on a DedicatedServer; if so, it is not created.

In ULoadingScreenManager::Tick, it performs the UpdateLoadingScreen check to update the visibility state of Loading.

Loading Control

The core is determining whether it is in the Loading state. It checks various situations that require Loading; if any one of them is met, it considers that Loading needs to be displayed (before that, it also checks some situations where Loading is not needed).

Situations where Loading is determined not to be needed:

  • If the command line sets the Loading display off via NoLoadingScreen, then Loading is considered unnecessary.
  • If GameVieportClient is nullptr, then Loading is not needed.

Situations where Loading is determined to be needed:

  • If ForceLoadingScreenVisible is set, it is considered needed
  • If GameInstance fails to get WorldContext
  • If WorldContext fails to get World
  • If GameState cannot be obtained
  • If the map is in a loading state
  • If the scene is in a pending travel state
  • If the scene is in a pending network connection state
  • If World has not yet BeginPlay
  • If World is in seamless travel
  • If GameState overrides that Loading should occur
  • If the GameState上 Component overrides that Loading should occur
  • If the configured ExternalLoadingProcessors override that Loading should occur
  • If PlayerController overrides that Loading should occur
  • If the PlayerController Component overrides that Loading should occur
  • If it is in split-screen game and there is an invalid PlayerController
  • If there is no PlaayerController

In terms of approach, user customization is mainly provided through the ILoadingProcessInterface; implementations can determine whether Loading should occur, and if any one returns that Loading is needed, the final state will be in the Loading state.

Design Analysis

Determining the Map Loading State

In ULoadingScreenManager::Initialize, the pre-map-load event PreLoadMapWithContext and the map-load-completed event PostLoadMapWithWorld are registered, so that some processing can be performed at the timing of map travel.

Note that the object of FCoreUObjectDelegates is process-level. If running in a multiplayer manner, callbacks not belonging to this GameInstance may be triggered at this time, so it is necessary to check whether the World’s GameInstance is this GameInstance.

At PreLoadMapWithContext, mark that it is currently in the LoadMap state.

cpp
1
bCurrentlyInLoadMap = true;

At PostLoadMapWithWorld, mark that it is no longer in the LoadMap state.

Outputting Why It Is in the Loading State

During Tick, the check is performed. Each time a LogLoadingScreenHeartbeatInterval is reached (or if LogLoadingScreenReasonEveryFrame is set), the reason for the Loading state is output (if LogLoadingScreenReasonEveryFrame is set, the reasons for not needing it are also output).

To this end, each time whether Loading should be displayed is determined, the reason needs to be recorded in DebugReasonForShowingOrHidingLoadingScreen.

Adjusting Rendering Settings

To save performance, some rendering settings are adjusted during Loading.

  • ShaderPipelineCache::SetBatchMode is set to Fast, and restored to Background when disabled
  • Set bDisableWorldRendering to disable world rendering
  • Set HighPriorityLoading high-priority loading mode
  • Adjust the thread heartbeat latency coefficient
  • Pause main-thread heartbeat detection

Intercepting Input

When Loading is created, an InputPreProcessor is registered to implement input interception.

cpp
1
2
InputPreProcessor = MakeShareable<FLoadingScreenInputPreProcessor>(new FLoadingScreenInputPreProcessor());  
FSlateApplication::Get().RegisterInputPreProcessor(InputPreProcessor, 0);

Adjusting Thread Heartbeat

In the Loading control flow, thread heartbeat is controlled by calling interfaces on FThreadHeartBeat.

For details, see: ThreadHeartBeat Mechanism in UnrealEngine

UI Refresh

By calling the Tick function of SlateApplication once, the Loading screen is ensured to refresh immediately.

cpp
1
2
// Tick Slate to make sure the loading screen is displayed immediately  
FSlateApplication::Get().Tick();

When the UI is hidden, a GC function is called immediately to ensure that the Loading screen is destroyed (this feels not very meaningful, though).

cpp
1
GEngine->ForceGarbageCollection(true);

Conclusion

In terms of design approach, CommonLoadingScreen does not call Loading when Loading is needed; instead, it creates Loading when it determines that the Loading state is needed. This design helps handle, when multiple Loading logics need to be invoked at the same time, how to control the final Loading state. At the same time, with good Debug instrumentation, it is convenient to know the specific reason for Loading at that time.

This post is licensed under CC BY-NC-SA 4.0. If it helped, or if you have anything to say, the comment box below is open.
Page views —
Unique visitors —

Loading...