Call of Duty 4: Custom Dvars

From COD Modding & Mapping Wiki
Jump to navigation Jump to search

By Drofder2004

Someone mentioned they did not know how to do custom dvars, instead of answering in a post, here is a quick scripting tutorial
if(getdvar("<dvar>") == "")
   setdvar("<dvar>", "<default>");

(Code above in pseudocode)

"If dvar <dvar> is equal to nothing (if it doesn't exist)...
...create dvar with default value"

Thats the bare basics.

Next you can create minimum and maximum values (if the dvar is numeric)


if(getDvar("<dvar>") == "")
   setDvar("<dvar>", "<default>");

else if(getDvarInt("<dvar>") > 10)
   setDvar("<dvar>", 10);

else if(getDvarInt("<dvar>") < 0)
   setDvar("<dvar>", 0);


Again, pseudocode, starting at line 3 (continued from above).

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

Make sure you get you note the usage of getDvar and getDvarInt (also, you can use getDvarFloat, to retrieve decimal values).

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


if(getDvar("<dvar>") == "")
   setDvar("<dvar>", "<default>");

else if(getDvarInt("<dvar>") > 10)
   setDvar("<dvar>", 10);

else if(getDvarInt("<dvar>") < 0)
   setDvar("<dvar>", 0);

<variable> = getDvarInt("<dvar>");


Remeber, use the correct 'getDvar', use...

  • getDvar for TEXT based dvars (example: getDvar("g_gametype"); )
  • getDvarInt for INTEGERS (whole number, example: getDvarInt("scr_forcerespawn"); )
  • getDvarFloat for FLOATS (decimals, example: getDvarFloat("scr_hq_timelimit"); )


Another good use, is to use the dvar as a wait command

For example,

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

   iprintln("Wait dvar is equal to 0");
}


The while() in the above script will keep waiting 1 second, until the dvar is changed to 0, once changed, the main function will continue.

Now you know dvars!


Sources

CodJumper