Alphish / gm-community-toolbox

A collection of utility functions for GameMaker.
MIT License
36 stars 6 forks source link

array_duplicate #89

Open Kezarus-tech opened 3 days ago

Kezarus-tech commented 3 days ago

A simple function to duplicate an array. Good when you need to do some computations, but don't want to mess up the original array.

function array_duplicate(arraySource){
    var len = array_length(arraySource);
    var arrayDest = [];
    array_copy(arrayDest, 0, arraySource, 0, len);

    return arrayDest;
}
Alphish commented 3 days ago

Might want to consider the naming here - given the convention in other programming languages and variable_clone being a thing in GameMaker, array_clone(source, deep = false) might work better.

But I don't mind the concept of the function itself! Even if it may be solved with variable_clone(array, 1) for shallow copy or variable_clone(array, 128) for deep copy, sometimes having dedicated and more descriptive functions works for the better. On that note, struct_clone(source, deep = false) might be handy too!

I'll leave it here for more discussion.

Kezarus-tech commented 1 day ago

If variable_clone already exists, I don't see why implement something like I suggested.

And, hmmm, I see that variable_clone can be a name too broad for readability, but not that much as GML isn't a strongly typed language. Well, I leave the closing of this to you guys, but I don't think there is a need for a duplicated function just for name sake.

Mercerenies commented 21 hours ago

I had no idea variable_clone worked for arrays too. I think having an array_clone would be great, especially if the docs for this proposed function make a note about variable_clone. Even if the function itself doesn't get used, it could serve as a great signpost for people like me who didn't know any better.