Skip to content

Create a Mod

Panadero1 edited this page Mar 18, 2021 · 4 revisions

Full Modding vs Game Modes

Before we start this tutorial, I want to specify that this tutorial is specifically for adding a custom gamemode into the main title screen. It will be listed as an option when the game is first launched.
For this tutorial, every time you see the word 'mod', know that I am referring to 'game mode'
If you want to completely change how the game runs, you'll have to make a branch or a fork

Intro

So you want to make a mod, eh? Well you've come to the right place.

This will be a tutorial for everything you need to edit in the code to have a working mod.

Before Modding

Before you start modding, you should have:

  • An IDE (Interactive Development Environment). I recommend Visual Studio Code
  • At least some experience with C# (the more you have, the better, but I tried to make it as easy as possible without much coding)
  • Git experience and some software to be able to commit changes to the remote repo (I recommend GitHub Desktop)

The Steps for Creating a Custom Mode

Step 1. Define a New Game Mode

We'll start in Data\GameModeCommands.cs

At the bottom of the file, you'll see a few lines that say:

public static CommandChoices <name> = new CommandChoices (new List<Command>()

...followed by many lines of statements.

This is the list of commands that the user has access to in-game per each gamemode.

You will be creating a new one for your gamemode here. Add a statement after the last one has ended. Like this

public static CommandChoices <YourGameModeName> = new CommandChoices(new List<Command>()
{

});

Replace <YourGameModeName> with a name that has pascal case format: ThisIsAnExampleOfPascalCase

Great. You've completed the first step. The next is to start adding commands into the game mode

Step 2. Adding pre-existing commands

There are quite a few already existing commands in the Class GameModeCommands (as you probably could tell)

The first part of the document is split into two sections: Functions and Commands

Each Command has a reference to a function as well as a few useful parameters that make evaluation, parameters, and help commands much easier

Your CommandChoices is defined by initializing a list of commands. You can add whichever ones you like, and the full list can be found here: Documentation

Adding is as simple as writing the name of the command. For example, you should probably add the 'exit' command.

It would look like this:

public static CommandChoices <YourGameModeName> = new CommandChoices(new List<Command>()
{
    _exit
});

When adding multiple commands, remember to have a comma at the end of every line to separate them:

public static CommandChoices <YourGameModeName> = new CommandChoices(new List<Command>()
{
    _exit,
    _load,
    _save
});

Step 3. Defining new commands

Creating a new command is not too hard, however this is where the C# experience comes into play.

Let's say you wanted to add a command called _jump and wanted it to jump over one tile and land on the one past that (like checkers)

The first thing you would need is a function to represent _jump

In the #region Functions, define a new function. For our _jump example, it will look like this.

private static void Jump(string[] parameters)
{

}

We'll do the coding a bit later, but it's nice to have the function defined first

Next, go down to the #region Commands and add a new command. Our example looks like this.

private static Command _jump = new Command(
    "jump",
    "Jumps the player in a cardinal direction over one tile and onto the next",
    new string[]
    {
        "Indicate the cardinal direction where you would like to jump"
    },
    true);

Going through the parameters one at a time, what we just entered was as follows:

  1. Identifier
  2. Description
  3. Array of parameters that get asked
  4. Boolean for whether or not this takes time to perform

If you want more information on these parameters, please see the Documentation page

To add this new command to your game mode, put it in your CommandChoices List at the bottom

public static CommandChoices JumpyGame = new CommandChoices(new List<Command>()
{
    _exit,
    _load,
    _save,
    _jump
});

Now let's code that jump. The example below is how I would write it. For help on how to code with this engine, visit the Documentation page

private static void Jump(string[] parameters)
{
    if (CommandInterpretation.InterpretDirection(parameters[0], out Coord direction))
    {
        World.LoadedLevel.Grid.MoveContents(World.Player.Contents, new Coord(direction.X * 2, direction.Y * 2));
    }
}

Step 4. Adding your Main() function and loop

Good news! You've added all you need to in the Data\GameModeCommands.cs file.

We are now moving on to the Game\Game.cs file, where we are adding a few quick things

First things first, take a look at the line that looks like this:

public static Dictionary<string, (Action action, CommandChoices commands)> GameModes = new Dictionary<string, (Action, CommandChoices)>()
{
    ...
}

Don't worry about what this means if you don't understand it; all we need to do is add an entry to this Dictionary to get our game mode onto the screen

This dictionary is simply telling the game what to display at the start and what to run when the user selects a specific option.

To add your gamemode to this, you need to make a new line in the brackets (please put this above the "exit" one)

You'll format the new line like this:

{ Name, (MainFunction, GameModeCommands.YourCommandChoices) },
  1. Name is where you'll enter the name that will be displayed
  2. Unless you want to have some specific way that the game runs, you can just reference GameEngine for your Main function.
    1. If you want to make changes to how the game runs, don't edit GameEngine; instead, make your own function and put it instead of GameEngine
  3. GameModeCommands.YourCommandChoices is whatever you named the list of commands you made earlier

If you want to make a custom function as your Main function to run instead of GameEngine, just copy from GameEngine and tweak what you want to tweak. You can ask for help in our discord (linked in footer)

Test it!

Build and run your program and you should see it listed as one of the options on the title screen

Feel super proud of your work? Make a pull request and if I like it enough I'll include it with the main game!! :)