• Hey! Guest! The 40th (!!!) GMC Jam will take place between February 25th, 12:00 UTC to March 1st 12:00 UTC. Why not join in this very special anniversary jam! Click here to find out more!

GMS 2.3+ How to remove warnings for optional arguments?

Hi!

In my current project, I have quite a few functions where the last argument is an optional instance id. They are generally of the following form:

GML:
function fn(arg_1)
{
    var arg_2 = argument_count > 1 ? argument[1] : self;
    arg_2.something = arg_1; // or something to this effect
}
The problem with this is that arguments are not all listed in the function declaration, which imo hinders readability a bit. Since, according to the manual, unset arguments simply get initialized to undefined, I thought I would check for this in the function body, which leads to this mildly more readable form:

GML:
function fn(arg_1, arg_2)
{
    if (is_undefined(arg_2)) arg_2 = self;
    arg_2.something = arg_1; // or something to this effect
}
Which, granted, does work when I run the game. However, the code editor still gives me warnings when I call this function, telling me that I'm passing fewer arguments than are necessary. I tried following the JSDoc format for optional arguments, but this doesn't seem to remove the warnings:

GML:
/// @function      fn(arg_1, arg_2)
/// @param arg_1   The first, non-optional argument
/// @param [arg_2] The second, optional argument
function fn(arg_1, arg_2)
{
    if (is_undefined(arg_2)) arg_2 = self;
    arg_2.something = arg_1; // or something to this effect
}
Is there a way to remove these unwanted warnings, or must I use the (imho) less legible argument_count strategy for the time being?

Thanks in advance!
 

samspade

Member
I don't think there's a perfect solution, but here's another imperfect one: don't name the arguments. You can still use the @param to provide instructions
GML:
/// @function fn(value, [inst])
/// @param {value} num non_optional
/// @param {inst} inst_id optional
function fn()
{
    var inst = argument_count > 1 ? argument[1] : self;
    inst.something = argument[0]; // or something to this effect
}
Personally, I'd generally prefer the flags.
 

DaveInDev

Member
Hi there, I encounter the same problem. Annoying warnings as soon as I try to use optional arguments with the undefined default value. Any solution since your initial post ?
 

Nidoking

Member
How hard is it to write a wrapper function that takes just one argument and calls this function with self or self.id as the second argument?

function fn_self(arg_1)
{
return fn(arg_1, self.id);
}

Call the one you need for any situation and no warnings needed.
 

DaveInDev

Member
Yes of course, that's a turnaround. But if you use several optional arguments like I do, you need many wrappers. -> Not very readable at the end.
I could also go back to the old arguments[] way, but it's again a lose of readability.

And, as this very promising "optional arguments with the undefined default value" feature was introduced in recent versions (with these new flexible named arguments), we could expect that this warning could be disabled in some way. Maybe in the langage/gml options, where you alread can find a specific warning option...
 

Nidoking

Member
Yes of course, that's a turnaround. But if you use several optional arguments like I do, you need many wrappers. -> Not very readable at the end.
If you have that many optional arguments, surely there must be sensible defaults. If the warnings bother you that much, just use the default arguments when you call the function. I'm going to agree with you that there should be a way to specify that arguments are optional and explicitly suppress warnings based on not supplying them, but I would personally consider the warnings a cost of using optional arguments as things stand, and say that if you don't want to pay the cost, don't use them.
 

DaveInDev

Member
Of course, I'll accept these warnings if I must.
My question was very simple : I just wanted to know if there was some news on this subject. Apparently not. Thanks.
 

Yal

🍋 *lemon noises*
GMC Elder
Yes of course, that's a turnaround. But if you use several optional arguments like I do, you need many wrappers. -> Not very readable at the end.
I could also go back to the old arguments[] way, but it's again a lose of readability.

And, as this very promising "optional arguments with the undefined default value" feature was introduced in recent versions (with these new flexible named arguments), we could expect that this warning could be disabled in some way. Maybe in the langage/gml options, where you alread can find a specific warning option...
How about making your variadic functions take an array literal argument called optional, then unpacking it into more descriptive vars in the function? Better than having zero named arguments, at least.

For even more readability, pass in a ds_map instead:
GML:
var optional = ds_map_create();
optional[?"girth"] = 20
optional[?"versimilitude"] = 0.5
optional[?"taste"] = "eucalyptus"
myFunction(self, pi, 3.07, optional)
(This also means your arguments become mutually independent, which gives you even more freedom)
 
Top