S&box Wiki

Revision Difference

creating_a_gamemenu#550110

<cat>UI.GameMenu</cat> <title>Creating A Game Menu</title> # 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> ``` From there you can add stylesheets and panels as you would normally define any UI. ⤶ # Resources⤶ ⤶ 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.⤶ ⤶ <upload src="1/8db65d128d9c32a.png" size="107091" name="image.png" />⤶ # 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(); ``` # Reference 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. <upload src="1/8db65cf6caa2185.png" size="30346" name="image.png" />