/
Work with variables in HSL scripts

Work with variables in HSL scripts

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).

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:

data_type $variable_name;

where:

data_type identifies the type of data object that the variable can contain
variable_name 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:

data_type $variable_name = value;

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 Boolean flag. See Boolean. true
Float Floating point number. See Float. 265.8, -973.2
Int Integer. See Int. 0, 4, -7
String Characters. See String. "My mother has a car!"
Vector One-dimensional array of 3 floating points or integers (stored as floating points). See Vector. <<1, 8.2, 7.6>>

Variable names

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:

string $boneName;
int $_sample_set;
string $name;

The following examples show the incorrect use of variables:

float $2PI;
vector cheese;

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.

string $boneName;

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[2];
$test[0] = 1;
$test[1] = 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.
 getGlobalFloatVar;
 getGlobalIntVar;
 getGlobalStringVar;
Caution
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.



© Copyright Vicon Motion Systems. All rights reserved.

Vicon trademarks