Raised This Month: $ Target: $400
 0% 

Please help with a custom ConVar


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
lake393
AlliedModders Donor
Join Date: Oct 2008
Old 02-11-2011 , 19:25   Please help with a custom ConVar
Reply With Quote #1

Hello, I've tried looking at the docs and the wiki, as well as a number of searches, and I still have no idea what I'm doing wrong.

I want to make a custom convar called myplugin_verbose_logging, that my plugin will be able to read.

I have the following code:
Code:
new g_iVerboseMode = 0;
public OnConfigsExecuted()
{
    new Handle:hCreateVerboseConvar = CreateConVar("myplugin_verbose_logging", "0", "Determines whether to use verbose logging");
    AutoExecConfig(true, "myplugin");
    if(hCreateVerboseConvar != INVALID_HANDLE)
    {
        g_iVerboseMode = GetConVarInt(FindConVar("myplugin_verbose_logging"));
        LogMessage("Verbose mode: %i", g_iVerboseMode);
    }
    else
    {
        LogMessage("Error creating verbose mode convar");
    }
}
I restarted the server and look in /orangebox/cstrike/cfg/sourcemod/ and myplugin.cfg is nowhere to be found, which I believe contradicts the true in the first argument (bool:autoCreate) of AutoExecConfig(), according to the docs page here.

So, I figured that perhaps AutoExecConfig() failed to create the file for some reason, so I created it myself (/orangebox/cstrike/cfg/sourcemod/myplugin.cfg) with the following contents:
Code:
myplugin_verbose_logging "1"
I restarted the server and now look in the sourcemod logs in /orangebox/cstrike/addons/sourcemod/logs/ and read what was outputted using LogMessage() in the above code. It says: Verbose mode: 0

There are no errors at all. Its just not reading that file as far as I can tell. It could be because I'm doing it wrong, so if someone could help me make it right, I would appreciate it greatly.

Thank you.
lake393 is offline
Cr(+)sshair
Member
Join Date: Mar 2010
Old 02-11-2011 , 22:05   Re: Please help with a custom ConVar
Reply With Quote #2

Code:
new g_iVerboseMode = 0;
new Handle:hCreateVerboseConvar = INVALID_HANDLE;

public OnPluginStart()
{
    hCreateVerboseConvar = CreateConVar("myplugin_verbose_logging", "0", "Determines whether to use verbose logging");
    AutoExecConfig(true, "myplugin");
    
    if(hCreateVerboseConvar != INVALID_HANDLE)
    {
        g_iVerboseMode = GetConVarInt(hCreateVerboseConvar);
        LogMessage("Verbose mode: %i", g_iVerboseMode);
    }
    else
    {
        SetFailState("Error creating verbose mode convar");
    }
}

Last edited by Cr(+)sshair; 02-11-2011 at 22:34.
Cr(+)sshair is offline
Silvers
SourceMod Plugin Approver
Join Date: Aug 2010
Location: SpaceX
Old 02-11-2011 , 22:28   Re: Please help with a custom ConVar
Reply With Quote #3

PHP Code:
 if(hCreateVerboseConvar != INVALID_HANDLE)
    {
        
g_iVerboseMode GetConVarInt(hCreateVerboseConvar);
    } 
No need to FindConVar since you already get the handle of the ConVar when creating it.
__________________
Silvers is offline
Cr(+)sshair
Member
Join Date: Mar 2010
Old 02-11-2011 , 22:30   Re: Please help with a custom ConVar
Reply With Quote #4

Quote:
Originally Posted by Silvers View Post
PHP Code:
 if(hCreateVerboseConvar != INVALID_HANDLE)
    {
        
g_iVerboseMode GetConVarInt(hCreateVerboseConvar);
    } 
No need to FindConVar since you already get the handle of the ConVar when creating it.
Yep!
Cr(+)sshair is offline
Silvers
SourceMod Plugin Approver
Join Date: Aug 2010
Location: SpaceX
Old 02-11-2011 , 22:33   Re: Please help with a custom ConVar
Reply With Quote #5

I would also recommend moving that code to OnPluginStart (but maybe thats fine there I dunno, majority put into OnPluginStart).

To be honest, I've hardly seen anyone check that the convar is created so I assume all of that is pointless. I've never seen a plugin fail to create a convar.
__________________
Silvers is offline
Cr(+)sshair
Member
Join Date: Mar 2010
Old 02-11-2011 , 22:35   Re: Please help with a custom ConVar
Reply With Quote #6

Quote:
Originally Posted by Silvers View Post
I would also recommend moving that code to OnPluginStart (but maybe thats fine there I dunno, majority put into OnPluginStart).

To be honest, I've hardly seen anyone check that the convar is created so I assume all of that is pointless. I've never seen a plugin fail to create a convar.
I totally overlooked that, I've been programming all day and I could have sworn it said OnPluginStart instead of OnConfigs. Wow, really tired. Thanks for pointing that out! Changed that in my post, BRB more coffee =P

Last edited by Cr(+)sshair; 02-11-2011 at 22:38.
Cr(+)sshair is offline
lake393
AlliedModders Donor
Join Date: Oct 2008
Old 02-12-2011 , 00:29   Re: Please help with a custom ConVar
Reply With Quote #7

Thanks for replying guys. Other than moving it from OnConfigsExecuted() to OnPluginStart(), I don't really see any difference from the original code though.

Quote:
Originally Posted by Silvers View Post
No need to FindConVar since you already get the handle of the ConVar when creating it.
Understood. I'll implement this.

One thing I just noticed... it WORKS when I do sm plugins myplugin reload, but when I restart the server, it doesn't work.
lake393 is offline
lake393
AlliedModders Donor
Join Date: Oct 2008
Old 02-12-2011 , 00:47   Re: Please help with a custom ConVar
Reply With Quote #8

Aaahaa!

The CreateConVar() and AutoExecConfig() parts have to go inside OnPluginStart()

The reading of the convar has to go inside OnConfigsExecuted()

My updated code. Thanks to you both for your replies! I changed my code to implement ideas from both of you.

PHP Code:
// ConVars.
new Handle:g_hCvarVerbose INVALID_HANDLE;
new 
bool:g_bCvarVerbose false;

public 
OnPluginStart()
{
    
// Create ConVars.
    
g_hCvarVerbose CreateConVar("myplugin_verbose_logging""0""Determines whether to use verbose logging");
    
AutoExecConfig(true"myplugin");
}

public 
OnConfigsExecuted()
{
    
// Read ConVars.
    
g_bCvarVerbose GetConVarBool(g_hCvarVerbose);
    
LogMessage("Verbose mode: %b"g_bCvarVerbose);

lake393 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 01:54.


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