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

ConVar changes not working


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
ThatKidWhoGames
Veteran Member
Join Date: Jun 2013
Location: IsValidClient()
Old 09-03-2017 , 11:18   ConVar changes not working
Reply With Quote #1

Trying to hook convar changes and changing them seems to not update the convar value if the cvar is changed in game with the sm_cvar command. Here is the code:
PHP Code:
#pragma semicolon 1

#define PLUGIN_AUTHOR "Sgt. Gremulock"
#define PLUGIN_VERSION "1.0.2"

#include <sourcemod>

// ConVars
ConVar cv_ChatText;
ConVar cv_CenterText;

// Booleans
new bool:bChatText;
new 
bool:bCenterText;

public 
Plugin myinfo 
{
    
name "[TF2] End Round Friendly Fire",
    
author PLUGIN_AUTHOR,
    
description "Enable's friendly fire at the end of a round and disables it on the start of the next round.",
    
version PLUGIN_VERSION,
    
url "grem-co.com"
};

public 
void OnPluginStart()
{
    
HookEvent("teamplay_round_win"Event_RoundWin);
    
HookEvent("teamplay_round_start"Event_RoundStart);

    
cv_ChatText CreateConVar("sm_endroundff_chat""1""Announce ff status in chat?"_true0.0true1.0);
    
HookConVarChange(cv_ChatTextConVar_ChatText);
    
bChatText GetConVarBool(cv_ChatText);

    
cv_CenterText CreateConVar("sm_endroundff_center""1""Announce ff status in center text?"_true0.0true1.0);
    
HookConVarChange(cv_CenterTextConVar_CenterText);
    
bCenterText GetConVarBool(cv_CenterText);

    
CreateConVar("sm_endroundff_version"PLUGIN_VERSION"Plugin's version."FCVAR_REPLICATED|FCVAR_NOTIFY);

    
AutoExecConfig(true"endroundff""sourcemod");
}

public 
ConVar_ChatText(Handle:convar, const String:oldValue[], const String:newValue[])
{
    if (
bChatText)
    {
        
SetConVarBool(convartrue);
    }
    else
    {
        
SetConVarBool(convarfalse);
    }
}

public 
ConVar_CenterText(Handle:convar, const String:oldValue[], const String:newValue[])
{
    if (
bCenterText)
    {
        
SetConVarBool(convartrue);
    }
    else
    {
        
SetConVarBool(convarfalse);
    }
}

public 
void OnConfigsExecuted()
{
    
GetConVarBool(cv_ChatText);
    
GetConVarBool(cv_CenterText);
}

public 
Action Event_RoundWin(Event event, const char[] namebool dontBroadcast)
{
    
SetConVarInt(FindConVar("mp_friendlyfire"), 1);

    if (
bChatText && !bCenterText)
    {
        
PrintToChatAll("[SM] End of round friendly fire has been enabled.");
    }
    else if (
bCenterText && !bChatText)
    {
        
PrintCenterTextAll("End of round friendly fire has been enabled.");
    }
    else if (
bChatText && bCenterText)
    {
        
PrintCenterTextAll("End of round friendly fire has been enabled.");
        
PrintToChatAll("[SM] End of round friendly fire has been enabled.");
    }
}

