代码鉴赏
- 下面的代码展示了如何创建ConVar,并且设定其初始值,然后获取其值的流程
#pragma newdecls required
ConVar g_cvTestValue1;
ConVar g_cvTestValue2;
ConVar g_cvTestValue3;
ConVar g_cvTestValue4;
ConVar g_cvTestValue5;
public void OnPluginStart()
{
g_cvTestValue1 = CreateConVar("sm_testvalue1", "0", "Test Value");
g_cvTestValue2 = CreateConVar("sm_testvalue2", "1", "Test Value");
g_cvTestValue3 = CreateConVar("sm_testvalue3", "2", "Test Value");
g_cvTestValue4 = CreateConVar("sm_testvalue4", "3", "Test Value");
g_cvTestValue5 = CreateConVar("sm_testvalue5", "4", "Test Value");
}
public void OnMapStart()
{
g_cvTestValue1.SetString("1");
g_cvTestValue2.SetString("2");
g_cvTestValue3.SetString("3");
g_cvTestValue4.SetString("4");
g_cvTestValue5.SetString("5");
char TestValue1[256];
char TestValue2[256];
char TestValue3[256];
char TestValue4[256];
char TestValue5[256];
g_cvTestValue1.GetString(TestValue1, sizeof(TestValue1));
g_cvTestValue2.GetString(TestValue2, sizeof(TestValue2));
g_cvTestValue3.GetString(TestValue3, sizeof(TestValue3));
g_cvTestValue4.GetString(TestValue4, sizeof(TestValue4));
g_cvTestValue5.GetString(TestValue5, sizeof(TestValue5));
PrintToServer("======================================");
PrintToServer("ConVar.GetString");
PrintToServer("g_cvTestValue1 = %s", TestValue1);
PrintToServer("g_cvTestValue2 = %s", TestValue2);
PrintToServer("g_cvTestValue3 = %s", TestValue3);
PrintToServer("g_cvTestValue4 = %s", TestValue4);
PrintToServer("g_cvTestValue5 = %s", TestValue5);
PrintToServer("======================================");
PrintToServer(" ");
PrintToServer("======================================");
PrintToServer("ConVar.IntValue");
PrintToServer("g_cvTestValue1 = %i", g_cvTestValue1.IntValue);
PrintToServer("g_cvTestValue2 = %i", g_cvTestValue2.IntValue);
PrintToServer("g_cvTestValue3 = %i", g_cvTestValue3.IntValue);
PrintToServer("g_cvTestValue4 = %i", g_cvTestValue4.IntValue);
PrintToServer("g_cvTestValue5 = %i", g_cvTestValue5.IntValue);
PrintToServer("======================================");
PrintToServer(" ");
PrintToServer("======================================");
PrintToServer("ConVar.FloatValue");
PrintToServer("g_cvTestValue1 = %0.2f", g_cvTestValue1.FloatValue);
PrintToServer("g_cvTestValue2 = %0.2f", g_cvTestValue2.FloatValue);
PrintToServer("g_cvTestValue3 = %0.2f", g_cvTestValue3.FloatValue);
PrintToServer("g_cvTestValue4 = %0.2f", g_cvTestValue4.FloatValue);
PrintToServer("g_cvTestValue5 = %0.2f", g_cvTestValue5.FloatValue);
PrintToServer("======================================");
}
参考: