Skip to content
Guido "Harb edited this page Nov 15, 2019 · 1 revision

NOTE that this submodule is currently located in R2API.Utils!

This submodule allows mods to have their concommands and convars added to the game console. It does this by scanning your assembly for all RoR2.Convar types, and all methods with the RoR2.ConCommand attribute.

To make your mod be scanned for them, simply call R2API.Utils.CommandHelper.AddToConsoleWhenReady. That's it. You can call this before the console is ready, or after the console is ready, the submodule will take care of it nonetheless.

It's important that convars and concommands are static.

ConCommand functions will get passed a ConCommandArgs object, which contains information about the sender too. The class itself has helper functions to automatically check/cast arguments to type, and ensuring argument count.

Example

[R2APISubmoduleDependency("CommandHelper")]
class CommandHelperExample: BaseUnityPlugin
{
    private void Awake(){
        R2API.Utils.CommandHelper.AddToConsoleWhenReady();
    }

    //It's imporant the convars and commands are static. After all, it doesn't make sense for an instance to have a convar, how would the game know which instance you wanted to change the convar of?
    private static RoR2.Convars.IntConvar MyIntConvar = new RoR2.Convars.IntConvar(
        "commandhelper_int",//the way the convar will be called in console, typically all lowercase.
        RoR2.ConVarFlags.None,//the convarFlags. Most important one is ConvarFlags.ExecuteOnServer, which will make the convar be changed on the Host of the session only.
        "0",//A string with your initial value. Yes, even if it's an int.
        "Changing this number changes a number!");//The helptext when using `help convar`. This text is also searched through with `find keyword`.

    [RoR2.ConCommand(commandName = "commandhelper_com", flags = RoR2.ConVarFlags.None, helpText = "This is is a command and can take arguments.")]
     static void ccExampleCom(RoR2.ConCommandArgs args){
         if(args.Count == 0){
            R2API.Utils.ChatMessage.Send("Wow, no args!");
            return;
         }
         args.CheckArgumentCount(2); //This cancels the command if there's not at least 2 arguments.

         args.GetArgInt(0);
        MyIntConvar.Value.SetString(args[0]);//this is how you would set the convar manually, while also showing off you can acces the args as an array.

        myEnumType castToEnum = args.getArgEnum<MyEnumType>(1);//This will automatically convert things like 0,1,2 and "Neutral", "Monster", "Player" to the appropriate Enum.
        UnityEngine.Debug.Log(castToEnum);//Since the user just wrote in the console, you might as well write back to them.
     }
}