View Single Post
Author Message
PatriotGames
AlliedModders Donor
Join Date: Feb 2012
Location: root@irs:/# rm -rf /
Old 02-05-2019 , 14:52   To hook or not to hook, that is the question.
Reply With Quote #1

Please help a new coder understand the current recommended practice for tracking convar changes in both transitional and methodmap syntax.

In this example we have several different approaches:
PHP Code:
ConVar g_cvMyCvar;
bool g_bMyCvar;

public 
void OnPluginStart()
{
    
g_cvMyCvar CreateConVar("my_cvar""0""This cvar does something. 0 = Disable, 1 = Enable"FCVAR_NOTIFY);
    
    
g_bMyCvar GetConVarBool(g_cvMyCvar); //set bool var to cvar value
    
HookConvarChange(g_cvMyCvar CvarsChanged); // transitional hook cvar change function
    
    
g_cvMyCvar.AddChangeHook(CvarsChanged); //methodmap hook cvar change property
    
}

public 
void CvarsChanged(Handle convar, const char oldValue[], const char newValue[])
{
    
GetCvars();
}

public 
void GetCvars()
{
    
g_bMyCvar GetConVarBool(g_cvMyCvar); //transitional syntax
    
g_bMyCvar g_cvMyCvar.BoolValue;    //methodmap property
}

void MyCvarFunction()
{
    if (
GetConvarBool(g_cvMyCvar)); //does not use a cvar hook at all. Is this sub-optimal?
    
{
        
//code
    
}
}

void MyCvarBoolFunction()
{
    If (
g_bMyCvar//transitional syntax and methodmap property use the bool var for g_cvMyCvar. Any advantage of one over the other?
    
{
        
// code
    
}

void MyCvarMethodMapFunction()
{
    If (
g_cvMyCvar.BoolValue))    // A methodmap that uses the cvar directly without the hook property. Is this ok?
    
{
        
// code
    
}

What is the recommended practice?

Bonus question: what's the optimal method for using cvars to calculate another cvars new value?

Exmple:
PHP Code:
void SomeFunction()
{
    
SetConVarInt(g_cvSomeCvar, (g_iAnotherCvar + (g_iYetAnotherCvar g_iSomeConstant)));

Thanks,
PG

Last edited by PatriotGames; 02-05-2019 at 15:55.
PatriotGames is offline