|
| 1 | +--- |
| 2 | +title: Console Commands |
| 3 | +description: How to add a new console command |
| 4 | +--- |
| 5 | + |
| 6 | +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. |
| 7 | + |
| 8 | +The callback function is what's invoked every time the command is used. Click here to see its prototype. Example: |
| 9 | + |
| 10 | +```csharp |
| 11 | +public void OnPluginStart() |
| 12 | +{ |
| 13 | + var flags = ConVarFlag.LinkedConcommand | ConVarFlag.ServerCanExecute | ConVarFlag.ClientCanExecute; |
| 14 | + AddConsoleCommand("sm_myslap", "slap me baby!", flags, Command_MySlap); |
| 15 | +} |
| 16 | + |
| 17 | +public ResultType Command_MySlap(int caller, int context, string[] arguments) |
| 18 | +{ |
| 19 | + return ResultType.Continue; |
| 20 | +} |
| 21 | +``` |
| 22 | + |
| 23 | +Now we've successfully implemented a command -- though it doesn't do anything yet. |
| 24 | +In fact, it will say "Unknown command" if you use it! This is because you're not returning `ResultType.Handled` in your callback. |
| 25 | +Since you haven't, Server believes you didn't want the Source2 Engine to know the command was registered, and it handles it so. |
| 26 | +The reason Server expects your function to return `ResultType.Handled` is because of the Result tag you put in your function's prototype. |
| 27 | +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. |
| 28 | + |
| 29 | +```csharp |
| 30 | +public ResultType Command_MySlap(int caller, int context, string[] arguments) |
| 31 | +{ |
| 32 | + return ResultType.Handled; |
| 33 | +} |
| 34 | +``` |
| 35 | + |
| 36 | +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. |
| 37 | +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. |
| 38 | + |
| 39 | +## Adding a Console Command |
| 40 | + |
| 41 | +You can bind commands in the `OnPluginStart` (or anywhere you like). |
| 42 | + |
| 43 | +::tabs{variant="card"} |
| 44 | + ::div{label="c#" icon="vscode-icons:file-type-csharp2"} |
| 45 | + ```csharp |
| 46 | + using Plugify; |
| 47 | + using static s2sdk.s2sdk; |
| 48 | + |
| 49 | + public unsafe class Sample : Plugin |
| 50 | + { |
| 51 | + public void OnPluginStart() |
| 52 | + { |
| 53 | + var flags = ConVarFlag.LinkedConcommand | ConVarFlag.ServerCanExecute | ConVarFlag.ClientCanExecute; |
| 54 | + AddConsoleCommand("custom_command", "A command is registered during OnPluginStart", flags, |
| 55 | + (caller, context, arguments) => |
| 56 | + { |
| 57 | + if (caller == -1) return; |
| 58 | + PrintToServer("Custom command called.\n"); |
| 59 | + }, HookMode.Post); |
| 60 | + } |
| 61 | + } |
| 62 | + ``` |
| 63 | + :: |
| 64 | + ::div{label="c++" icon="vscode-icons:file-type-cpp3"} |
| 65 | + ```cpp |
| 66 | + #include <plugify/cpp_plugin.hpp> |
| 67 | + #include "s2sdk.hpp" |
| 68 | + |
| 69 | + using namespace s2sdk; |
| 70 | + |
| 71 | + class Sample : public plg::IPluginEntry { |
| 72 | + public: |
| 73 | + void OnPluginStart() override { |
| 74 | + ConVarFlag flags = ConVarFlag::LinkedConcommand | ConVarFlag::ServerCanExecute | ConVarFlag::ClientCanExecute; |
| 75 | + AddConsoleCommand("custom_command", "A command is registered during OnPluginStart", flags, |
| 76 | + [](int caller, int context, const plg::vector<plg::string>& arguments) -> void { |
| 77 | + if (caller == -1) return; |
| 78 | + PrintToServer("Custom command called.\n"); |
| 79 | + }, HookMode::Post); |
| 80 | + } |
| 81 | + } |
| 82 | + ``` |
| 83 | + :: |
| 84 | + ::div{label="python" icon="vscode-icons:file-type-python"} |
| 85 | + ```python |
| 86 | + from plugify.plugin import Plugin |
| 87 | + from plugify.pps import s2sdk as s2 |
| 88 | + |
| 89 | + class Sample(Plugin): |
| 90 | + def plugin_start(self): |
| 91 | + flags = s2.ConVarFlag.LinkedConcommand | s2.ConVarFlag.ServerCanExecute | s2.ConVarFlag.ClientCanExecute |
| 92 | + s2.AddConsoleCommand("custom_command", "A command is registered during OnPluginStart", flags, |
| 93 | + lambda caller, context, arguments: ( |
| 94 | + None if caller == -1 else s2.PrintToServer("Custom command called.\n") |
| 95 | + ), s2.HookMode.Post) |
| 96 | + ``` |
| 97 | + :: |
| 98 | + ::div{label="go" icon="vscode-icons:file-type-go"} |
| 99 | + ```go |
| 100 | + package main |
| 101 | + |
| 102 | + import ( |
| 103 | + "fmt" |
| 104 | + "s2sdk" |
| 105 | + "github.com/untrustedmodders/go-plugify" |
| 106 | + ) |
| 107 | + |
| 108 | + func init() { |
| 109 | + plugify.OnPluginStart(func() { |
| 110 | + flags := s2sdk.ConVarFlag.LinkedConcommand | s2sdk.ConVarFlag.ServerCanExecute | s2sdk.ConVarFlag.ClientCanExecute |
| 111 | + s2sdk.AddConsoleCommand("custom_command", "A command is registered during OnPluginStart", flags, |
| 112 | + func(caller int, context int, arguments []string) { |
| 113 | + if caller == -1 { |
| 114 | + return |
| 115 | + } |
| 116 | + s2sdk.PrintToServer("Custom command called.\n") |
| 117 | + }, s2sdk.HookMode.Post) |
| 118 | + }) |
| 119 | + } |
| 120 | + ``` |
| 121 | + :: |
| 122 | + ::div{label="js" icon="vscode-icons:file-type-js-official"} |
| 123 | + ```js |
| 124 | + import { Plugin } from 'plugify'; |
| 125 | + import { s2 } from ':s2sdk'; |
| 126 | +
|
| 127 | + export class Sample extends Plugin { |
| 128 | + pluginStart() { |
| 129 | + const flags = s2.ConVarFlag.LinkedConcommand | s2.ConVarFlag.ServerCanExecute | s2.ConVarFlag.ClientCanExecute; |
| 130 | + s2.AddConsoleCommand("custom_command", "A command is registered during OnPluginStart", flags, |
| 131 | + (caller, context, arguments) => { |
| 132 | + if (caller === -1) return; |
| 133 | + s2.PrintToServer("Custom command called.\n"); |
| 134 | + }, s2.HookMode.Post); |
| 135 | + } |
| 136 | + } |
| 137 | + ``` |
| 138 | + :: |
| 139 | + ::div{label="lua" icon="vscode-icons:file-type-lua"} |
| 140 | + ```lua |
| 141 | + local plugify = require 'plugify' |
| 142 | + local Plugin = plugify.Plugin |
| 143 | + local s2 = require 's2sdk' |
| 144 | + |
| 145 | + local Sample = {} |
| 146 | + setmetatable(Sample, { __index = Plugin }) |
| 147 | + |
| 148 | + function Sample:plugin_start() |
| 149 | + local flags = bit.bor(s2:ConVarFlag.LinkedConcommand, s2:ConVarFlag.ServerCanExecute, s2:ConVarFlag.ClientCanExecute) |
| 150 | + s2:AddConsoleCommand("custom_command", "A command is registered during OnPluginStart", flags, |
| 151 | + function(caller, context, arguments) |
| 152 | + if caller == -1 then return end |
| 153 | + s2:PrintToServer("Custom command called.\n") |
| 154 | + end, s2:HookMode.Post) |
| 155 | + end |
| 156 | + |
| 157 | + local M = {} |
| 158 | + M.Sample = Sample |
| 159 | + return M |
| 160 | + ``` |
| 161 | + :: |
| 162 | +:: |
| 163 | + |
| 164 | +## Accessing Command Parameters |
| 165 | + |
| 166 | +`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. |
| 167 | + |
| 168 | +```csharp |
| 169 | +public void OnCommand(int caller, int context, string[] arguments) |
| 170 | +{ |
| 171 | + Console.Write($@" |
| 172 | + Command Caller: {caller} |
| 173 | + Command Context: {context} |
| 174 | + Arg Count: {arguments.Length} |
| 175 | + Arg String: {arguments} |
| 176 | + First Argument: {arguments[0]} |
| 177 | + Second Argument: {command[1]}"); |
| 178 | +} |
| 179 | +``` |
| 180 | + |
| 181 | +```shell |
| 182 | +> custom_command "Test Quoted" 5 13 |
| 183 | + |
| 184 | +# Output |
| 185 | +Command Caller: 1 |
| 186 | +Command Context: 0 |
| 187 | +Arg Count: 4 |
| 188 | +Arg String: "Test Quoted" 5 13 |
| 189 | +First Argument: custom_command |
| 190 | +Second Argument: Test Quoted |
| 191 | +``` |
0 commit comments