Main Section

The main section is the heart and sole of any GPC script. It is run in a constant loop and any code executed during run time can be traced back to it. Therefore, the main section is mandatory and a GPC script is not valid without one.

Just as with any nested code, the main section start and finish points are denoted with a { or } respectively, like so;

main
{ //Main Start
}

When the main section reaches the End point, the output report to the console is created, any remaps are evaluated and the report is sent to the console. The main section is then restarted from the beginning.

Code within the main section is executed in the order it is written. It is important to remember this when building your own GPC scripts as the output report to the console is created at the end of the main section, so it is possible to create code which would cancel each other out and result in a different output to the console than you expected. For example;

int press_lt;
main{
if(get_val(XB1_RT)){
press_lt = TRUE;
}
press_lt = FALSE;
if(press_lt) {
set_val(XB1_LT, 100);
}
}

As you can see above, the variable 'press_lt' is set to TRUE when the RT/R2 button is pressed but is immediately set to FALSE in the next line of code. Therefore theif(press_lt) statement will always be FALSE and the code nested within the if statement will never be executed.

However, if we were to move the code around, then we would get the expected output;

int press_lt;
main{
press_lt = FALSE;
if(get_val(XB1_RT)){
press_lt = TRUE;
}
if(press_lt) {
set_val(XB1_LT, 100);
}
}

If RT/R2 is pressed then press_lt is set to TRUE after it has been set to FALSE, the if(press_lt) statement will see that it is TRUE and the nested code will be run. If RT/R2 isn't being pressed then 'press_lt' is not set to TRUE after is has been set to FALSE and the nested code is not run.

If press_lt = FALSE; was nested within an else statement, which is a better way to do it, then it could be placed after the press_lt = TRUE; line, like so;

int press_lt;
main{
if(get_val(XB1_RT)){
press_lt = TRUE;
} else {
press_lt = FALSE;
}
if(press_lt) {
set_val(XB1_LT, 100);
}
}

The above code will operate as expected because the else statement will only be TRUE if RT/R2 is not being pressed. Therefore, 'press_lt' is only set to false when RT/R2 is not pressed.

So, as you can see in the above examples, it is important to remember that code is executed in the order it is written and simply moving the placement of a line can have a significant effect of the output to the console.