public 
Action Event_RoundStart(Event event, const char[] namebool dontBroadcast)
{
    
SetConVarInt(FindConVar("mp_friendlyfire"), 0);

    if (
bChatText && !bCenterText)
    {
        
PrintToChatAll("[SM] End of round friendly fire has been disabled.");
    }
    else if (
bCenterText && !bChatText)
    {
        
PrintCenterTextAll("End of round friendly fire has been disabled.");
    }
    else if (
bChatText && bCenterText)
    {
        
PrintCenterTextAll("End of round friendly fire has been disabled.");
        
PrintToChatAll("[SM] End of round friendly fire has been disabled.");
    }

ThatKidWhoGames is offline
OSWO
Senior Member
Join Date: Jul 2015
Location: United Kingdom, London
Old 09-03-2017 , 11:40   Re: ConVar changes not working
Reply With Quote #2

You don't need to make a variable to store the data of a ConVar. You can call

PHP Code:
<convar>.BoolValue 
https://sm.alliedmods.net/new-api/co...nVar/BoolValue
__________________
SourceTimer | WeaponSkins++ | BasePlugins++ https://github.com/OSCAR-WOS
OSWO is offline
ThatKidWhoGames
Veteran Member
Join Date: Jun 2013
Location: IsValidClient()
Old 09-03-2017 , 11:53   Re: ConVar changes not working
Reply With Quote #3

Quote:
Originally Posted by OSWO View Post
You don't need to make a variable to store the data of a ConVar. You can call

PHP Code:
<convar>.BoolValue 
https://sm.alliedmods.net/new-api/co...nVar/BoolValue
Thanks! I'll let you know if it works
ThatKidWhoGames is offline
cravenge
Veteran Member
Join Date: Nov 2015
Location: Chocolate Factory
Old 09-03-2017 , 18:50   Re: ConVar changes not working
Reply With Quote #4

Cleaned up the code and got rid of the old syntax:
PHP Code:
#pragma semicolon 1
#include <sourcemod>

#define PLUGIN_AUTHOR "Sgt. Gremulock"
#define PLUGIN_VERSION "1.0.2"

ConVar cv_ChatTextcv_CenterText
bool bChatTextbCenterText

public 
Plugin myinfo =  

    
name "[TF2] End Round Friendly Fire"
    
author PLUGIN_AUTHOR
    
description "Enable's friendly fire at the end of a round and disables it on the start of the next round."
    
version PLUGIN_VERSION
    
url "grem-co.com" 
}; 

