sourcemod
sourcemod copied to clipboard
Cannot change convars in OnConfigsExecuted
Environment
- Operating System version: linux
- Game/AppID (with version if applicable): csgo
- Current SourceMod version: SM 1.10
- Current SourceMod snapshot: 6536
- Current Metamod: Source snapshot: 1145
Description
Sometimes, when I change plugin convars in OnConfigsExecuted (and the plugin has a config created and executed with AutoExecConfig), the convars doesnt't change, and their values are the ones from AutoExecConfig config file. I change convars here based on map config files, thats why I do it like in the code example.
Problematic Code (or Steps to Reproduce)
#pragma semicolon 1
#include <sourcemod>
ConVar g_Cvar_TestConfigs;
public OnPluginStart()
{
g_Cvar_TestConfigs = CreateConVar("sm_test_configs", "1", "", FCVAR_NONE, true, 0.0);
AutoExecConfig(true, "testconfigs");
}
public void OnConfigsExecuted()
{
SetConVar("sm_test_configs", "0");
RequestFrame(Frame_OnConfigsExecuted);
}
void Frame_OnConfigsExecuted()
{
if (g_Cvar_TestConfigs.IntValue)
{
LogError("sm_test_configs is 1");
}
}
void SetConVar(const char[] cvarName, const char[] value)
{
ConVar cvar = FindConVar(cvarName);
if (cvar)
{
cvar.SetString(value);
}
}
Logs
L 04/10/2022 - 13:49:37: [testconfigs.smx] sm_test_configs is 1
I tested, on Windows, CSGO game.
ConVar.SetString() will change value last.
"sm_test_configs" = "0" ( def. "1" ) min. 0.000000
Metamod:Source version 1.11.0-dev+1145
SourceMod Version: 1.10.0.6528
#pragma semicolon 1
#include <sourcemod>
ConVar g_Cvar_TestConfigs;
public OnPluginStart()
{
g_Cvar_TestConfigs = CreateConVar("sm_test_configs", "1", "", FCVAR_NONE, true, 0.0);
HookConVarChange(g_Cvar_TestConfigs, convarchange);
AutoExecConfig(true, "testconfigs");
}
public void convarchange(ConVar convar, const char[] oldValue, const char[] newValue)
{
PrintToServer("convarchange %s", newValue);
}
public void OnConfigsExecuted()
{
SetConVar("sm_test_configs", "0");
RequestFrame(Frame_OnConfigsExecuted);
}
void Frame_OnConfigsExecuted()
{
if (g_Cvar_TestConfigs.IntValue)
{
LogError("sm_test_configs is 1");
}
}
void SetConVar(const char[] cvarName, const char[] value)
{
ConVar cvar = FindConVar(cvarName);
if (cvar)
{
cvar.SetString(value);
}
}
The bug is happening sometimes, not every time.
After all, changing cvar from code and from config file this way, it not look brilliant way to do it. I can't think a situation why you need to do this way.
I change convars based on current map, so I have to use FindConVar.