This article's documentation is for anything that uses the Source engine. Click here for more information.

$definevariable

From Valve Developer Community
Jump to navigation Jump to search
English (en)Translate (Translate)

$definevariable is a QC command available in all Source Source games. It creates a Wikipedia icon variable. The variable's name, if surrounded by $, will be substituted by its value whenever encountered.

Note.pngNote:Variables do not work within "quote blocks"!
Note.pngNote:Variables declared out of the scope of a macro can't be used within the macro. You can pass the data on to the macro by instead using it as a token of the macro and retrieving it that way.
Note.pngNote:If you define the same variable twice, the compiler will use the first one it finds and drop the second one.
Icon-Bug.pngBug:Similar token prefixes causes the compiler parser to merge, swap, or even ignore some variable values. This doesn't happen with Source Filmmaker Source Filmmaker and Garry's Mod Garry's Mod  [todo tested in?]
Icon-Bug.pngBug:Tokens cannot be pushed further than 3 $include levels  [todo tested in?]

This command is a lighter version of $definemacro.

Example

$definevariable baseFile "fighter1"

$sequence attack $baseFile$_Attack // results in -> fighter1_Attack when resolved
$sequence run    $baseFile$_Run    // results in -> fighter1_Run    when resolved
$sequence idle   $baseFile$_Idle   // results in -> fighter1_Idle   when resolved

Here's an example of passing the variable to a macro. We pass $basefile$ to the macro to replace instances of $fightername$

$definevariable baseFile "fighter1"

$definemacro makefighteranim fightername framerate \\
$sequence attack $fightername$_Attack fps $framerate$ \\
$sequence run    $fightername$_Run    fps $framerate$ \\
$sequence idle   $fightername$_Idle   fps $framerate$ \\

$makefighteranim $baseFile$ 30 // Here we give "fighter1" from $basefile$ to the macro to be used for $fightername$ token

Using Invalid Variables as Conditionals

Due to the compiler only ever using the first instance of a variable it encounters, you can structure parts of your QC logic to rely on a certain variable, and only have that variable properly exposed to the compiler under certain criteria.

Define a macro or write an $include that contains your conditional compile parameters, and place either the valid or the invalid variable within. Further down the compile chain, you can then state the variable again just before the QC logic that requires it, ensuring it is properly defined (or blocked) only under certain conditions.

This is useful for complex compiles where repeatedly deleting or commenting out large sections of logic is impractical, or removing an $include or macro introduces other compile issues. In addition to simply changing compile parameters, many QC commands that you may want to use conditionally in such a compile can be invalidated altogether by a misnamed variable without actually breaking the compile, such as $renamebone, $alwayscollapse, and $jigglebone.

Example

Here we have a model that we want to quickly compile in two different ways - with jiggleboned hair, and without. There are more jigglebones elsewhere on the model so we want a solution that only disables the hair jiggles.

If $include_hair_jiggles is not exposed, the correct variable "hair" is not encountered, instead the jigglebone commands do nothing because it now tries to jiggle bones that do not exist. (prp_null instead of prp_hair)

Thus we now have the ability to toggle the hair jiggles on and off by simply commenting out $include_hair_jiggles.

$include_hair_jiggles

$definemacro include_hair_jiggles \\
$definevariable hair "hair"       \\
//complex logic goes here         \\

$definemacro jiggle bone      \\
$jigglebone $bone$            \\
{                             \\
	is_flexible {             \\
		yaw_stiffness 80      \\
		yaw_damping 8         \\
		pitch_stiffness 80    \\
		pitch_damping 8       \\
		length 20             \\
		angle_constraint 10   \\
		}                     \\
}                             \\

$definevariable hair "null"
$jiggle prp_$hair$_front_L   $jiggle prp_$hair$_side_L   $jiggle prp_$hair$_back_L	
$jiggle prp_$hair$_front_R   $jiggle prp_$hair$_side_R   $jiggle prp_$hair$_back_R

The same results can usually be achieved by just using $definemacro, but this provides a simple alternative solution for situations where using a macro alone would create some kind of conflict.