Cvars
A cvar is a server setting with a name: mp_freezetime, sv_gravity, or one
of your plugin's own. A Cvar object reads and writes one:
const freezeTime = new Cvar("mp_freezetime");
const speed = new Cvar("myplugin_speed", "250"); // created with "250" if the server has no such cvar
speed.addEventListener("change", (event) => {
console.log(`${event.cvar.name}: ${event.oldValue} -> ${event.value}`);
});
function startWarmup() {
freezeTime.number = 0;
console.log(`speed ${speed.number}, text "${speed.value}", on: ${speed.boolean}`);
}
Like an input's value, a cvar is text underneath, and it can be read and
written three ways:
| Property | Reads | Writes |
|---|---|---|
value | the text: "250" | the text |
number | a number: 250 | a number; a whole one is written without a fraction ("5", not "5.000000") |
boolean | true for anything but 0 | true as 1, false as 0 |
The second argument of new Cvar is the default. When the server has no cvar
by that name, it is created with that value - the way a plugin adds its own
settings. Without a default, a missing cvar stays missing: exists is
false, reading gives "", 0 and false, and writing does nothing.
Make a Cvar once, at the top of the file, and use it from there. A Cvar
made at the top of the file finds its cvar once the server has loaded every
plugin, so the game's own cvars and those of other plugins are there by the
time your code reads them.
Hearing a change
addEventListener("change", listener) calls the listener on every change -
from the console, from amx_cvar, from another plugin or from this one - with
event.cvar, event.oldValue and event.value (both as text).
removeEventListener("change", listener) with the same function stops it.
A plugin that reads its settings once can do it in the "configsExecuted" event, after
the server has run its config files (Plugin):
const reward = new Cvar("myplugin_reward", "300");
let rewardMoney = 0;
server.addEventListener("configsExecuted", () => {
rewardMoney = reward.number;
});
For settings richer than a few numbers - lists, nested sections, per-map values - a config file is often the better place (Configs).
Flags
Some game data is a set of on/off flags: which buttons a player holds, which parts of the HUD are hidden, whether an entity is on the ground. In a plugin such a set is an array of names:
Messages
The server tells each client what to draw and play through messages: a chat line, a HUD icon, the round clock, a death notice. A plugin hears a message before it leaves, reads it, changes it or stops it: