×
Menu

Flow Control Commands

Flow Control Commands
 
Flow control commands are integral to most GPC scripts as they allow you control the flow of executed code and tell the GPC how and when to make decisions.   Without any flow control commands in place, all your combos would run at once and you would have probably end up with a useless script.  Below are the GPC commands you can use to control the flow of code;
 
Command
Description
Evaluates an expression to its boolean value
Executes alternative code when an if statement is FALSE
Executes alternative code when an if statement is FALSE and its expression is TRUE
Loops code until its expression is FALSE or the break command is used
 
1
if
 
The if command is one of the single most useful tools for your GPC scripting needs.  It allows you to control when certain blocks of code are run and it takes any expression, function or code which returns or has a value. It can also be used in the init, main, combo and function sections of your code.
 
The if command works by evaluating the expression within it to its boolean value.  
 
An expression is anything which has a value. Such as a function call which returns a value, a variable, a mathematical sum. literal values and comparisons.
 
In computer programming languages, a Boolean is a data type with only the two possible values, TRUE or FALSE.  In GPC, FALSE means 0 and TRUE is a value other than 0 be it a negative or positive value.
 
If the expression placed within an if commands brackets is TRUE, then the code nested within it is executed, as sown below;
 
// |¯¯¯¯¯¯¯¯¯¯¯¯|
// | If this is |
// ↓    true    ↓
if ( expression )
{   //←¯¯¯¯¯¯¯¯¯|
    //          | Run code placed here
}   //←_________|
 
If the expression is FALSE, then the code contained within the block is ignored.
 
You can also nest an if command within another if command in as many times as the bytecode limit will allow in your script. As shown below with other examples of if command usage;
 
int x = 5, y = 10, z = 20;
 
init {
 
    if(get_console() == PIO_PS4)
        x = 30;
 
}
 
main {
 
    if(get_val(7)) {
        if(x < y) {
            if(get_val(9) < -50)
                x = y + z;
            if(get_val(9) > 50)
                x = y + 20;
        }
    }
    if(x <= (z - y)) {
        z = y;
        x = 6;
    }
 
}
2
else
 
Should you wish to execute a different block of code when an if command does not return TRUE then you would use the else command.
 
An else command must have an if command preceding it.  The code block contained within an else command will be executed if the expression in the if statement returns FALSE, as shown below;
 
int ADS;
 
main {
 
    if(get_val(XB1_LT)) // if LT / L2 is pressed...
        ADS = TRUE;
    else               // else (If LT / L2 is not pressed)
        ADS = FALSE;
 
}
 
The above code will set the variable ADS to TRUE if LT / L2 is pressed and will set it to FALSE if it is not.
3
else if
 
else if is a combination of else and if.  Just like the else command it allows for a different block of code to be executed when the statement within the if command returns FALSE.  However, it will only execute the code block when the statement within its parameter returns TRUE.
 
For example, if you were playing a first person shooter game and wished for rapid fire to be disabled when you aim down the sights but wanted the CronusMAX PLUS to automatically hold breath in the game to steady your aim while aiming down the sights, the following code would do this for you;
 
main {
 
    if(get_val(XB1_LT)) // if LT / L2 is pressed...
        combo_run(Auto_HB);
    else if(get_val(XB1_RT)) // else if LT / L2 is not pressed and RT / R2 is....
        combo_run(Rapid_Fire);
 
}
 
combo Auto_HB {
    wait(400);
    set_val(XB1_LS, 100);
    wait(100);
    wait(2000);
}
 
combo Rapid_Fire {
    set_val(XB1_RT, 100);
    wait(40);
    set_val(XB1_RT, 0);
    wait(30);
    set_val(XB1_RT, 0);
}
 
You can also extend an else if command with an else command or another else if statement.
 
main {
 
    if(get_val(XB1_LT)) // if LT / L2 is pressed...
        combo_run(Auto_HB);
    else if(get_val(XB1_RT)) // else if LT / L2 is not pressed and RT / R2 is....
        combo_run(Rapid_Fire);
    else {
        sensitivity(XB1_RX, 50, 125);
        sensitivity(XB1_RY, 50, 125);
    }
 
}
4
while
 
Other than the main section, the while command is the only loop function in GPC.  As the main function runs in a loop, there really isn't a need to use this command.  If you do choose to use it, although it can be used in the main section, it is recommended to only use it within a function so as not to temporarily halt the execution of a main iteration.
 
The operation of the while command is straight forward, it will execute code nested within it until the expression contained in its parameter is no longer TRUE;
 
main {
 
    if(event_press(XB1_A))
        _myfunction(10);
 
}
 
function _myfunction(var) {
 
    while(var) {
        var = var - 1;
    }
 
}
 
5
break
 
Should you wish to be able to end the while loop early, the break command has been created to make this possible.  You would simply create a condition, which when TRUE, would call the break command thus ending the while loop, like so;
 
main {
 
    if(event_press(XB1_A))
        _myfunction(10);
 
}
 
function _myfunction(var) {
 
    while(var) {
        var = var - 1;
 
        if(event_press(XB1_B))
            break;
    }
 
}