S&box Wiki

Revision Difference


<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 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 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!** **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.