S&box Wiki

Revision Difference

Making_Gamemode#543962

<cat>Code.Game</cat> <title>Creating A Gamemode</title>⤶ ⤶ <warning>As the API is prone to changes, this guide might go out of date often - it will be updated often. It is not recommended to use this unless you currently have access to S&box and know what you are doing.</warning>⤶ ⤶ To start creating your gamemode you'll have to set up a few files and folders in the game's `addons` directory, the easiest way to do this is to download the [minimal gamemode](https://github.com/Facepunch/sbox-minimal) and rename it to your liking.⤶ ⤶ S&box loads all files in your addon folder regardless of their location so it shouldn't matter where you put them but it's considered good practice to keep your code in it's own `code` folder and your content in separate folders by type (models, textures, ect.) ⤶ # Generating your solution⤶ ⤶ Every time you add or remove a project from your addons folder you'll want to run the `GenerateProjects.bat` file that's in there. It'll create/update csproj files for each of your addons and add them to a single solution file so that everything is easily accessible from within Visual Studio.⤶ ⤶ <warning>.NET 5.0 (or 6.0) is required for `GenerateProjects.bat` to work. If running it doesn't seem to be doing anything, run it through command prompt and follow the link that shows up when it errors out.</warning>⤶ ⤶ Once it's done running, open the `Addons.sln` solution in Visual Studio and code away.⤶ ⤶ # Running your gamemode⤶ ⤶ Assuming that everything works as it should you'll be able to launch the game and see your gamemode pop up right there in the menu under installed addons, if it doesn't (or shows the incorrect name) you'll want to check and make sure your .addon file is set up properly. You can use [the official sandbox gamemode](https://github.com/Facepunch/sandbox/blob/master/.addon) as reference.<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.