S&box Wiki

Loading Screens

What is a loading screen?

Loading screens are shown when entering a game server. During loading the client connects to the server, downloads the game, map and any addons, and then actually joins the game.

sbox_0185.jpg

The loading screen usually shows progress indicating which of these things it's doing. It's also nice to have a cancel button.

Defining Your Loading Screen

When your game is opened in the menu, we look in your game for a Panel or RootPanel that implements the Sandbox.Menu.ILoadingScreenPanel interface. If we find it, then we create it and that's your loading screen.

So to create a loading screen panel in plain c#, you'd do something like this.

public class MyMainMenu : RootPanel, Sandbox.Menu.ILoadingScreenPanel { public void OnLoadingProgress( Sandbox.Menu.LoadingProgress progress ) { } }

or to define one in Razor you'd do

@using Sandbox; @using Sandbox.UI; @using Sandbox.Menu; @inherits RootPanel @implements Sandbox.Menu.ILoadingScreenPanel <root> <div>Loading!</div> <div>@Progress.Title</div> </root> @code { public LoadingProgress Progress; public void OnLoadingProgress( LoadingProgress progress ) { Progress = progress; StateHasChanged(); } }

From there you can add stylesheets and panels as you would normally define any UI.

Resources

Much like the Game Menu, you can add specific resources to download before the rest that are required for your loading screen.

You just add them into your project settings Menu files.

image.png

Your Game Menu and Loading Screen resources will both be in this list and both be downloaded before loading up your menu!

LoadingProgress

The OnLoadingProgress method receives a LoadingProgress struct from the engine. This holds information on the current load progress.

Cancelling

To cancel, just call Game.Menu.CancelLoading(). You can easily wire a button up to do this in Razor like this.

<div class="controls"> <div class="button" onclick="@Game.Menu.CancelLoading"> Cancel </div> </div>