Scripting: Custom Cvars

Tutorials for Call of Duty 2 mapping

Moderator: Core Staff

Post Reply
User avatar
Drofder2004
Core Staff
Core Staff
Posts: 13313
Joined: April 13th, 2005, 8:22 pm
Location: UK, London

Scripting: Custom Cvars

Post by Drofder2004 » August 1st, 2007, 12:57 am

Someone mentioned they did not know how to do custom cvars, instead of answering in a post, here is a quick scripting tutorial...

Code: Select all

if(getcvar("<cvar>") == "")
   setcvar("<cvar>", "<default>");
Code above in pseudocode...
"If cvar <cvar> is equal to nothing (if it doesn't exist)...
...create cvar with default value"

Thats the bare basics.

Next you can create minimum and maximum values (if the cvar is numeric...

Code: Select all

if(getCvar("<cvar>") == "")
   setCvar("<cvar>", "<default>");
else if(getCvarInt("<cvar>") > 10)
   setCvar("<cvar>", 10);
else if(getCvarInt("<cvar>") < 0)
   setCvar("<cvar>", 0);
Again, pseudocode, starting at line 3 (continued from above).

...else, if the cvar is greater than 10, set the cvar to 10, or
...if the cvar is less than 0, set it to 0.

Make sure you get you note the usage of getCvar and getCvarInt (also, you can use getCvarFloat, to retrieve decimal values).

And finally, using the cvar in your script...
Now, there are many ways to use the cvar, but the main way is to use it as a variable...

Code: Select all

if(getCvar("<cvar>") == "")
   setCvar("<cvar>", "<default>");
else if(getCvarInt("<cvar>") > 10)
   setCvar("<cvar>", 10);
else if(getCvarInt("<cvar>") < 0)
   setCvar("<cvar>", 0);
<variable> = getCvarInt("<cvar>");
Remeber, use the correct 'getCvar', use...

getCvar for TEXT based cvars (example: getCvar("g_gametype"); )
getCvarInt for INTEGERS (whole number, example: getCvarInt("scr_forcerespawn"); )
getCvarInt for FLOATS (decimals, example: getCvarFloat("scr_hq_timelimit"); )

Another good use, is to use the cvar as a wait command...
For example,

Code: Select all

main()
{
   while(getCvar("wait") != 0)
   {
      wait 1;
   }

   iprintln("Wait cvar is equal to 0");
}
The "while()" in the above script will keep waiting 1 second, until the cvar is changed to 0, once changed, the main function will continue.

Now you know cvars :)
Image
Virgin Media 20Mb Broadband:
"Perfect for families going online at the same time, downloading movies, online gaming and more."
Borked internet since: 22-07-2010

User avatar
waywaaaard
Core Staff
Core Staff
Posts: 2214
Joined: February 6th, 2006, 3:18 pm
Location: Germany/Bayern

Post by waywaaaard » August 1st, 2007, 1:35 am

mh I don't really understand that: I want to make a cvar for my function but I don't get it

Code: Select all

forcefps()
{
	while(1)
	{
	players = getentarray("player", "classname");
	for(i=0;i<players.size;i++)
	{
	players[i] setClientCvar("com_maxfps", 125);
	}
	wait .5;
	}
}
I want it to be set as default 125 and than u can choose between 0-1000
mh
THAT HANDS WERE NOT TRACED!
visit my blog: Link
Soviet wrote:Yeah, watch out, Peds will hit you with his +5 D-Battleaxe of homosexuality :roll:

User avatar
Drofder2004
Core Staff
Core Staff
Posts: 13313
Joined: April 13th, 2005, 8:22 pm
Location: UK, London

Post by Drofder2004 » August 3rd, 2007, 1:37 am

So you want to make a function where you can change the fps per player?
Ok, so firstly, lets look at your code and rewrite it to be compatible with a cvar...

Code: Select all

forcefps()
{
   while(1)
   {
   players = getentarray("player", "classname");
   for(i=0;i<players.size;i++)
   {
   players[i] setClientCvar("com_maxfps", 125);
   }
   wait .5;
   }
}
The part you want to change is the '125'. So we must change this to a 'variable'...

Code: Select all

players[i] setClientCvar("com_maxfps", level.fpscvar);
Ok, because of this cvar, we want to make sure the cvar is defined (otherwise we get errors).

Code: Select all

forcefps()
{
   if(!isDefined(level.fpscvar))
      level.fpscvar = 125;

   while(1)
   {
   players = getentarray("player", "classname");
   for(i=0;i<players.size;i++)
   {
   players[i] setClientCvar("com_maxfps", level.fpscvar);
   }
   wait .5;
   }
}
that will make sure when the thread is run, that the variable exists witha default value. Now we create a new function which will check the change of the cvar, we will use a combination of techniques and modify the 'wait' technique.

Code: Select all

cvarcheck()
{
   for(;;)
   {
      if(getCvar("lev_forcefps") == "")
         setCvar("lev_forcefps", 125);

      level.forcefps = getCvarInt("lev_forcefps");

      oldfps = getCvarInt("lev_forcefps");

      while(getCvarInt("lev_forcefps") == oldfps)
         wait 1;
   }
}
thats it... to help understand, again, pseudocode.
Forever [for(;;)]
Check if cvar [lev_forcefps] is equal to nothing
- if true, set the cvar to a default value of 125

Set the level variable [level.forcefps] to the current integer value of the cvar [lev_forcefps].

Set the local variable [oldfps] to the current integer value of the cvar [lev_forcefps].

While the cvar [lev_forcefps] is equal to the value of the local variable [oldfps]

Wait for 1 second.

[Repeat]
Try that, and report errors/problems or hopefully successes :)
Image
Virgin Media 20Mb Broadband:
"Perfect for families going online at the same time, downloading movies, online gaming and more."
Borked internet since: 22-07-2010

User avatar
Drofder2004
Core Staff
Core Staff
Posts: 13313
Joined: April 13th, 2005, 8:22 pm
Location: UK, London

Post by Drofder2004 » August 3rd, 2007, 6:03 am

KillerSam wrote:I love pseudocode.
When I read the code through first time - I took in most of it, but wasnt sure on other parts.
Read it with pseudocode and it was fully clear :)
All tutorials should come with pseudocode, it allows the learners to understand what the 'language' is saying in a language they understand. I try and read everything I create in pseudocode as it allows is good for spotting where something has gone wrong. :)
Image
Virgin Media 20Mb Broadband:
"Perfect for families going online at the same time, downloading movies, online gaming and more."
Borked internet since: 22-07-2010

User avatar
waywaaaard
Core Staff
Core Staff
Posts: 2214
Joined: February 6th, 2006, 3:18 pm
Location: Germany/Bayern

Post by waywaaaard » August 3rd, 2007, 1:58 pm

ok thx will have to read it more closer again laterz
THAT HANDS WERE NOT TRACED!
visit my blog: Link
Soviet wrote:Yeah, watch out, Peds will hit you with his +5 D-Battleaxe of homosexuality :roll:

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests