Skip to content

Commit 9834f1a

Browse files
committed
fix: add sdk guides
1 parent a0a55d0 commit 9834f1a

File tree

7 files changed

+476
-10
lines changed

7 files changed

+476
-10
lines changed

content/3.use-cases/1.metamod-plugin/2.installation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ This guide will walk you through the installation process for **MMS2-Plugify** o
1111
::steps
1212
### **Install Metamod**
1313
[Metamod (> 2.0)](https://www.sourcemm.net/downloads.php/?branch=master) is required for loading Plugify.
14-
:read-more{icon="lucide:link" to="/use-cases/metamod=plugin/metamod"}
14+
:read-more{icon="lucide:link" to="/use-cases/metamod-plugin/metamod"}
1515
::
1616

1717
## Installation Steps

content/6.plugins/5.s2sdk/3.guide.md

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
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+
```
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
---
2+
title: Console Variables
3+
description: How to read & write console variables (ConVars).
4+
---
5+
6+
## Finding a ConVar
7+
8+
Use the `FindConVar` static method to find a reference to an existing ConVar handle (or `null`).
9+
10+
```csharp
11+
var cheatsCvar = FindConVar("sv_cheats");
12+
```
13+
14+
## Manipulating Primitive Values
15+
16+
Reading the value of a ConVar will depend on the type; for basic value Cvars (like float, int, bool) you can use the `GetConVar` method to get a `copy` as the value.
17+
18+
```csharp
19+
var value = GetConVarBool(cheatsCvar); // false
20+
```
21+
22+
Because this is passed by copy, you can't simply set this value to change the underlying value, e.g. You need to use the `SetConVar` method to change the value of a ConVar.
23+
24+
```csharp
25+
SetConVarBool(cheatsCvar, true, /*replicate*/true, /*replicate*/false);
26+
27+
// You can also use the simplified helper:
28+
SetConVar(cheatsCvar, true);
29+
```
30+
31+
## Manipulating Objects
32+
33+
By default, the `GetConVar` method will return a string value for string Cvars. You can use the `GetConVarString` method to get a `String` object, which you can then manipulate as normal. To set the value back, you will need to use the `SetConVarString` method.
34+
35+
```csharp
36+
var stringCvar = FindConVar("sv_skyname");
37+
var skyName = GetConVarString(stringCvar);
38+
PrintToServer($"sv_skyname = {skyName}");
39+
40+
// You can then manipulate the string as normal.
41+
skyName = "foobar";
42+
43+
SetConVarString(stringCvar, skyName);
44+
```
45+
46+
## Creating ConVars
47+
48+
You can create your own ConVars using the `CreateConVar` method. This is useful for creating custom settings for your plugin.
49+
50+
```csharp
51+
var cvar = CreateConVar("my_plugin_cvar", "default_value", "This is my custom ConVar", ConVarFlags.None);
52+
```

0 commit comments

Comments
 (0)