YoYoGames / GameMaker-Bugs

Public tracking for GameMaker bugs
26 stars 8 forks source link

A function to copy the argument array to a GML array #6732

Open tabularelf opened 4 months ago

tabularelf commented 4 months ago

Is your feature request related to a problem?

Sometimes it'd be nice to take the contents of argument and pass it straight into array functions for processing, as it isn't super fast or as convenient in some cases.

Describe the solution you'd like

The new function argument_array_copy([srcOffset], [srcLength], [destArray], [destOffset]), will copy the whole argument array within the current function scope, into a GML array. If the target GML array is not big enough, it will be resized accordingly to fit in the argument contents.

The arguments go as follow:

Example:

function add() {
   static args = [];
   // The argument array contents is copied to the static variable args.
   // Automatically resizing the GML array if it's not big enough.
   argument_array_copy(0, argument_count, args); 

   return array_reduce(args, function(previous, current, index) {
       return previous + current;
   }, 0, argument_count);
}

Describe alternatives you've considered

Looping

function add() {
    var i = 0;
    var value = 0;
    repeat(argument_count) {
        value+= argument[i];
        i++;
    }
    return value;
}

Passing in an array of values

function add(args) {
   return array_reduce(args, function(previous, current, index) {
       return previous + current;
   });
}

Additional context

I had previously discussed this with Russell on Discord about possibly allowing to pass argument directly to array functions, without creating an array loop. Due to complications, it was instead suggested to be a function that can copy the argument array, as a GML array. Though, seeing that I can't find an existing feature request, I decided to make one.

tinkerer-red commented 1 month ago

Honestly if variable_clone(argument) or array_copy([], 0, argument, 0, argument_count) just worked I wouldn't mind handling the more niche cases on my own. However once array_copy is supported, most people will begin to ask for all the other array functions, doing just variable clone would avoid this issue, and still open it up for more use cases over all.