Console Commands

How to add a new console command

First, let's look at what a simple command requires. Commands are registered using the AddConsoleCommand function. They require a name, a description, a callback function, and command flags.

The callback function is what's invoked every time the command is used. Click here to see its prototype. Example:

public void OnPluginStart()
{
    var flags = ConVarFlag.LinkedConcommand | ConVarFlag.ServerCanExecute | ConVarFlag.ClientCanExecute;
    AddConsoleCommand("sm_myslap", "slap me baby!", flags, Command_MySlap);
}

public ResultType Command_MySlap(int caller, int context, string[] arguments)
{
    return ResultType.Continue;
}

Now we've successfully implemented a command -- though it doesn't do anything yet. In fact, it will say "Unknown command" if you use it! This is because you're not returning ResultType.Handled in your callback. Since you haven't, Server believes you didn't want the Source2 Engine to know the command was registered, and it handles it so. The reason Server expects your function to return ResultType.Handled is because of the Result tag you put in your function's prototype. The Result tag specifies that Command_MySlap must return one of four things. See the Result enumeration in the API to learn more about these return types and when to use them.

public ResultType Command_MySlap(int caller, int context, string[] arguments)
{
    return ResultType.Handled;
}

Now the command will report no error, but it still won't do anything. This is because returning "ResultType.Handled" in a command callback will prevent the engine from processing the command. The engine will never even see that the command was run. This is what you will want to do if you are registering a completely new command through SourceMod.

Adding a Console Command

You can bind commands in the OnPluginStart (or anywhere you like).

c#
c++
python
go
js
lua
using Plugify;
using static s2sdk.s2sdk;

public unsafe class Sample : Plugin
{
    public void OnPluginStart()
    {
        var flags = ConVarFlag.LinkedConcommand | ConVarFlag.ServerCanExecute | ConVarFlag.ClientCanExecute;
        AddConsoleCommand("custom_command", "A command is registered during OnPluginStart", flags, 
        (caller, context, arguments) =>
        {
            if (caller == -1) return;
            PrintToServer("Custom command called.\n");
        }, HookMode.Post);
    }
}

Accessing Command Parameters

arguments array provides access to the calling command parameters. The first parameter will always be the command being called. Quoted values are returned unquoted, e.g.

public void OnCommand(int caller, int context, string[] arguments)
{
    Console.Write($@"
        Command Caller: {caller}
        Command Context: {context}
        Arg Count: {arguments.Length}
        Arg String: {arguments}
        First Argument: {arguments[0]}
        Second Argument: {command[1]}");
}
> custom_command "Test Quoted" 5 13

# Output
Command Caller: 1
Command Context: 0
Arg Count: 4
Arg String: "Test Quoted" 5 13
First Argument: custom_command
Second Argument: Test Quoted