Difference between revisions of "SourcePawn Basics - Customization through ConVars"

From AlliedModders Wiki
Jump to: navigation, search
m
(More content)
Line 3: Line 3:
 
Server owners like customization.  Lots of customization.  So, how do you add customization to your plugin?  The simplest route is by adding ConVars.
 
Server owners like customization.  Lots of customization.  So, how do you add customization to your plugin?  The simplest route is by adding ConVars.
  
== ConVars / CVars ==
+
== General Info ==
  
 
Console Variables (ConVars or CVars for short) are server settings that can be changed by server owners.  Anything you put into your server's server.cfg file is a convar.
 
Console Variables (ConVars or CVars for short) are server settings that can be changed by server owners.  Anything you put into your server's server.cfg file is a convar.
Line 9: Line 9:
 
Just like the server itself, plugins can create their own convars.  A convar is different from a regular variable in that it is visible outside your plugin.
 
Just like the server itself, plugins can create their own convars.  A convar is different from a regular variable in that it is visible outside your plugin.
  
=== Creating your own ConVars ===
+
== Creating your own ConVars ==
  
 
ConVars should always be created in OnPluginStart.
 
ConVars should always be created in OnPluginStart.
Line 26: Line 26:
 
; min : A Float with the maximum allowed value
 
; min : A Float with the maximum allowed value
  
==== Useful ConVar Flags ====
+
=== Useful ConVar Flags ===
  
 
* <tt>FCVAR_NONE</tt> - The default, no flags set.
 
* <tt>FCVAR_NONE</tt> - The default, no flags set.
Line 36: Line 36:
 
* <tt>FCVAR_PLUGIN</tt> - Defined by a 3rd Party plugin.  SourceMod probably adds this already even if you don't.
 
* <tt>FCVAR_PLUGIN</tt> - Defined by a 3rd Party plugin.  SourceMod probably adds this already even if you don't.
  
=== Locating ConVars from the game or other plugins ===
+
== Standard ConVars ==
 +
 
 +
There are a few standard ConVars that most plugins have.
 +
 
 +
The first is a version ConVar.  Unlike most ConVars, you don't need to store the <tt>Handle</tt> of this one as you never need to read its value.
 +
 
 +
Normally, your version ConVar will look a little like this.
 +
 
 +
  #define VERSION "1.0"
 +
 
 +
  public OnPluginStart()
 +
  {
 +
      CreateConVar("test_version", VERSION, "Test Plugin version", FCVAR_NOTIFY|FCVAR_DONTRECORD|FCVAR_PLUGIN|FCVAR_SPONLY);
 +
  }
 +
 
 +
VERSION is defined here so that it can be used in the plugin's myinfo struct as well.
 +
 
 +
A second common ConVar is the enable ConVar.
 +
 
 +
  new Handle:g_hEnabled;
 +
 
 +
  public OnPluginStart()
 +
  {
 +
      g_hEnabled = CreateConVar("test_enable", "1", "Test Plugin Enable", FCVAR_NOTIFY|FCVAR_DONTRECORD, true, 0.0, true, 1.0);
 +
  }
 +
 
 +
Note that FCVAR_DONTRECORD is still present.  This is a recommendation in case your plugin has an <tt>AutoExecConfig</tt> file so that your enable ConVar can be controlled by map configurations or server.cfg.
 +
 
 +
FCVAR_NOTIFY is here as well so that GameTracker and the like can tell if the plugin is enabled.
 +
 
 +
=== Loading ConVars from a file ===
 +
 
 +
SourceMod has a command <tt>AutoExecConfig</tt> to automatically read/create a ConVar configuration file.  To use it, put this at the end of OnPluginStart after your CreateConVar lines:
 +
 
 +
      AutoExecConfig(true, "nameofplugin");
 +
 
 +
which will create a file named <tt>cfg/sourcemod/nameofplugin.cfg</tt>
 +
 
 +
== Locating ConVars from the game or other plugins ==
  
 
Looking up plugins is an expensive operation and should be done just once if you can help it.
 
Looking up plugins is an expensive operation and should be done just once if you can help it.
Line 52: Line 90:
  
 
== Reading and Setting ConVars ==
 
== Reading and Setting ConVars ==
 +
 +
Both ConVars from your plugin and ConVars from the game or other plugins can be read the same way.
  
 
ConVars can be read as several different kinds of values: cell (int), bool, Float, or String.
 
ConVars can be read as several different kinds of values: cell (int), bool, Float, or String.
Line 71: Line 111:
  
 
This pattern is common for functions that do String manipulations.
 
This pattern is common for functions that do String manipulations.
 
== Standard ConVars ==
 
 
There are a few standard ConVars that most plugins have.
 
 
The first is a version ConVar.  Unlike most ConVars, you don't need to store the <tt>Handle</tt> of this one as you never need to read its value.
 
 
Normally, your version ConVar will look a little like this.
 
 
  #define VERSION "1.0"
 
 
 
  public OnPluginStart()
 
  {
 
      CreateConVar("test_version", VERSION, "Test Plugin version", FCVAR_NOTIFY|FCVAR_DONTRECORD|FCVAR_PLUGIN|FCVAR_SPONLY);
 
  }
 
 
