• 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!

GameMaker [SOLVED] Copy data from a 2d array to another 2d array?

PlayerOne

Member
Rather straight forward: I'm currently trying to find a method to copy 2d array data from one object to another. I know array_copy() exists but that seems to only work with 1D arrays.

Any help is appreciated.
 
Easiest way is probably to take advantage of Studio's "copy-on-write" array implementation. To be breif, create a script as follows:

array_copy_2d
Code:
/// array_copy_2d(array) = array_copy;

var arr = argument0;

var temp = arr[0, 0];

arr[0, 0] = 0;  // Without being able to trst, this line might not be needed
arr[0, 0] = temp;

return (arr);
... Then use it like thus:

Code:
var my_array, arr_copy;

may_array[0, 0] = 0;
my_array[0, 1] = 1;
// etc...
my_array[4, 7] = 678;

arr_copy = array_copy_2d(my_array);
 

Simon Gust

Member
I am unsure, but I think you can copy a refrence and use it forever without using an accessor.
Code:
new_array = old_array;
the new_array is now the same as old_array and as long as you don't use an accessor to edit new_array, it shouldn't make changes on old_array.
Code:
new_array[i, j] = 10; // <-- OK
new_array[@ i, j] = 10; // <-- not OK, you're editing old_array
 

PlayerOne

Member
Easiest way is probably to take advantage of Studio's "copy-on-write" array implementation. To be breif, create a script as follows:

array_copy_2d
Code:
/// array_copy_2d(array) = array_copy;

var arr = argument0;

var temp = arr[0, 0];

arr[0, 0] = 0;  // Without being able to trst, this line might not be needed
arr[0, 0] = temp;

return (arr);
... Then use it like thus:

Code:
var my_array, arr_copy;

may_array[0, 0] = 0;
my_array[0, 1] = 1;
// etc...
my_array[4, 7] = 678;

arr_copy = array_copy_2d(my_array);

I'll be honest I didn't think this would work due to the lack for loops. Since looping and copying the data seemed like the norm when going through grid /array data.

Your code did work and frankly I'm surprised. This is something I'll keep in my archive for future use. You sir are amazing.
 

Speno

Member
Easiest way is probably to take advantage of Studio's "copy-on-write" array implementation. To be breif, create a script as follows:

array_copy_2d
Code:
/// array_copy_2d(array) = array_copy;

var arr = argument0;

var temp = arr[0, 0];

arr[0, 0] = 0;  // Without being able to trst, this line might not be needed
arr[0, 0] = temp;

return (arr);
... Then use it like thus:

Code:
var my_array, arr_copy;

may_array[0, 0] = 0;
my_array[0, 1] = 1;
// etc...
my_array[4, 7] = 678;

arr_copy = array_copy_2d(my_array);

I ran into issues with this solution, in some circumstances the "copied" array definitely still referred to the original (much to my surprise since it definitely worked in other cases). I still have no idea what causes this to happen, but it might have been something to do with me embedding my arrays within arrays of ds_maps.
From here on out I'll be using the following to be safe, which worked instantly for me:


GML:
function array_copy_2d(array)
{
    var copy = []
    for(var a = 0; a < array_length(array);a++){
            copy[a] = []
            array_copy(copy[a],0,array[a],0,array_length(array[a]))
    }
    return copy
}
 

FrostyCat

Redemption Seeker
@Speno

This is why you should check timestamps before using a solution. The thread is from 2018, which predates both the removal of 2D arrays in 2020 and the deprecation of array copy-on-write in 2022. The solution presented has been dying for years and officially dead for months now.

If you find another old thread before 2020 containing supposed "solutions", DO NOT post on it thanking or replying to anyone in it, and DO NOT be surprised if it no longer works. Let it sit where it was, and if you want to ask questions about it, create a new thread with a link to it.
 

Waesome

Member
Found this thread in Sept. 2022 while having a problem copying nested arrays.

I had to code up a solution which seems to work while probably slow.
FYI I don't like using recursion either.
GML:
function sReplaceArrayByCopy(dst, src){

    array_delete(dst, 0, array_length(dst))
    for (var i = 0; i < array_length(src); i++)
    {
        if is_array(src[i])
            {    dst[i] = []
                sReplaceArrayByCopy(dst[i], src[i])}
        else
            {dst[i] = src[i]}
    }

}
This should work with nested arrays of any type
[1, [21,22, [ 231, 232], 24], 3, 4, [51,52], 6] for example should be copyable

if you don't do this with nested arrays the you might only have references to the original array's subarrays, which will change the original when modifying the copy.

There might be a better way but I couldn't find anything
 
Top