public 
void OnPluginStart()
{
    
CreateConVar("sm_endroundff_version"PLUGIN_VERSION"Plugin's version."FCVAR_REPLICATED|FCVAR_NOTIFY);
    
cv_ChatText CreateConVar("sm_endroundff_chat""1""Announce ff status in chat?"_true0.0true1.0);
    
cv_CenterText CreateConVar("sm_endroundff_center""1""Announce ff status in center text?"_true0.0true1.0);
    
    
bChatText cv_ChatText.BoolValue;
    
bCenterText cv_CenterText.BoolValue;

    
cv_ChatText.AddChangeHook(ConVar_ChatText);
    
cv_CenterText.AddChangeHook(ConVar_CenterText);
    
    
AutoExecConfig(true"endroundff""sourcemod");
    
    
HookEvent("teamplay_round_win"Event_RoundWin);
    
HookEvent("teamplay_round_start"Event_RoundStart);


public 
void ConVar_ChatText(ConVar cvar, const char[] sOldValue, const char[] sNewValue)
{
    
bChatText cv_ChatText.BoolValue;
}

public 
void ConVar_CenterText(ConVar cvar, const char[] sOldValue, const char[] sNewValue)
{
    
bCenterText cv_CenterText.BoolValue;
}

public 
Action Event_RoundWin(Event event, const char[] namebool dontBroadcast

    
FindConVar("mp_friendlyfire").SetInt(1);
    
    if (
bChatText && !bCenterText)
    {
        
PrintToChatAll("[SM] End of round friendly fire has been enabled.");
    }
    else if (
bCenterText && !bChatText)
    {
        
PrintCenterTextAll("End of round friendly fire has been enabled.");
    }
    else if (
bChatText && bCenterText)
    {
        
PrintCenterTextAll("End of round friendly fire has been enabled.");
        
PrintToChatAll("[SM] End of round friendly fire has been enabled.");
    }
    
    return 
Plugin_Continue;


public 
Action Event_RoundStart(Event event, const char[] namebool dontBroadcast

    
FindConVar("mp_friendlyfire").SetInt(0);
    
    if (
bChatText && !bCenterText
    { 
        
PrintToChatAll("[SM] End of round friendly fire has been disabled."); 
    } 
    else if (
bCenterText && !bChatText
    { 
        
PrintCenterTextAll("End of round friendly fire has been disabled."); 
    } 
    else if (
bChatText && bCenterText
    { 
        
PrintCenterTextAll("End of round friendly fire has been disabled."); 
        
PrintToChatAll("[SM] End of round friendly fire has been disabled."); 
    }
    
    return 
Plugin_Continue;

cravenge is offline
ThatKidWhoGames
Veteran Member
Join Date: Jun 2013
Location: IsValidClient()
Old 09-03-2017 , 18:56   Re: ConVar changes not working
Reply With Quote #5

Quote:
Originally Posted by cravenge View Post
Cleaned up the code and got rid of the old syntax:
PHP Code:
#pragma semicolon 1
#include <sourcemod>

#define PLUGIN_AUTHOR "Sgt. Gremulock"
#define PLUGIN_VERSION "1.0.2"

ConVar cv_ChatTextcv_CenterText
bool bChatTextbCenterText

public 
Plugin myinfo =  

    
name "[TF2] End Round Friendly Fire"
    
author PLUGIN_AUTHOR
    
description "Enable's friendly fire at the end of a round and disables it on the start of the next round."
    
version PLUGIN_VERSION
    
url "grem-co.com" 
}; 

public 
void OnPluginStart()
{
    
CreateConVar("sm_endroundff_version"PLUGIN_VERSION"Plugin's version."FCVAR_REPLICATED|FCVAR_NOTIFY);
    
cv_ChatText CreateConVar("sm_endroundff_chat""1""Announce ff status in chat?"_true0.0true1.0);
    
cv_CenterText CreateConVar("sm_endroundff_center""1""Announce ff status in center text?"_true0.0true1.0);
    
    
bChatText cv_ChatText.BoolValue;
    
bCenterText cv_CenterText.BoolValue;

    
cv_ChatText.AddChangeHook(ConVar_ChatText);
    
cv_CenterText.AddChangeHook(ConVar_CenterText);
    
    
AutoExecConfig(true"endroundff""sourcemod");
    
    
HookEvent("teamplay_round_win"Event_RoundWin);
    
HookEvent("teamplay_round_start"Event_RoundStart);


public 
void ConVar_ChatText(ConVar cvar, const char[] sOldValue, const char[] sNewValue)
{
    
bChatText cv_ChatText.BoolValue;
}

public 
void ConVar_CenterText(ConVar cvar, const char[] sOldValue, const char[] sNewValue)
{
    
bCenterText cv_CenterText.BoolValue;
}

public 
Action Event_RoundWin(Event event, const char[] namebool dontBroadcast

    
FindConVar("mp_friendlyfire").SetInt(1);
    
    if (
bChatText && !bCenterText)
    {
        
PrintToChatAll("[SM] End of round friendly fire has been enabled.");
    }
    else if (
bCenterText && !bChatText)
    {
        
PrintCenterTextAll("End of round friendly fire has been enabled.");
    }
    else if (
bChatText && bCenterText)
    {
        
PrintCenterTextAll("End of round friendly fire has been enabled.");
        
PrintToChatAll("[SM] End of round friendly fire has been enabled.");
    }
    
    return 
Plugin_Continue;


public 
Action Event_RoundStart(Event event, const char[] namebool dontBroadcast

    
FindConVar("mp_friendlyfire").SetInt(0);
    
    if (
bChatText && !bCenterText
    { 
        
PrintToChatAll("[SM] End of round friendly fire has been disabled."); 
    } 
    else if (
bCenterText && !bChatText
    { 
        
PrintCenterTextAll("End of round friendly fire has been disabled."); 
    } 
    else if (
bChatText && bCenterText
    { 
        
PrintCenterTextAll("End of round friendly fire has been disabled."); 
        
PrintToChatAll("[SM] End of round friendly fire has been disabled."); 
    }
    
    return 
Plugin_Continue;


Thank you lad!!
ThatKidWhoGames is offline
Reply



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 09:57.


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