All variables in GPC are 16 bit signed integers. An integer (from the Latin 'integer' which means 'whole') is a number which can be written without a fractional component. For example, 0, 20, 128 and -1000 are all integers while 4.2, 5.6 or -110.9 are not. Therefore, GPC does not support fractions and will round down any decimal to a zero. Meaning 3.4 would become 3.
16 bit signed means the variables can store an integer ranging from -32768 to +32767
The following sections on variables can be found within this page;
Declaring Variables
A variable is a place where data can be stored in the Virtual Machines memory. A variables name can start with either an underscore ( _ ) or a letter and can be followed by any combination of letters, digits or underscores. They are however case sensitive, so cronuszen, CronusZen and CRONUSZEN would specify three different variables.
Variables defined this way in GPC are global, this means they can accessed and modified within the init or main sections as well as a combo or function. Only variables assigned to user created functions are local. Details of how variables operate within user functions can be found here.
Global variables must be declared before the main or init sections and therefore cannot be declared after or in either of those sections. As shown below;
int myVar = 100, MYVar;
int MYVAR = -40;
init {
int incorrect; //This will cause an error
}
int Incorrect; //This will also cause an error
main {
int INCORRECT; //This will again cause an error
}
Variables are always assigned a value. If no value is assigned when they are declared, then they are initialized with a value of 0 (zero). The value assigned to a variable can be altered during runtime, as shown below;
int myVar = 100, MYVar; // myVar initial value is 100, MYVar initial value is 0
int MYVAR = -40; // MYVAR initial value is -40
main {
MYVar = myVAR - MYVAR; // MYVar value is now 60
}
Boolean Variables
Unlike other programming languages, such as C# for example, GPC does not require a separate variable type for Boolean values. The integers in GPC have been designed to support Boolean expressions. The keywords TRUE and FALSE have been assigned the values 1 and 0 respectively as shown in the example below;
int myvar, MYVAR;
main {
myvar = TRUE; //myvar = 1
MYVAR = FALSE; //MYVAR = 0;
}
You can therefore use integers in your code to create a toggle switch which enables or disables sections of code, like so;
int myvar;
main {
if(event_press(XB360_A)) { //If A / Cross is pressed on the controller
myvar = !myvar; //myvar equals not myvar. So if myvar is TRUE or has a value it will be made FALSE / assigned the value of 0 (zero)
//If myvar is FALSE or has a value of 0 (zero) it will be made TRUE / assigned the value of 1
}
if(myvar) { //If myvar has a value other than 0 (zero)
//Do something
}
}
As seen in the above example, a variable will return TRUE in an if statement if it has any value other than 0 (zero). You can however use operators should you wish for your nested code to only be active if a variable is a certain value;
int myvar;
main {
if(event_press(XB360_A)) { //If A / Cross is pressed on the controller
myvar = myvar + 1; //myvar equals myvar plus 1.
}
if(event_press(XB360_X)) { //If X / Square is pressed on the controller
myvar = myvar - 1; //myvar equals myvar minus 1.
}
if(myvar == 2) { //If mvar has a value of 2.
//Do something
}
if(myvar >= 4) { //If mvar has a value of greater than or equal to 4.
//Do something
}
if(myvar < 2) { //If mvar has a value less than 2.
//Do something
}
}
Arrays
An array is a method for creating multiple variables in a single command. Arrays are 0 (zero) based so creating a 5 dimensional array will create 5 variables which can be accessed with the indexes 0 through 4, as shown below:
Arrays are assigned a value of 0 (zero) when initialized and cannot be assigned a different value before the init section. They are also global and can be assigned a value or modified at any point during run time in your script, like so:
When a script is compiled, all global variables are placed within an array. You can therefore use an array call to access subsequent variables from a variable which has not be initialized as an array, as shown below:
int myvar;
int _1st = 10;
int _2nd = 20;
int _3rd = 30;
int _4th = 40, _5th = 50;
int var_to_place_value_into;
main {
var_to_place_value_into = myvar[1]; //var_to_place_value_into equals 10 (_1st)
var_to_place_value_into = myvar[2]; //var_to_place_value_into equals 20 (_2nd)
var_to_place_value_into = (myvar[4] + myvar[5]) - myvar[3]; //var_to_place_value_into equals 60 ((40 + 50) - 30)
var_to_place_value_into = _2nd[1]; //var_to_place_value_into equars 30 (_3rd)
}