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.Release | ConVarFlag.ClientCanExecute;
AddConsoleCommand("sm_myslap", "slap me baby!", flags, Command_MySlap);
}
public ResultType Command_MySlap(int caller, CommandCallingContext 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.
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.
import { Plugin } from 'plugify';
import * as s2 from ':s2sdk';
export class Sample extends Plugin {
pluginStart() {
const flags = s2.ConVarFlag.LinkedConcommand | s2.ConVarFlag.Release | s2.ConVarFlag.ClientCanExecute;
s2.AddConsoleCommand("custom_command", "A command is registered during OnPluginStart", flags,
(caller, context, arguments) => {
if (caller !== -1) {
s2.PrintToServer("Custom command called.\n");
}
return s2.ResultType.Handled;
}, s2.HookMode.Post);
}
}
local plugify = require 'plugify'
local Plugin = plugify.Plugin
local s2 = require 's2sdk'
local Sample = {}
setmetatable(Sample, { __index = Plugin })
function Sample:plugin_start()
local flags = bit.bor(s2:ConVarFlag.LinkedConcommand, s2:ConVarFlag.Release, s2:ConVarFlag.ClientCanExecute)
s2:AddConsoleCommand("custom_command", "A command is registered during OnPluginStart", flags,
function(caller, context, arguments)
if caller != -1 then s2:PrintToServer("Custom command called.\n") end
return s2.ResultType.Handled
end, s2:HookMode.Post)
end
local M = {}
M.Sample = Sample
return M
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.