S&box Wiki

Revision Difference


<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) following the provided readme, you will need to edit the `.addon` as well as the `Game.cs` in order to rename fully. 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 it's own folder in the addons directory, for example an addon called "myaddon" would be in the following path: `C:/Program Files (x86)/Steam/steamapps/common/sbox/addons/myaddon/` The most important file you need to begin with is the `.addon` file, this is a simple JSON configuration file defining what your addon is called, it's dependencies and what games it contains. Below is a simple example to get you started, everything should be self explanatory but if you need more you can read up on the <page>.addon schema</page>. ⤶ ```json⤶ ⤶ ```⤶ { "name": "myaddon", "sharedassets": "*.*", "depends": [ "base" ], "gamemodes": [ { "name": "mygame", "title": "My Game", "description": "You will have fun." } ] } ``` After you have your `.addon` file setup properly 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, for the most part content isn't limited to specific folders, however you should try to roughly follow: * `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> and is marked with the Library attribute with the same name as defined in your `.addon` file, this ensures the game can be found. **Your game will not work if the Library name is not matching.** ```csharp [Library( "mygame" )] 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 On it's own a Game won't create a player controllable entity (a pawn), this is in order to give you full control over what sort of game you want to make, a pawn can be anything from a complex animated character to a dull prop, pawns can be assigned and unassigned at will. Starting with a simple character is as simple as defining your pawn 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 [Library( "mygame" )] 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.