User Created Functions
User Created Functions
As well as having a significant number of built in functions, GPC allows the user to create their own custom functions. A function can run any code valid in the main section and code is also executed in the order it is written.
main {
if (get_val(PS4_CROSS)) { // If we get a value other than 0 from A / CROSS...
if (myfunction(10,20) == 30) { // If myfunction returns a value of 30
combo_run(mycombo); // run combo mycombo
}
else if (myfunction(10, 20) == -10) { // If myfunction returns a value of -10
combo_stop(mycombo); // stop mycombo if it is running
}
}
}
combo mycombo {
set_val(PS4_TRIANGLE, 100);
wait(1000);
wait(1000);
}
function myfunction(_1stvalue, _2ndvalue) { // myfunction start
if (get_val(PS4_CIRCLE)) { // If we get a value other than 0 from B / Circle...
return _1stvalue + _2ndvalue; // return '_1stvalue' plus '_2ndvalue'
}
return _1stvalue - _2ndvalue; // other '_1stvalue' - '_2ndvalue'
}
Calling a Function
myfunction(10,20);
To call (or run) a function, you simply type its name and put any parameters it requires in between ( and ).
When a function is called, the code within it is executed and the return value is sent back to where it was called from.
User functions are what is known as global scope, this means they can be called from the init, main and combo sections. They can even be called from within another function, however, GPC does not support recursive calls of functions. This means a function cannot be called from within itself.
Function Name and Declaration
function myfunction() {}
To declare a function type function followed by a name and (). Within the brackets () you place the names of any parameters you would like the function to have, if any. Function names and parameters follow the same rules as a variable, they can start with either an underscore ( _ ) or a letter and can be followed by any combination of letters, digits or underscores.
Functions must be declared last in a GPC script though, after the main and any combo section.
Syntax
function <Name> ( <Parameter(s) >);
Parameters
<Parameter(s) > : Optional parameters. You can use as many as you wish or none at all. Each one must be separated with a comma (,)
Function Parameters
function myfunction() {}
Function parameters can be thought of like local variables as they cannot be accessed outside of the function they are defined within. A value can be passed to them and they can be used within the function just like a variable could.
As GPC only supports the one data type (16bit Integers) you do not need to specify the data type of parameters within a function and the name of a parameter follows the same rules as a function or variable, they can start with either an underscore ( _ ) or a letter and can be followed by any combination of letters, digits or underscores.
Function parameters are optional. You are not required to have any at all. The example above is perfectly valid.
Returning from a function
return is a command unique to functions. It is not mandatory for each user function to have a return value though. If there is no return in a function, then 0 (zero) will be automatically returned.
You can have multiple return points within a function. Once the first return command is executed, the function returns a value to where is was called and the function is terminated. The code beyond that point in the function will not be run.
Returning a value is one of the single most useful commands within a function as it can be used as a boolean value to enable or disable sections of code, to set parameters in other functions or to set a variable to a desired value. In the following example, you will see a couple of uses for the return command;
int RF_HOLD = 40;
int RF_NULL = 30;
main {
if(myfunction()) {
if(get_val(XB1_RT)) {
combo_run(Rapid_Fire);
}
}
}
combo Rapid_Fire {
set_val(XB1_RT, 100);
wait(RF_HOLD);
set_val(XB1_RT, 0);
wait(RF_NULL);
set_val(XB1_RT, 0);
}
function myfunction() {
if(get_val(XB1_VIEW)) {
if(get_val(XB1_A))
RF_HOLD = adjust_speed(RF_HOLD, 10, 1000, 10);
if(get_val(XB1_B))
RF_NULL = adjust_speed(RF_NULL, 10, 1000, 10);
set_val(XB1_A, 0);
set_val(XB1_B, 0);
set_val(XB1_LB, 0);
set_val(XB1_RB, 0);
set_val(XB1_VIEW, 0);
set_val(TRACE_1, RF_HOLD / 10);
set_val(TRACE_2, RF_NULL / 10);
return 0;
}
return 1;
}
function adjust_speed(var, min_value, max_value, adjustment_increment) {
if(event_press(XB1_RB) && var < max_value)
var = var + adjustment_increment;
if(event_press(XB1_LB) && var > min_value)
var = var - adjustment_increment;
return var;
}
Putting it all together
When the GPC script is first loaded, the two variables RF_HOLD and RF_NULL are created with a value of 40 and 30 respectively.
The main section then starts its first iteration (run). When it gets to the line below the myfunction() function is executed.
if(myfunction()) {
The code in 'myfunction()' is then run. If XB1_VIEW is not being pressed, the code nested in the statement:
if(get_val(XB1_VIEW)) { //If we get a value from View other than 0
is ignored as the if statement is FALSE. So the next line executed in the function is:
return 1; //If we do not get a value from View, return 1
at which point the value of 1 is returned to the statement;
if(myfunction()) {
Thus making the above statement TRUE and the code;
if(get_val(XB1_RT)) { //If we get a value from RT / R2 other than 0
combo_run(Rapid_Fire); //Run combo Rapid_Fire
}
is executed. The main section carries on until it reaches its end point and loops.
However, if XB1_VIEW is being held when 'myfunction()' is executed, then the following code is run;
if(get_val(XB1_A))
RF_HOLD = adjust_speed(RF_HOLD, 10, 1000, 10);
if(get_val(XB1_B))
RF_NULL = adjust_speed(RF_NULL, 10, 1000, 10);
set_val(XB1_A, 0);
set_val(XB1_B, 0);
set_val(XB1_LB, 0);
set_val(XB1_RB, 0);
set_val(XB1_VIEW, 0);
set_val(TRACE_1, RF_HOLD / 10);
set_val(TRACE_2, RF_NULL / 10);
return 0; //Return 0
As you can see, if XB1_A or XB1_B are not also held down, the code set a few buttons to 0, writes the value of our two variables to TRACE values and then most importantly, reaches the line:
return 0; //Return 0
at which point a value of 0 is returned to the statement;
if(myfunction()) {
making it FALSE, so the code;
if(get_val(XB1_RT)) { //If we get a value from RT / R2 other than 0
combo_run(Rapid_Fire); //Run combo Rapid_Fire
}
which is nested within that statement is ignored and not executed.
If XB1_VIEW and XB1_A are both held when 'myfunction()' is executed, then the following line of code is reached and run;
RF_HOLD = adjust_speed(RF_HOLD, 10, 1000, 10);
what the above line means is the variable RF_HOLD equals the return value of the function 'adjust_speed' or you could say the return value from 'adjust_speed' is stored in RF_HOLD. So lets take a look at how that function returns a value.
As you can see above, four values are being sent to the function 'adjust_speed'. The value of RF_HOLD, 10, 1000 and 10. So lets take a look at the declaration of the function 'adjust_speed';
function adjust_speed(var, min_value, max_value, adjustment_increment) {
function 'adjust_speed' requires 4 arguments, the variable to be adjusted, the minimum value you want it to be, the maximum value you wish for it to be and how much to adjust it by each increment.
To manipulate the variable, the function executes the following code;
if(event_press(XB1_RB) && var < max_value)
var = var + adjustment_increment;
if(event_press(XB1_LB) && var > min_value)
var = var - adjustment_increment;
return var;
In the first part of this code, if XB1_RB is pressed and the variable value passed to the function is less than the maximum value allowed, the value passed in the forth parameter (10 in this case) is added to the value of var. The value of var is the returned to where the function is called. Therefore making RF_HOLD equal 10 more than it did before.
If XB1_LB is pressed and the variable value passed to the function is greater than the minimum value allowed, the value passed in the forth parameter is subtracted from the value of var. The value of var is the returned to where the function is called. Therefore making RF_HOLD equal 10 less than it did before.
An identical process is carried out if XB1_VIEW and XB1_B are pressed when 'myfunction()' is executed with the exception being that RF_NULL is adjusted rather than RF_HOLD.
Last updated