Raised This Month: $32 Target: $400
 8% 

Solved Work with ConVars


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
OneMore
Senior Member
Join Date: Feb 2019
Old 03-07-2021 , 09:48   Work with ConVars
Reply With Quote #1

Please advise what is the most effective way to work with ConVars from a resources and processor utilisation point of view?

One option is with global variables.
Code:
ConVar g_Cvar1;
int g_i1;

public OnPluginStart() {
    g_Cvar1 = CreateConVar("sm_cvar1", "20", "cvar descr.", 0, true, 0.0, true, 40.0);
    g_i1 = GetConVarInt(g_Cvar1);
}
SomeFunction() {
   int somevalue = g_i1 *5;
}
Another option is just using a method like g_Cvar1.IntValue
Let's assume that this function will be used once per minute for each player.

One more question is what is the best way now to look for ConVar changes? If I use global variables like in the above example, I use quite a lot of code to follow the convars changes:
Code:
public OnPluginStart() {
    g_Cvar1 = CreateConVar("sm_cvar1", "1", "Set 0 to disable", 0, true, 0.0, true, 1.0);
    HookConVarChange(g_Cvar1, OnSettingsChange);
    g_Cvar2 = CreateConVar("sm_cvar2", "10", "10 is good", 0, true, 0.0, true, 10.0);
    HookConVarChange(g_Cvar2, OnSettingsChange);
    AutoExecConfig(true,"bestplugin","sourcemod");
}
public void OnConfigsExecuted() {
    g_b1 = GetConVarBool(g_Cvar1);
    g_i2 = GetConVarInt(g_Cvar2);
}
void OnSettingsChange(ConVar convar, const char[] oldValue, const char[] newValue)
{
    if (convar == g_Cvar1)
        g_b1 = StringToInt(newValue) == 1 ? true : false;
    else if (convar == g_Cvar2)
        g_i2 = StringToInt(newValue);
}
Maybe you can advise a more effective way?

Last edited by OneMore; 03-07-2021 at 12:09.
OneMore is offline
Ilusion9
Veteran Member
Join Date: Jun 2018
Location: Romania
Old 03-07-2021 , 10:08   Re: Work with ConVars
Reply With Quote #2

You dont have to store the convar value in another variable, just use g_Cvar1.IntValue
Also, you dont have to use the same function for HookConVarChange

PHP Code:

public OnPluginStart()
{
    
g_Cvar1 CreateConVar("sm_cvar1""1""Set 0 to disable"0true0.0true1.0);
    
g_Cvar2 CreateConVar("sm_cvar2""10""10 is good"0true0.0true10.0);
}

void someFunction()
{
    
// use g_Cvar1.IntValue
    // use g_Cvar2.IntValue

__________________
Ilusion9 is offline
OneMore
Senior Member
Join Date: Feb 2019
Old 03-07-2021 , 10:27   Re: Work with ConVars
Reply With Quote #3

Quote:
Originally Posted by Ilusion9 View Post
You dont have to store the convar value in another variable, just use g_Cvar1.IntValue
Also, you dont have to use the same function for HookConVarChange
Ok, but if I have let's say about 6-8 convars and their value is requested once per minute for each player, is it about as efficient as to store their value in regular variables and use them instead of convar methods?

I care about to have less processor load to avoid lags on the server. If we talk about a one-two convars and requesting their values several times per map, of course, your example is perfect. But what about high load?

Last edited by OneMore; 03-07-2021 at 10:30.
OneMore is offline
Ilusion9
Veteran Member
Join Date: Jun 2018
Location: Romania
Old 03-07-2021 , 11:57   Re: Work with ConVars
Reply With Quote #4

Quote:
Originally Posted by OneMore View Post
I care about to have less processor load to avoid lags on the server. If we talk about a one-two convars and requesting their values several times per map, of course, your example is perfect. But what about high load?
g_Cvar1.IntValue is already stored in memory. IntValue is a class member (just an integer variable), not a function (method). g_Cvar1.IntValue is changed automatically when the convar changes.
__________________
Ilusion9 is offline
OneMore
Senior Member
Join Date: Feb 2019
Old 03-07-2021 , 12:09   Re: Work with ConVars
Reply With Quote #5

Ok, thanks for the help. There are not too many resources to read about what is under the hood...
OneMore is offline
Marttt
Veteran Member
Join Date: Jan 2019
Location: Brazil
Old 03-07-2021 , 15:57   Re: Work with ConVars
Reply With Quote #6

Well, there are some scenarios where makes sense storing some information related to the cvar, usually when you have to do some manipulation with that.

Example: I have a cvar that stores a string about color

somecvar_name_color "255 0 0"

Then I need to convert it into an int / int[3] / int[4] to use in some netprops/keyvalues inside an entity.

Where a string is more user friendly, although the entity needs that in another format.

Otherwise, you should be fine using the (e.g.) IntValue directly from the ConVar.


As @Ilusion9 said, you don't need to use the same HookConVarChange naming.
But I use cause is easier to maintain later. (usually cvars aren't supposed to change so often, unless in specific scenarios)
__________________

Last edited by Marttt; 03-07-2021 at 15:59.
Marttt is offline
farawayf
Senior Member
Join Date: Jan 2019
Old 03-08-2021 , 07:06   Re: Work with ConVars
Reply With Quote #7

something like this will be more optimised

PHP Code:
char cValue[12];
int iValue;

public 
void OnPluginStart()
{
    
ConVar cvar;
    
cvar CreateConVar("sm_cvar_colorvalue""255 255 255");
    
cvar.GetString(cValue12);
    
// cvar.AddChangeHook(HookCallback);

    
cvar CreateConVar("sm_cvar_number""123");
    
iValue cvar.IntValue;
    
// cvar.AddChangeHook(HookCallback);


Last edited by farawayf; 03-08-2021 at 07:07.
farawayf is offline
asherkin
SourceMod Developer
Join Date: Aug 2009
Location: OnGameFrame()
Old 03-08-2021 , 07:33   Re: Work with ConVars
Reply With Quote #8

Quote:
Originally Posted by Ilusion9 View Post
g_Cvar1.IntValue is already stored in memory. IntValue is a class member (just an integer variable), not a function (method). g_Cvar1.IntValue is changed automatically when the convar changes.
IntValue is a property backed by native code, accessing it is a function call.

It is still very cheap though - don't write unreadable soup in the name of premature optimization.
__________________

Last edited by asherkin; 03-08-2021 at 07:33.
asherkin is offline
MAGNAT2645
Senior Member
Join Date: Nov 2015
Location: AlliedMods.net
Old 03-08-2021 , 08:44   Re: Work with ConVars
Reply With Quote #9

Quote:
Originally Posted by Ilusion9 View Post
g_Cvar1.IntValue is already stored in memory. IntValue is a class member (just an integer variable), not a function (method). g_Cvar1.IntValue is changed automatically when the convar changes.
There's no real "class" and "class member" terms in SourcePawn.
As Asherkin said, ConVar.(Int|Float|Bool)Value are properties which are represented as native functions (so they're kinda like methods).
You can't store anything in methodmap (in this case enum struct should be useful). In my understanding property is a simplified method that doesn't accept arguments (as functions do), it has just one magic this parameter (and setters have second parameter which represented as value to be set).
__________________
MAGNAT2645 is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 12:36.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode