This page explains how to create custom chat commands that mod users can use to run your code. This can be used for cheats, enabling/disabling features on the fly, testing features, and more.
Before reading this page, I recommend reading the Chat Commands (Users) page so you better understand how the system is used.
First, you will want to initialize your plugin (more info on the Basic Setup page), making sure to give a chat trigger. Then, to create your custom chat command, use plugin.AddCommand() where ‘plugin’ is the variable you got from initializing.
There are a few parameters to this function:
string command | The word a user will enter to run your chat command. For example, the chat trigger for TR Tools is ‘tr’ and one of the commands is ‘list_licenses’. This would be entered as ‘/tr list_licenses'. |
string description | A description shown to the user if they run a help command on this particular command. For more information on how help commands work, see the Chat Commands (Users) page. |
Func<string[], string> method | The method you want called when this command is run. Should be just the name of the function with no parentheses. See next section for more info. |
params string[] argumentNames | The names of each argument that can be used for your command. These names are used only when a user runs a help command on this command in order to indicate what arguments are expected. If your command has no arguments, you can leave this blank. |
Each command needs a method/function that will be called any time your command is entered. This method MUST have a return type of string
and use only one argument of string[] args
.
Whatever your function returns will be automatically sent as a chat message. If there is no need to give the user any feedback, you can just return null or an empty string. However, if you need to tell the player what happened or give negative/positive feedback, you would generally return your feedback. If desired, you can manully send messages or errors as well. If you are doing this manually or there is no need to give the user any feedback, you can just return null or an empty string.
The argument of the function is an array of strings. This allows a user to input any number of arguments past the command and all will be sent to your method. If your method needs no arguments, you must still keep the parameter in your function but it can be ignored within the function. If you do use any arguments, it is always recommended to start your function by checking for the number of arguments and if it is invalid, send an error letting the user know what they're missing.
An example chat command method is seen below:
public string MyCommandMethod(string[] args) {
// Checks if there are enough arguments
if (args.Length == 0)
return "Command is missing an argument."
/* Here is where you'd put any functionality related to your command. */
// Enough commands were entered, so return a successful message.
return "Command successful!";
}
There are a few other useful functions for chat in TR Tools. These are described below:
TRChat.SendMessage(string message, string name) | Creates a new chat bubble with your message. The name is in between brackets just as with base game chat messages. The name paramters can also be left blank to have no name shown at all. |
TRChat.SendError(string message, string name) | Creates a chat bubble as above, but the message text is red, and if no name is specified it will by default be “<ERROR>”. |
TRChat.SendMessage(Color color, string message, string name) | Creates a chat bubble with your message, but allows you to choose the color of the message text. |