Variables In VSL
From Virtools Wiki
Contents |
Introduction
A variable is a way to store a value that can be used inside a VSL script. A variable always has a specified type (f.e. integer, float, boolean etc) and a scope. The scope of the variable determines in which parts of the script it can be used and in which parts of the script it cannot be used. Here are a few examples of variable scopes:
Code:
void foo()
{
float x; // declare the variable, start of scope
// variable x can only be used here
} // end of scope
void foo(int a) // start of scope for a
{
// a can only be used here
} // end of scope for a
void foo()
{
for (int i = 0; i < 10; ++i) // start of scope for i
{
// i can only be used here
} // end of scope for i
}
Global Variables
Global variables are basically variables that are declared outside a function and therefor can be accessed from within multiple functions. Global variables are defined at the very top of your VSL script (global variables cannot be declared inside functions). There are two types of global variables:
- static global variables, this type of global variable will keep its value between two executions of the script.
- shared global variables, this type of global variable can keep its value static, but it can be shared between all scripts that declare it.
Code:
static int a; // declare global static variable a
shared float b; // declare global shared variable b
void foo1()
{
// use a and b here
}
void foo2()
{
// use a and b here too!
}
Structures
You can organize variables inside a struct. A struct is a user-defined composite type composed of members, members can be different types. For example:
Code:
struct Person // define the structure
{
String name;
int age;
float weight;
}
void main()
{
Person P; // declare P
P.name = "Santa"; // set the values
age = 300;
wieght = 500;
}
Enums
An enumerated type is a user-defined type that consists of a set of constants called enumerators. When you define enumerations they are assigned integer values by default starting with 0. Here is how you define and use an enum:
Code:
enum FruitType
{
APPLE, // number 0 by default
PEAR, // 1
BANANA, // 2
KIWI // 3
};
void main()
{
FruitType F; // declare variable F
F = BANANA; // set F to BANANA (2)
}
