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.
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);
}
}
#include <plugify/cpp_plugin.hpp>
#include "s2sdk.hpp"
using namespace s2sdk;
class Sample : public plg::IPluginEntry {
public: void OnPluginStart() override {
ConVarFlag flags = ConVarFlag::LinkedConcommand | ConVarFlag::ServerCanExecute | ConVarFlag::ClientCanExecute;
AddConsoleCommand("custom_command", "A command is registered during OnPluginStart", flags,
[](int caller, int context, const plg::vector<plg::string>& arguments) -> void {
if (caller == -1) return;
PrintToServer("Custom command called.\n");
}, HookMode::Post);
}
}
from plugify.plugin import Plugin
from plugify.pps import s2sdk as s2
class Sample(Plugin):
def plugin_start(self):
flags = s2.ConVarFlag.LinkedConcommand | s2.ConVarFlag.ServerCanExecute | s2.ConVarFlag.ClientCanExecute
s2.AddConsoleCommand("custom_command", "A command is registered during OnPluginStart", flags,
lambda caller, context, arguments: (
None if caller == -1 else s2.PrintToServer("Custom command called.\n")
), s2.HookMode.Post)
package main
import (
"fmt"
"s2sdk"
"github.com/untrustedmodders/go-plugify"
)
func init() {
plugify.OnPluginStart(func() {
flags := s2sdk.ConVarFlag.LinkedConcommand | s2sdk.ConVarFlag.ServerCanExecute | s2sdk.ConVarFlag.ClientCanExecute
s2sdk.AddConsoleCommand("custom_command", "A command is registered during OnPluginStart", flags,
func(caller int, context int, arguments []string) {
if caller == -1 {
return
}
s2sdk.PrintToServer("Custom command called.\n")
}, s2sdk.HookMode.Post)
})
}
import { Plugin } from 'plugify';
import { s2 } from ':s2sdk';
export class Sample extends Plugin {
pluginStart() {
const flags = s2.ConVarFlag.LinkedConcommand | s2.ConVarFlag.ServerCanExecute | s2.ConVarFlag.ClientCanExecute;
s2.AddConsoleCommand("custom_command", "A command is registered during OnPluginStart", flags,
(caller, context, arguments) => {
if (caller === -1) return;
s2.PrintToServer("Custom command called.\n");
}, 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.ServerCanExecute, s2:ConVarFlag.ClientCanExecute)
s2:AddConsoleCommand("custom_command", "A command is registered during OnPluginStart", flags,
function(caller, context, arguments)
if caller == -1 then return end
s2:PrintToServer("Custom command called.\n")
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.
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