View Single Post
Psyk0tik
Veteran Member
Join Date: May 2012
Location: Homeless
Old 02-13-2019 , 03:46   Re: To hook or not to hook, that is the question.
Reply With Quote #8

Quote:
Originally Posted by PatriotGames View Post
Thanks, Dragokas, that makes sense.

PG
To add more to this, we hook a cvar handle to a changehook if we want something to happen as soon as the value of that cvar is changed. (i.e. g_cvIsOn.AddChangeHook(vIsOnHook) or HookConVarChange(g_hIsOn, vIsOnHook))

If we just want to check the current value of the cvar, we can just call it when we need it (or cache it to a variable if you prefer that). (i.e. bIsOn = g_cvIsOn.BoolValue or bIsOn = GetConVarBool(g_hIsOn))

In this example, we're going to check the initial value of the "mp_gamemode" cvar and then hook it so we can check for the value whenever it changes:

PHP Code:
ConVar g_cvMPGameMode;
bool g_bIsCampaign;

public 
void OnPluginStart()
{
    
g_cvMPGameMode FindConVar("mp_gamemode");

    if (
g_cvMPGameMode != null)
    {
        
g_cvMPGameMode.AddChangeHook(vMPGameModeHook);
    }
}

public 
void OnMapStart()
{
    if (
g_cvMPGameMode != null)
    {
        
char sGameMode[32];
        
g_cvMPGameMode.GetString(sGameModesizeof(sGameMode));

        if (
StrEqual(sGameMode"versus"))
        {
            
g_bIsCampaign false;
        }
    }
}

public 
void vMPGameModeHook(ConVar convar, const char[] oldVal, const char[] newVal)
{
    if (
StrEqual(newVal"versus"))
    {
        
g_bIsCampaign false;
    }
    else
    {
        
g_bIsCampaign true;
    }

I assume you've made this thread due to the changes I made to your plugin last week. I'm sorry for not explaining it to you or adding comments in the code to help you. If you want, we can discuss any other questions you may have in private chat or on Discord.
__________________
Psyk0tik is offline