create / use cvars in QC
Moderator: InsideQC Admins
5 posts
• Page 1 of 1
create / use cvars in QC
Hello,
I know that basically it is NOT possible to create a cvar via QC (that is normally an engine thing).
Fortunately it is possible to use the free cvar “temp1” for qc mods.
Trickle´s Dodgeball uses it too, as I just saw.
But then, during studying DarkPlaces dpextensions.qc, I found this little beauty by divVerent:
Unfortunately my English is not good enough to fully understand everything.
But as far as I understand, it is possible to use values, that are specified by the new cvar in config.cfg, in QC mods.
You “only” have to specify the new cvar name in divVerent´s idea / implementation.
And add the new cvar and its value into config.cfg.
Is this correct ? Or is it a misunderstanding of what “DP_QC_AUTOCVARS” does ?
I tried to use it, but could not.
Did anyone used this before ? Has experience with it ?
What would I have to do to get a new cvar, lets say: “inside3d” and use its specified value (in config.cfg) in my QC mod ?
I am currently developing my small mod and want to use a cvar to do something like:
My above example works with “temp1” perfectly ! But an additional cvar would always be nice to be more constructive, wouldn’t it ?
I remember, Spike saying one time in this forum, that there are already more than “temp1” cvar, that modders can use.
Spike mentioned: “scratched*” and “saved*”
But none of them work like “temp1” does. Darkplaces doesn’t know them.
Thank you very much for your help on this topic.
Kind regards,
Seven
PS: Another idea, that just popped into my mind while writing this is:
Misuse an already existing cvar in the QC mod, which is normally not used by the majority of people,
like special joystick cvars for example: "joyadvaxisv"
This would be a workaround to have more cvars to be used in QC mods.
.
I know that basically it is NOT possible to create a cvar via QC (that is normally an engine thing).
Fortunately it is possible to use the free cvar “temp1” for qc mods.
Trickle´s Dodgeball uses it too, as I just saw.
But then, during studying DarkPlaces dpextensions.qc, I found this little beauty by divVerent:
- Code: Select all
//DP_QC_AUTOCVARS
//idea: divVerent
//darkplaces implementation: divVerent
//description:
//allows QC variables to be bound to cvars
//(works for float, string, vector types)
//example:
// float autocvar_developer;
// float autocvar_registered;
// string autocvar__cl_name;
//NOTE: copying a string-typed autocvar to another variable/field, and then
//changing the cvar or returning from progs is UNDEFINED. Writing to autocvar
//globals is UNDEFINED. Accessing autocvar globals after cvar_set()ing that
//cvar is IMPLEMENTATION DEFINED (an implementation may either yield the
//previous, or the current, value). Whether autocvar globals, after restoring
//a savegame, have the cvar's current value, or the original value at time of
//saving, is UNDEFINED. Restoring a savegame however must not restore the
//cvar values themselves.
//In case the cvar does NOT exist, then it is automatically created with the
//value of the autocvar initializer, if given. This is possible with e.g.
//frikqcc and fteqcc the following way:
// var float autocvar_whatever = 42;
//If no initializer is given, the cvar will be initialized to a string
//equivalent to the NULL value of the given data type, that is, the empty
//string, 0, or '0 0 0'. However, when automatic cvar creation took place, a
//warning is printed to the game console.
Unfortunately my English is not good enough to fully understand everything.
But as far as I understand, it is possible to use values, that are specified by the new cvar in config.cfg, in QC mods.
You “only” have to specify the new cvar name in divVerent´s idea / implementation.
And add the new cvar and its value into config.cfg.
Is this correct ? Or is it a misunderstanding of what “DP_QC_AUTOCVARS” does ?
I tried to use it, but could not.
Did anyone used this before ? Has experience with it ?
What would I have to do to get a new cvar, lets say: “inside3d” and use its specified value (in config.cfg) in my QC mod ?
I am currently developing my small mod and want to use a cvar to do something like:
- Code: Select all
If (cvar(“inside3d”) == 2)
Do this…
else if (cvar(“inside3d”) == 3)
Do that…
else if (cvar(“inside3d”) == 4)
Do something else…
else if (cvar(“inside3d”) == 5)
Do nothing…
My above example works with “temp1” perfectly ! But an additional cvar would always be nice to be more constructive, wouldn’t it ?
I remember, Spike saying one time in this forum, that there are already more than “temp1” cvar, that modders can use.
Spike mentioned: “scratched*” and “saved*”
But none of them work like “temp1” does. Darkplaces doesn’t know them.
Thank you very much for your help on this topic.
Kind regards,
Seven
PS: Another idea, that just popped into my mind while writing this is:
Misuse an already existing cvar in the QC mod, which is normally not used by the majority of people,
like special joystick cvars for example: "joyadvaxisv"
This would be a workaround to have more cvars to be used in QC mods.
.
- Seven
- Posts: 301
- Joined: Sat Oct 06, 2007 8:49 pm
- Location: Germany
couple of things to note:
1. the cvar() builtin has a fairly high overhead, depending on engine. in most engines, it will be slower than a find() or a traceline().
you don't want to call it multiple times on the same cvar as you did in your example.
2. autocvars can be used as such:
var float autocvar_mycvarname = 5;
when the mod is loaded, the engine will create a 'mycvarname' with a value "5" if it didn't already exist.
(if you have no =, you don't need the 'var', which is a frikqcc extension (also works in fteqcc). earlier qccs may have issue, as would frikqcc/fteqcc if the '=' is present without the 'var', in other words, 'float autocvar_mycvarname;' will work in any qcc, but its default value will be 0.)
You can then 'directly' read the cvar by acessing autocvar_mycvarname. Note however that you cannot change the cvar by assigning to the autocvar - you still need to use builtins for that, but the autocvar will be updated before the next frame. You should generally also avoid changing such cvars too often.
you can use string types instead if you want.
3. for greater portability, the 'set' console command (localcmd it) will create a cvar. this is useful to know even with autocvars, for configs and things. more engines support the 'set' command than autocvars.
you can typically check to see if a cvar exists by using cvar_string("foo") and checking against "". if its empty, it doesn't exist, at least if its meant to only hold numeric values.
1. the cvar() builtin has a fairly high overhead, depending on engine. in most engines, it will be slower than a find() or a traceline().
you don't want to call it multiple times on the same cvar as you did in your example.
2. autocvars can be used as such:
var float autocvar_mycvarname = 5;
when the mod is loaded, the engine will create a 'mycvarname' with a value "5" if it didn't already exist.
(if you have no =, you don't need the 'var', which is a frikqcc extension (also works in fteqcc). earlier qccs may have issue, as would frikqcc/fteqcc if the '=' is present without the 'var', in other words, 'float autocvar_mycvarname;' will work in any qcc, but its default value will be 0.)
You can then 'directly' read the cvar by acessing autocvar_mycvarname. Note however that you cannot change the cvar by assigning to the autocvar - you still need to use builtins for that, but the autocvar will be updated before the next frame. You should generally also avoid changing such cvars too often.
you can use string types instead if you want.
3. for greater portability, the 'set' console command (localcmd it) will create a cvar. this is useful to know even with autocvars, for configs and things. more engines support the 'set' command than autocvars.
you can typically check to see if a cvar exists by using cvar_string("foo") and checking against "". if its empty, it doesn't exist, at least if its meant to only hold numeric values.
- Spike
- Posts: 2892
- Joined: Fri Nov 05, 2004 3:12 am
- Location: UK
Thank you very much Spike.
Unfortunately I have one last question, because I still didnt fully understand:
My task is to implement the users desire/wish into my mod.
So that the mod is flexible to fulfill the users request/wish.
And therefore I must be able to read the custom cvar out of the config.cfg in my mod.
Example:
User adds the line into config.cfg (or autoexec if necessary):
"inside3d" "5"
(or whatever value the player wants. "5" means dodgeball color red for example).
I must tell the users of course in my mods readme.txt to write the line into his config.cfg and what the different values mean.
I understood that in the qc mod I would have to add this line inside the function where I want to use the "inside3d" cvar:
float autocvar_mycvarname;
This would only tell the engine that a cvar "inside3d" exists, but the value is still "0" (as you described in your post).
Now I can read the REAL "inside3d" value (which the user has written in the config.cfg) with:
Or can it be done simple as this ? :
This will set for example the color for the dodgeball.
Then I know what the mod user wants and can work with the value I now know in my mod, right ?
Thank you in advance for your short reply and confirmation that I can do it the way I described above.
I use your great fteqcc by the way.
Regards,
Seven
Unfortunately I have one last question, because I still didnt fully understand:
My task is to implement the users desire/wish into my mod.
So that the mod is flexible to fulfill the users request/wish.
And therefore I must be able to read the custom cvar out of the config.cfg in my mod.
Example:
User adds the line into config.cfg (or autoexec if necessary):
"inside3d" "5"
(or whatever value the player wants. "5" means dodgeball color red for example).
I must tell the users of course in my mods readme.txt to write the line into his config.cfg and what the different values mean.
I understood that in the qc mod I would have to add this line inside the function where I want to use the "inside3d" cvar:
float autocvar_mycvarname;
This would only tell the engine that a cvar "inside3d" exists, but the value is still "0" (as you described in your post).
Now I can read the REAL "inside3d" value (which the user has written in the config.cfg) with:
- Code: Select all
If (cvar(“inside3d”) == 2)
Do this…
else if (cvar(“inside3d”) == 3)
Do that…
else if (cvar(“inside3d”) == 4)
....
Or can it be done simple as this ? :
- Code: Select all
b = cvar(“inside3d”);
Then make the if....else decision with "b" directly
This will set for example the color for the dodgeball.
Then I know what the mod user wants and can work with the value I now know in my mod, right ?
Thank you in advance for your short reply and confirmation that I can do it the way I described above.
I use your great fteqcc by the way.
Regards,
Seven
- Seven
- Posts: 301
- Joined: Sat Oct 06, 2007 8:49 pm
- Location: Germany
Misuse an already existing cvar in the QC mod, which is normally not used by the majority of people,
You really don't want to do this. 10 years ago something like vid_mode would have been a candidate; today - use it at your peril. You never really know what's going to be needed or used tomorrow. Specific example - I had someone complain to me when I removed the joystick code from my engine a short time (month or two) ago. People still like to play Quake with controllers, and the joystick code supports them.
I've also seen mods stuffcmd cvar values that are only used in software engines, so they're not safe to touch either. Beyond Belief stuffcmds r_maxsurfs and r_maxedges when you enter a map, for example.
temp/saved/scratch cvars are provided by ID for precisely this kind of purpose, and if DP doesn't support them then it really should, otherwise it risks breaking on legacy mods that use them.
We had the power, we had the space, we had a sense of time and place
We knew the words, we knew the score, we knew what we were fighting for
We knew the words, we knew the score, we knew what we were fighting for
-

mh - Posts: 2292
- Joined: Sat Jan 12, 2008 1:38 am
you want to make a cvar named 'inside3d', right, fine...
config.cfg (to set it to 5 whether it exists or not):
set inside3d 5
qc definition (to give default 5):
var autocvar_inside3d = 5;
qc usage:
note that the config needs 'set', because the engine doesn't know about the cvar until it is either created with 'set', or created by the presence of an autocvar, but autocvars are only checked when a map is started, which is after configs are execed, hence why the config needs 'set'.
if you need to generate names for cvars on the fly, you can't use autocvars. note that the following qc code still works to create a cvar:
obviously, the config still needs to use set as qc is only loaded/run AFTER configs are execed.
You should read the description of the DP_CON_SET extension.
config.cfg (to set it to 5 whether it exists or not):
set inside3d 5
qc definition (to give default 5):
var autocvar_inside3d = 5;
qc usage:
- Code: Select all
if (autocvar_inside3d == 2)
dosomething();
else if (autocvar_inside3d == 3)
dosomethingelse();
else if (autocvar_inside3d == 4)
dosomethingelseotherthanthat();
note that the config needs 'set', because the engine doesn't know about the cvar until it is either created with 'set', or created by the presence of an autocvar, but autocvars are only checked when a map is started, which is after configs are execed, hence why the config needs 'set'.
if you need to generate names for cvars on the fly, you can't use autocvars. note that the following qc code still works to create a cvar:
- Code: Select all
if (cvar_string("inside3d") == "")
localcmd("set inside3d 0\n");
obviously, the config still needs to use set as qc is only loaded/run AFTER configs are execed.
You should read the description of the DP_CON_SET extension.
- Spike
- Posts: 2892
- Joined: Fri Nov 05, 2004 3:12 am
- Location: UK
5 posts
• Page 1 of 1
Who is online
Users browsing this forum: No registered users and 1 guest