A variable (var) is a named storage location that contains a value and can be read/rewrite many times. There are two types of variables.
A global variable starts with
$followed by an identifier (name). The global variable name is any combination of letters, digits and
$variable1 $100 $____
Their values are available from any place of the code.
A saved variable is a special global variable available only in
VCSmodes. Its name is prefixed with
$_var. The value of this variable persists across saved games. Global variables denoted by
$var) are not saved and they get blank values when the LCS or VCS game loads.
A local variable name may only be a number followed by
Their values are available only within the current script or the mission.
Each script or a mission have 2 special local variables called
TIMERB. The value of a timer variable is increased automatically when the game clock advances, so they are commonly used to measure time elapsed since the timer reset:
0006: TIMERA = 0 // reset the timer
0001: wait 0 ms
0019: TIMERA > 2000 // if the timer value is > 2000, i.e. 2 seconds has passed
004D: jump_if_false @WAIT_2S
0662: printstring "2 seconds has passed" // display the message
Variables are commonly used in the expressions. If the right operand is a number constant, the opcode can be omitted:
$var = 0
$myarray($index, 10i) >= 150
If both operands in the expression are variables, the compiler can not determine the correct opcode, because the types of the variables are unknown.
For example, there are two opcodes to increment a variable value:
0058for integer values and
0059for floating-point values.
0058: $Var1 += $Var2 // (int)
0059: $Var1 += $Var2 // (float)
Assuming there is no opcode, which one to use?
$Var1 += $Var2 // ??
To communicate the compiler a variable type use the
VAR..ENDconstruct allows to declare variables and their types for the advanced use.
var <variable>: <type> end
For example, if both variables are declared, the compiler is able to process the expression without opcodes:
$Var1 : Integer
$Var2 : Integer
$Var1 += $Var2 // opcode 0058
The following types of variables are supported:
Int- integer values
Float- floating-points values
ShortString- a variable containing a string literal with the fixed length (only for the arrays, use
LongString- a variable containing a string literal with the variable length (only for the arrays, use
The types of the local variables can be declared too.
Since v3.2.0 it's possible to declare a variable of a built-in type (
LongString) using only the type name.
<type> <variable name>
Starting from v3.4.0 it is possible to declare variables with custom names:
a = 1
distance = 15.5
name = 'CJ'
An initial value can follow the variable name to reduce the number of the lines of code:
int a = 1
float distance = 15.5
string name = 'CJ'
The compiler binds a new local variable to each name. In the example above one may expect a resulting code to look like:
You can specify an initial value for the variable when declaring it. Write
=and then the value:
$fVar: float = 1.0
float $fVar = 1.0
$fVaris now declared as
Floatand the compiler adds the opcode
0005in the script:
0005: $fVar = 1.0
Initialization is allowed for variables, but not for arrays.