S&box Wiki

Creating A Game Menu

What is a game menu?

Not all games are created equal, so it's hard for us to create a menu screen that is suitable for all types of games. But that's cool because we let you create your own 100% custom menu.

Your game menu is responsible for setting the mood of your game and presenting options to help your players get started.

The simplest menu would just contain a "start game" option. From there you can scale into creating and finding servers, lobbies, statistics, character customization, settings, update news etc.

Defining Your Menu

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

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

public class MyMainMenu : RootPanel, Sandbox.Menu.IGameMenuPanel { }

or to define one in Razor you'd do

@using Sandbox; @using Sandbox.UI; @inherits RootPanel @implements Sandbox.Menu.IGameMenuPanel <root> <div>Deathmatch 98</div> </root> <style> label { color: white; } </style>

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


You probably don't want to make people download your whole game to view the menu, so we let you create a wildcard list of files that you want people to download before opening it. Here you'd define things like stylesheets, images and sounds. If you're doing something more exotic you can define specific maps, models and materials too.


Any resources needed for custom a Loading Screen will also be added here, and both downloaded before your Game Menu is loaded

Doing Stuff

Most interactions you're going to do with the game menu are available via the Game.Menu global.

// Print the current game package name Log.Info( Game.Menu.Package.DisplayName ); // Create a new lobby await Game.Menu.CreateLobbyAsync(); // Name the lobby Game.Menu.Lobby.Title = "My Awesome Lobby"; // Leave the lobby Game.Menu.Lobby.Leave(); // Connect to a server Game.Menu.ConnectToServer( server ); // Start a server Game.Menu.StartServerAsync( 8, "My Server", "facepunch.square" ); // Hide the menu, show the game Game.Menu.HideMenu(); // Close the game, return to s&box Game.Menu.Close();

To start a game on an empty map, use the <empty> map ident.

Game.Menu.StartServerAsync( 8, "My Server", "<empty>" );


The default "scaffolding" main menu is available in the base addon. If you're wondering how to do something, it might be a good idea to look in there for how we've already done it.