Today I’m going to write about what you can do with ConVars.
SourceMod is delivering a huge variation of functions and things with it you can modify, hook, etc. ConVar.
First we will start with simple things like finding ConVars or creating them, but later we will.
First of all you have to know that ConVars are brought through handles so you need for most of the actions to create or have already created a handle
But lets start with one of the easiest things: creating a ConVar
There isn’t much to explain, just look at the code and you should understand how it works:
// Create a new Handle
new Handle:h_ConVar
// Normally ConVars are created in this forward
public OnPluginStart()
{
h_ConVar = CreateConVar("mynew_convar", "1")
}
This is the simplest way of creating a ConVar, but the function “CreateConVar()” provides a huge bunch of other option, like a decription of the ConVar or flags and so on. To quote the API (http://docs.sourcemod.net/api/):
Syntax: native Handle:CreateConVar(const String:name[], const String:defaultValue[], const String:description[]="", flags=0, bool:hasMin=false, Float:min=0.0, bool:hasMax=false, Float:max=0.0); Usage: name: Name of new convar. defaultValue: String containing the default value of new convar. description: Optional description of the convar. flags: Optional bitstring of flags determining how the convar should be handled. See FCVAR_* constants for more details. hasMin: Optional boolean that determines if the convar has a minimum value. min: Minimum floating point value that the convar can have if hasMin is true. hasMax: Optional boolean that determines if the convar has a maximum value. max: Maximum floating point value that the convar can have if hasMax is true.
With this all stuff you can create ConVars that control e.g. vote ratios and so on:
// Create a new Handle
new Handle:h_ConVar
// Normally ConVars are created in this forward
public OnPluginStart()
{
h_ConVar = CreateConVar("mynewconvar_ratio", "0.75", "Ratio", _, true, 0.5, true, 1.0)
}
This creates a ConVar with a latitude between 0.5 and 1.0. Now let’s come to what flags are available for ConVar and I have to say: a lot. But you need only a few that are important. If you’re interested in the whole list just look into the API or the file “console.inc” delivered with your SourceMod installation.
But here are these which I think are the most important:
- FCVAR_PLUGIN –> Says that the CVAR is defined by a 3rd party plugin
- FCVAR_SPONLY –> This cvar cannot be changed by clients
- FCVAR_REPLICATED –> Server setting enforced on clients
- FCVAR_NOTIFY –> Notifies players when changed
- FCVAR_DONTRECORD –> Don’t record these command in demo files
That should be the most important flags, but there are also special flags that are usefull for e.g. passwords
With this knowlegde a correct ConVar creation can look like this:
// Create a new Handle
new Handle:h_ConVar
// Normally ConVars are created in this forward
public OnPluginStart()
{
h_ConVar = CreateConVar("mynewconvar_ratio", "0.75", "Ratio", FCVAR_PLUGIN | FCVAR_SPONLY | FCVAR_REPLICATED | FCVAR_NOTIFY | FCVAR_DONTRECORD, true, 0.5, true, 1.0)
}
Now let’s come to another simple thing that you can do with existing (!) ConVar, you can find them
Sounds strange but you need to do this if you want to change values for example or if you want to check if the ConVar exists so that you can hook it
But first let’s find a variable:
// Create a new Handle
new Handle:h_FindConVar
// Find ConVar
public OnPluginStart()
{
h_FindConVar = FindConVar("hostname")
}
That’s all
If the ConVar cannot be found INVALID_HANDLE will be returned. What for can I use this handle then? Well with this handle you can change the ConVar’s value:
// Create a new Handle
new Handle:h_FindConVar
// Find ConVar
public OnPluginStart()
{
h_FindConVar = FindConVar("hostname")
}
// Custom function to set hostname
SetHostname()
{
SetConVarString(h_FindConVar, "Your new Hostname", false, false)
}
That’s it! The value is changed to the new one
Another thing that you can to with an ConVar is to hook them that your script will be recogniced if the values was changed and so you can interact with these values. For example you can prevent that another admin changes your hostname. Just look at this example:
// Create a new Handle
new Handle:h_FindConVar
// Find ConVar and hook it
public OnPluginStart()
{
h_FindConVar = FindConVar("hostname")
if (h_FindConVar != INVALID_HANDLE)
{
HookConVarChange(h_FindConVar, OnHostnameChange)
}
}
// Fired when an change of the ConVar was recognized
public OnHostnameChange(Handle:ConVar, const String:OldValue[], const String:NewValue[])
{
SetConVarString(ConVar, OldValue, false, false)
}
That’s it! The ConVar stays always the same and your server’s hostname won’t change.
If you want to see other examples for the ConVars visit the Wiki of the AlliedMods (http://wiki.alliedmods.net) there I also got my information
There are hunderts of possibilities what you can do with them. When I search for the word “ConVar” in the API there are 25 results found.
Hopefully you learned s.th. with these examples and I would be glad to here some feedback, what to do better and also tell me if there’re typos
Cya
0 Responses
Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.