Skip to content

Other ConsoleCommands

Mart Kartasev edited this page Sep 2, 2021 · 1 revision

Adding console commands:

Talewords has provided an attribute to mark methods with fitting signatures as usable from console:

[TaleWorlds.Library.CommandLineFunctionality.CommandLineFunction(string name, string groupname)]

Lets look at an example:

public class MyConsoleCommands
{
        //Note that when running the command, the pattern is <category>.<command_name>
        [CommandLineFunctionality.CommandLineArgumentFunction("command_name", "category")]
        public static string SomeConsoleMethod(List<string> consoleArguments)
        {
            DoSomethingWith(consoleArguments);

            var consoleOutput = "Hello world!";
            return consoleOutput;
        }
}

The command can be used from the game console. Open it with "Alt + `" and type ".<command_name>". Arguments are separated by "space".

Important:

  1. The method marked with attribute has to be static otherwise it wont be visible in the console.
  2. The method signature must declare List as its argument.
  3. The method _must_return a string for console output to inform the invoker that the command has been executed.
  4. If your command is not supposed to work outside of the campaign, add this to the beginning of method to avoid crashes:
if (!CampaignCheats.CheckCheatUsage(ref CampaignCheats.ErrorType))
                return CampaignCheats.ErrorType;

Recomendations

  • It is best not to clutter the classes of your feature with method signatures of console commands. A good practice is to add separate wrapper classes and static methods with the console command attributes.

That's all. Don't forget you are wonderful ;D