S&box Wiki

Revision Difference

Making_Gamemode#544146

<cat>Code.Game</cat> <title>Creating A Game</title> The easiest way to start creating a game is to download and rename the [minimal gamemode](https://github.com/Facepunch/sbox-minimal). You should also have an adequate coding workspace setup by following one of the following guides: * <page>Setting up Visual Studio</page> (Recommended) * <page>Setting up VSCode</page> # Addon directory structure Every addon is self contained in its own folder in the addons directory. For example an addon called "myaddon" would be in the following path: ``` C:/Steam/steamapps/common/sbox/addons/myaddon/ ``` This folder should contain an .addon file (which is just json). This tells the game that the folder contains an addon. ⤶ After you have your `.addon` file you can <page text="generate your solution file">Setting_up_Visual_Studio#generatingyoursolution</page> and dive into coding.⤶ ## Code All your code should be contained inside a code folder within your addon folder. ``` addons/myaddon/code/ ``` Any C# code within this folder is compiled and used for your game. ## Content All your content can go in folders within your addon folder. * `addons/myaddon/config` - for <page text="fgd">Linking Entities to Hammer</page> files or for defining custom asset types * `addons/myaddon/fonts` - fonts here are automatically loaded and available for usage in UI * `addons/myaddon/maps` - maps are added to the menu from here * `addons/myaddon/materials` * `addons/myaddon/models` * `addons/myaddon/particles` * `addons/myaddon/shaders` - shaders placed in here are compiled * `addons/myaddon/sounds` # Initial Game code Every game you make needs a <page text="Game">Sandbox.Game</page> class, this is the entry point for all your game logic. Initially you need to make sure your game class is derived from <page>Sandbox.Game</page>. This needs to be in a .cs file in your addon code folder, we recommend calling it `Game.cs`.⤶ ```csharp public partial class MyGame : Sandbox.Game { } ``` With this your game should now show up in the main menu and let you load into a map. # Your first pawn When a client joins a server, by default nothing happens. You need to create a Pawn for that client to control. A Pawn is just an Entity that is controlled by a Client. To create a first person player you can derive a new entity from the <page>Sandbox.Player</page> and creating it from Game.ClientJoined like so: ```csharp // MyPlayer.cs using Sandbox; partial class MyPlayer : Player { public override void Respawn() { SetModel( "models/citizen/citizen.vmdl" ); // Use WalkController for movement (you can make your own PlayerController for 100% control) Controller = new WalkController(); // Use StandardPlayerAnimator (you can make your own PlayerAnimator for 100% control) Animator = new StandardPlayerAnimator(); // Use ThirdPersonCamera (you can make your own Camera for 100% control) Camera = new ThirdPersonCamera(); EnableAllCollisions = true; EnableDrawing = true; EnableHideInFirstPerson = true; EnableShadowInFirstPerson = true; base.Respawn(); } } // Game.cs public partial class MyGame : Sandbox.Game { public override void ClientJoined( Client client ) { base.ClientJoined( client ); // Create a pawn and assign it to the client. var player = new MyPlayer(); client.Pawn = player; player.Respawn(); } } ``` # Footnotes * The <page text="Game">Sandbox.Game</page> class is an <page text="Entity">Sandbox.Entity</page> like everything else, and as such can make use of <page>networked types</page> fully to keep clients in sync on the current game state. * A good way to know what you can do with the Game is to use intellisense to see what you can override.