A variable is a name that you use in a command to represent a type of data. The variable is replaced with a specific data value when the command is executed. A variable consists of its name and a data type, which identifies the type of value the variable represents.
A variable is used as an argument in Shogun Post command syntax (see Understand HSL command syntax).
The following topics explain how to:
For information on reserved words, also see Understand reserved words.
Declare and assign variables
Declaring a variable means defining its name and data type. You must declare variables before you can use them. You can declare multiple variables on a single script line. It is wise to declare the variables a script will use near the top of the script, and to provide comments which detail what each variable does.
You declare a variable using the following syntax:
|identifies the type of data object that the variable can contain|
|is the name of the variable.|
Assigning a variable means giving a specific value to a declared variable. You assign a value to a variable using the equals sign (=). This is usually done to assign the results of an operation to a variable.
An assignment statement, is written with the variable followed by the equals sign, followed by the value assigned to the variable:
For example, to assign the type and value assigned to a variable named monster:
float $monster = 3.8;
The result of this assignment statement is a value of 3.8 being set for a variable named monster with a data type of float. This result can be used as a constant of the same type and value.
Variable data types
This table shows the data types that can be used for Shogun Post variables.
|Variable data type||Definition||Examples|
|Boolean flag. See Boolean.|
|Floating point number. See Float.|
|One-dimensional array of 3 floating points or integers (stored as floating points). See Vector.|
When naming a variable, note that the name:
- Must begin with a dollar sign ($)
- Must not contain spaces between characters
- Must not begin with a number
- Must not use reserved words (see Understand reserved words)
- Is case sensitive; note whether letters should be upper or lowercase
Variable syntax examples
The following examples show the correct use of variables:
The following examples show the incorrect use of variables:
The first example is invalid because it starts with number. The second example is invalid because it does not start with the dollar sign ($).
Declaration syntax examples
The following example shows how to declare a variable of type string called boneName which will be replaced with a bone name.
The following example shows how to declare multiple variables:
string $str1, $str2, $str3;
Assignment syntax examples
The following examples show how to assign specific values to an integer, float, and vector variable respectively:
int $frame = 1; float $distance = 9.54322; vector $position = <<1, 2.345, 6.789>>;
The following example declares and assigns arrays:
int $test; $test = 1; $test = 2;
Use global variables
Global variables are variables which persist across scripts and until Shogun Post is closed.
Here is an example of setting a global variable:
// Sets a global variable.
setGlobalVar $myVar value;
// Gets a global variable.
// Appropriate command must be used depending on variable type.
There is no way to delete a global variable, or to list global variables that have been set.
In general, global variables are something you want to use very sparingly. Shogun Post scripts can take parameters (inputs) and return a value, which reduces the need for global variables.