• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Optional Arguments for functions?

With 2.3 having the new functions stuff how do i declare optional arguments? do i still have to use the argument[...] format like so?

GML:
function myfunction(argument[0],argument[1])
{

}
or can i just do something like

GML:
function myfunction(wx[0],wy[1]){

}
 

chamaeleon

Member
With 2.3 having the new functions stuff how do i declare optional arguments? do i still have to use the argument[...] format like so?
Use argument_count and argument[] for the optional arguments.
GML:
function foo(a, b) {
    show_debug_message("a = " + string(a));
    show_debug_message("b = " + string(b));
    for (var i = 2; i < argument_count; i++) {
        show_debug_message("Optional arg " + string(i) + " = " + string(argument[i]));
    }
}
GML:
foo("abc", "def", "ghi", "jkl");
Code:
a = abc
b = def
Optional arg 2 = ghi
Optional arg 3 = jkl
 

rytan451

Member
GML:
function foo(a, b, c) {
    if (is_undefined(a)) {
        show_debug_message("a = undefined");
    } else {
        show_debug_message("a = " + string(a));
    }
    
    if (is_undefined(b)) {
        show_debug_message("b = undefined");
    } else {
        show_debug_message("b = " + string(b));
    }

    if (is_undefined(b)) {
        show_debug_message("b = undefined");
    } else {
        show_debug_message("b = " + string(b));
    }
}


foo(1, 2);
// a = 1
// b = 2
// c = undefined
 
Top