ConVar: Difference between revisions
No edit summary |
SirYodaJedi (talk | contribs) (→Good Practices: fcvar_cheat doesn't always work; this is abused in portal speedruns for airboat%) |
||
(24 intermediate revisions by 8 users not shown) | |||
Line 1: | Line 1: | ||
{{ | {{LanguageBar}} | ||
{{src topicon}} | |||
}} | |||
{{stub}} | {{stub}} | ||
'''ConVar''' is the C++ class used to create a [[console variable]] (frequently shortened to "cvar"), which is used in the [[Developer Console]]. | |||
Documentation on specific console variables can be found at [[:Category:Console | Documentation on specific console variables can be found at [[:Category:Console variables]]. | ||
For creating a new Console Variable see this. [[Developer_Console_Control#Using_the_ConVar_class|Developer Console Control]] | For creating a new Console Variable see this. [[Developer_Console_Control#Using_the_ConVar_class|Developer Console Control]] | ||
<!-- only info on console variables _in general_ should be on this page --> | <!-- only info on console variables _in general_ should be on this page --> | ||
ConVar uses the constructor shown below: | |||
ConVar( const char *pName, const char *pDefaultValue, int flags, const char *pHelpString, bool bMin, float fMin, bool bMax, float fMax ); | {{code|highlight=cpp|ConVar( const char *pName, const char *pDefaultValue, int flags, const char *pHelpString, bool bMin, float fMin, bool bMax, float fMax );}} | ||
The default value given as the second argument of the ConVar constructor is '''not''' the value set. To set a value, use: | The default value given as the second argument of the ConVar constructor is '''not''' the value set. To set a value, use: | ||
* | {{code|highlight=cpp|pName->SetValue([value])}}; | ||
== Good Practices == | |||
* Variables that you wish to be saved should be marked with flag [[FCVAR_ARCHIVE]]. These will be saved with the user bindings in <code>config.cfg</code>. Any cheat cvar should not be archived. | |||
* | * After whatever flags you may want to add (or 0 if you don't want any), you can set a help string. This will display when someone enters in the ConVar without any value or if they enter an invalid value. You can use {{code|\n}} to make a new line and use spaces after that to indent lines. Make your help-strings as rich and detailed as you want! | ||
* Use the flag FCVAR_REPLICATED to signify a server variable which is then ''replicated'' onto clients. For example, round timers or game rules should have this flag. | * Use the flag [[FCVAR_CHEAT]] to signify a ConVar that can only be used when sv_cheats is turned on. | ||
:{{bug|Some games will ignore this flag in singleplayer.}} | |||
* Use the flag [[FCVAR_REPLICATED]] to signify a server variable which is then ''replicated'' onto clients. For example, round timers or game rules should have this flag. | |||
* For multiplayer games, use FCVAR_NOTIFY to send a message in chat whenever this variable is changed. Use this for ConVars that change game mechanics. | * For multiplayer games, use [[FCVAR_NOTIFY]] to send a message in chat whenever this variable is changed. Use this for ConVars that change game mechanics. | ||
For more tips using ConVar flags, see [[Developer Console Control#The FCVAR flags|this page]]. | For more tips using ConVar flags, see [[Developer Console Control#The FCVAR flags|this page]]. | ||
== Examples == | |||
The code below will define a cvar named <code>cl_test</code>: | |||
{{code|highlight=cpp|ConVar cl_test("cl_test", 0, 0, "Example ConVar");}} | |||
Whats happening here is first, we are declaring the variable to be a <code>ConVar</code>, then we are setting the name of the cvar that would be used in games console, | |||
and after that, we set the default value, then flags, and finally, the help text for the console. | |||
We can use use these cvars to initialize other variables, like if you wanted to create a integer named test you could do as so | |||
{{code|highlight=cpp|int test {{=}} cl_test.GetInt();}} | |||
You could also have it be a float, and call <code>GetFloat()</code> | |||
Finally, don't forget to include <code>convar.h</code> in your file | |||
== See also == | == See also == | ||
[[Developer Console Control]] | [[Developer Console Control]] | ||
[[Category:Classes]] | |||
[[Category: |
Latest revision as of 16:22, 4 June 2025
ConVar is the C++ class used to create a console variable (frequently shortened to "cvar"), which is used in the Developer Console.
Documentation on specific console variables can be found at Category:Console variables. For creating a new Console Variable see this. Developer Console Control
ConVar uses the constructor shown below:
ConVar( const char *pName, const char *pDefaultValue, int flags, const char *pHelpString, bool bMin, float fMin, bool bMax, float fMax );
The default value given as the second argument of the ConVar constructor is not the value set. To set a value, use:
pName->SetValue([value])
;
Good Practices
- Variables that you wish to be saved should be marked with flag FCVAR_ARCHIVE. These will be saved with the user bindings in
config.cfg
. Any cheat cvar should not be archived.
- After whatever flags you may want to add (or 0 if you don't want any), you can set a help string. This will display when someone enters in the ConVar without any value or if they enter an invalid value. You can use \n to make a new line and use spaces after that to indent lines. Make your help-strings as rich and detailed as you want!
- Use the flag FCVAR_CHEAT to signify a ConVar that can only be used when sv_cheats is turned on.
Bug:Some games will ignore this flag in singleplayer. [todo tested in ?]
- Use the flag FCVAR_REPLICATED to signify a server variable which is then replicated onto clients. For example, round timers or game rules should have this flag.
- For multiplayer games, use FCVAR_NOTIFY to send a message in chat whenever this variable is changed. Use this for ConVars that change game mechanics.
For more tips using ConVar flags, see this page.
Examples
The code below will define a cvar named cl_test
:
ConVar cl_test("cl_test", 0, 0, "Example ConVar");
Whats happening here is first, we are declaring the variable to be a ConVar
, then we are setting the name of the cvar that would be used in games console,
and after that, we set the default value, then flags, and finally, the help text for the console.
We can use use these cvars to initialize other variables, like if you wanted to create a integer named test you could do as so
int test = cl_test.GetInt();
You could also have it be a float, and call GetFloat()
Finally, don't forget to include convar.h
in your file