This article is based on
UE5.6version ofLyra’sCommonLoadingScreen.
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
Loadingdisplay off viaNoLoadingScreen, thenLoadingis considered unnecessary. - If
GameVieportClientisnullptr, thenLoadingis not needed.
Situations where Loading is determined to be needed:
- If
ForceLoadingScreenVisibleis set, it is considered needed - If
GameInstancefails to getWorldContext - If
WorldContextfails to getWorld - If
GameStatecannot 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
Worldhas not yetBeginPlay - If
Worldis in seamless travel - If
GameStateoverrides thatLoadingshould occur - If the
GameState上Componentoverrides thatLoadingshould occur - If the configured
ExternalLoadingProcessorsoverride thatLoadingshould occur - If
PlayerControlleroverrides thatLoadingshould occur - If the
PlayerControllerComponentoverrides thatLoadingshould 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
FCoreUObjectDelegatesis process-level. If running in a multiplayer manner, callbacks not belonging to thisGameInstancemay be triggered at this time, so it is necessary to check whether theWorld’sGameInstanceis thisGameInstance.
At PreLoadMapWithContext, mark that it is currently in the LoadMap state.
| |
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.
| |
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.
| |
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).
| |
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.