Console Commands - SlimeRancherModding/SRML GitHub Wiki
Sometimes, you will want to add a console command to your mod. This page will show you a method you can use when making a new console command.
Create a brand new class. The typical naming convention for command classes is a one or two word statement that describes the command, followed by Command. In this case, we will be naming ours "ExampleCommand"
For a command, there are three things used. The variables, Execute, and GetAutoComplete.
The sample code can be found here:
using SRML.Console;
using System.Collections.Generic;
namespace MyFirstMod
{
class ExampleCommand : ConsoleCommand
{
// The command's command ID; how someone using the console will call your command.
public override string ID => "example";
// What will appear when the command is used incorrectly or viewed via the help command.
// Remember, <> is a required argument while [] is an optional one.
public override string Usage => "example <argument>";
// A description of the command that will appear when using the help command.
public override string Description => "Does stuff and things.";
// The code that the command runs. Requires you to return a bool.
public override bool Execute(string[] args)
{
// Checks if the code has enough arguments.
if (args == null || args.Length > 1)
{Console.LogError("Incorrect amount of arguments!", true);
return false;
}
Console.Log("Hello World!", true);
return true;
}
// A list that the autocomplete references from. You must return a List<string>.
public override List<string> GetAutoComplete(int argIndex, string argText)
{
// Checks which argument you're on.
List<string> result;
if (argIndex == 0)
{
result = new List<string>();
result.Add("hello");
}
else
{
// If you don't need arguments, replace the if statement with this line of code.
result = base.GetAutoComplete(argIndex, argText);
}
return result;
}
}
}
After writing the class for your new command, you will need a line of code registering your command within your ModEntryPoint.
SRML.Console.Console.RegisterCommand(new ExampleCommand());
Build your mod, and then once you get into the game, check the console to make sure your command has registered correctly. If you have done it right, you will see your command in action!
Congratulations! You have built your first console command!