S&box Wiki

Revision Difference

Making_Gamemode#543851

<cat>Code.Game</cat> ⤶ <title>Creating A Gamemode</title> <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 create an addon and have it show, you will have to create a folder within the addons directory. Name it however you please. ⤶ All files will be loaded as is with no need to place anything in specific folders, but the recommended structure is to have:⤶ ⤶ /code/: Where you put your code⤶ ⤶ /content/: Where you put your content⤶ ⤶ ## Addon File⤶ The .addon file contains crucial information about your game type and what it depends on. ⤶ To get started with a base gamemode, your .addon file should look like this:⤶ ⤶ ```js⤶ {⤶ "name": "",⤶ "version": "",⤶ "sharedassets": "*.*",⤶ ⤶ "depends": ⤶ [⤶ "base",⤶ "citizen" // Optional if you already have a model and animations, etc.⤶ ],⤶ ⤶ //⤶ // Addons can contain multiple addons, describe them here⤶ //⤶ "gamemodes":⤶ [⤶ {⤶ // These will show in the console when you load into the game.⤶ // They will not show on the gamemode list! To do that make an organization⤶ // and fill the proper information on gamemode creation.⤶ "name": "",⤶ "title": "",⤶ "description": ""⤶ }⤶ ]⤶ }⤶ ⤶ ⤶ ```⤶ ⤶ ## Generating Project⤶ After you have created your .addon file, you will now be able to use the GenerateProjects batch file located in the addons folder, which will create a csproject and allow you to easily see the code/content in base, rust, citizen, and other addons you have in the addons directory.⤶ ⤶ **Important! You must have .NET 5.0 (or 6.0) to run the GenerateProjects, if you do not have this run the batch file through command prompt and then go to the link given when it errors out!**⤶ ⤶ After you have generated your project, open the Addons solution where you can access your freshly generated csproj file for your addon.⤶ ⤶ ## Boilerplate⤶ To start, right click on your addon project in Visual Studio and click Add > New Folder, then name the new folder "code".⤶ ⤶ Under this folder, now create a new C# file called "Game.cs", this will be where your game starts up.⤶ Once you have created the file, you can now post the following code into it.⤶ ⤶ ```cs⤶ using Sandbox;⤶ using Addon.UI;⤶ ⤶ namespace Addon⤶ {⤶ [Library("addon", Title = "Addon")]⤶ partial class AddonGame : Game⤶ {⤶ public AddonGame()⤶ {⤶ Log.Info("Game Started");⤶ if (IsServer)⤶ new AddonHUD();⤶ }⤶ ⤶ public override Player CreatePlayer() => new AddonPlayer();⤶ }⤶ }⤶ ```⤶ Notice this is using classes we have not created yet. This will addressed later on. For the time being, replace the information accordingly with the name of your addon or gamemode.⤶ ⤶ Now you will need to create a class for your Player. Create another C# file and name it "Player.cs".⤶ The following is needed to spawn into the map you are loading into and walk around.⤶ ⤶ ```cs⤶ using Sandbox;⤶ using System;⤶ ⤶ namespace Addon⤶ {⤶ partial class AddonPlayer : BasePlayer⤶ {⤶ public AddonPlayer()⤶ {⤶ Log.Info("Addon Player");⤶ }⤶ ⤶ public override void Respawn()⤶ {⤶ SetModel("models/citizen/citizen.vmdl"); // If you have your own model, you can place it here instead.⤶ Controller = new WalkController();⤶ Animator = new StandardPlayerAnimator();⤶ Camera = new FirstPersonCamera();⤶ EnableAllCollisions = true;⤶ EnableDrawing = true;⤶ EnableHideInFirstPerson = true;⤶ EnableShadowInFirstPerson = true;⤶ base.Respawn();⤶ }⤶ }⤶ }⤶ ```⤶ ⤶ After you have created this and renamed it accordingly, create another folder inside the code folder called "ui". Inside this folder, create another C# file and name it "Hud.cs". Along with this, create another file called "Hud.scss", for your css. That will contain your own CSS formatting.⤶ ⤶ Place the following code inside the "Hud.cs" file:⤶ ⤶ ```cs⤶ using Sandbox;⤶ using Sandbox.UI;⤶ ⤶ namespace Addon.UI⤶ {⤶ [Library]⤶ public partial class AddonHUD : Hud⤶ {⤶ public AddonHUD()⤶ {⤶ if (!IsClient) return;⤶ ⤶ RootPanel.StyleSheet.Load("/ui/Hud.scss");⤶ RootPanel.AddChild<ChatBox>();⤶ }⤶ }⤶ }⤶ ```⤶ ⤶ Now when you hop into S&box, you should be able to see your gamemode, load into a map, and look around. 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 an example.