VERSION is defined here so that it can be used in the plugin's myinfo struct as well.
 
 
A second common ConVar is the enable ConVar.
 
 
  new Handle:g_hEnabled;
 
 
 
  public OnPluginStart()
 
  {
 
      g_hEnabled = CreateConVar("test_enable", "1", "Test Plugin Enable", FCVAR_NOTIFY|FCVAR_DONTRECORD, true, 0.0, true, 1.0);
 
  }
 
 
Note that FCVAR_DONTRECORD is still present.  This is a recommendation in case your plugin has an <tt>AutoExecConfig</tt> file.
 
 
You would then read this cvar with
 
 
      if (GetConVarBool(g_hEnabled))
 
      {
 
          // The plugin is currently enabled
 
      }
 
  
 
[[Category:SourcePawn Basics]]
 
[[Category:SourcePawn Basics]]

Revision as of 14:40, 18 October 2013

Prerequisite: This guide assumes you have read Introduction to SourcePawn and Introduction to SourceMod Plugins.

Server owners like customization. Lots of customization. So, how do you add customization to your plugin? The simplest route is by adding ConVars.

General Info

Console Variables (ConVars or CVars for short) are server settings that can be changed by server owners. Anything you put into your server's server.cfg file is a convar.

Just like the server itself, plugins can create their own convars. A convar is different from a regular variable in that it is visible outside your plugin.

Creating your own ConVars

ConVars should always be created in OnPluginStart.

When you create your ConVars, they should name them consistently. A good naming practice is nameofplugin_settingname or nameofplugin_setting_name.

ConVars are created via the CreateConVar command. The order of arguments are:

name 
The convar name, preferably using the naming scheme above
defaultValue 
A string with the default value.
description 
A description of what this ConVar does for the find or listcvars commands
flags 
An | separate list of convar flags
hasMin 
A bool stating whether the min argument exists
min 
A Float with the minimum allowed value
hasMax 
A bool stating whether the max argument exists
min 
A Float with the maximum allowed value

Useful ConVar Flags

  • FCVAR_NONE - The default, no flags set.
  • FCVAR_PROTECTED - Used for passwords. Don't send this value to clients who ask for it, only send 1 if it's set or 0 if it isn't.
  • FCVAR_SPONLY - Not changeable in multiplayer
  • FCVAR_NOTIFY - Players are informed when this ConVar changes. ConVars marked with this attribute are also visible to tracking services.
  • FCVAR_NEVER_AS_STRING - Don't try to print this out
  • FCVAR_DONTRECORD - Not recorded in demo files. Also, not recorded in AutoExecConfig files.
  • FCVAR_PLUGIN - Defined by a 3rd Party plugin. SourceMod probably adds this already even if you don't.

Standard ConVars

There are a few standard ConVars that most plugins have.

The first is a version ConVar. Unlike most ConVars, you don't need to store the Handle of this one as you never need to read its value.

Normally, your version ConVar will look a little like this.

 #define VERSION "1.0"
 
 public OnPluginStart()
 {
     CreateConVar("test_version", VERSION, "Test Plugin version", FCVAR_NOTIFY|FCVAR_DONTRECORD|FCVAR_PLUGIN|FCVAR_SPONLY);
 }

VERSION is defined here so that it can be used in the plugin's myinfo struct as well.

A second common ConVar is the enable ConVar.

 new Handle:g_hEnabled;
 
 public OnPluginStart()
 {
     g_hEnabled = CreateConVar("test_enable", "1", "Test Plugin Enable", FCVAR_NOTIFY|FCVAR_DONTRECORD, true, 0.0, true, 1.0);
 }

Note that FCVAR_DONTRECORD is still present. This is a recommendation in case your plugin has an AutoExecConfig file so that your enable ConVar can be controlled by map configurations or server.cfg.

FCVAR_NOTIFY is here as well so that GameTracker and the like can tell if the plugin is enabled.

Loading ConVars from a file

SourceMod has a command AutoExecConfig to automatically read/create a ConVar configuration file. To use it, put this at the end of OnPluginStart after your CreateConVar lines:

     AutoExecConfig(true, "nameofplugin");

which will create a file named cfg/sourcemod/nameofplugin.cfg

Locating ConVars from the game or other plugins

Looking up plugins is an expensive operation and should be done just once if you can help it.

The best location to do this is in the OnAllPluginsLoaded callback.

For example, here is how to look up the mp_autoteambalance ConVar

 new Handle:g_hAutoTeamBalance;
 
 public OnAllPluginsLoaded()
 {
     g_hAutoTeamBalance = FindConVar("mp_autoteambalance");
 }

Reading and Setting ConVars

Both ConVars from your plugin and ConVars from the game or other plugins can be read the same way.

ConVars can be read as several different kinds of values: cell (int), bool, Float, or String.

Here is how you read a bool ConVar:

     new bool:value = GetConVarInt(g_hAutoTeamBalance);

and to set the same ConVar:

     SetConVarInt(g_hAutoTeamBalance, true);

Int and Floats work roughly the same way with GetConVarInt / GetConVarFloat / SetConVarInt / SetConVarFloat.

Strings work the same for setting, but reading a String ConVar works slightly differently:

   decl String:value[64];
   GetConVarString(myConVar, value, sizeof(value));

This pattern is common for functions that do String manipulations.