GPC Developer Guides
User Created Functions
as well as having a significant number of built in functions, gpc allows a 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 gpc main { if (get val(ps4 cross)) { // if ps4 cross is held 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 ps4 circle is held return 1stvalue + 2ndvalue; // return 1stvalue plus 2ndvalue } return 1stvalue 2ndvalue; // return 1stvalue minus 2ndvalue } calling a function gpc 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 gpc function myfunction() {} to declare a function type function followed by a name and () within the parenthesis () 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 🔴 syntax function \<name> ( \<parameter(s) >); ⚪ parameters \<name> the name of the function \<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 gpc function myfunction() {} function parameters can be thought of as 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 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 it 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 the desired value in the following example, you will see a couple of uses for the return command; gpc 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 a value of 0 } return 1; //return a value of 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 gpc if(myfunction()) { the code in myfunction() is then run if xb1 view is not being pressed, the code nested in the statement gpc if(get val(xb1 view)) { //if we get a value from view other than 0 xb1 view is ignored as the if statement is false so the next line executed in the function is gpc 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; gpc if(myfunction()) { thus making the above statement true and the code; gpc 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; gpc 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 is 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 gpc return 0; //return 0 at which point a value of 0 is returned to the statement; gpc if(myfunction()) { making it false , so the code; gpc 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; gpc 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 let's 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 let's take a look at the declaration of the function 'adjust speed'; gpc 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; gpc 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 fourth parameter (10 in this case) is added to the value of var the value of var is 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 fourth parameter is subtracted from the value of var the value of var is 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