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

Copy a ds_grid to another ds_grid

J

Jaydabblju

Guest
Hey guys!

I try to copy an "original_grid" to a "copy_grid". I thought it would be an easy task but it seems a little bit difficult to me.

First I tried "copy_grid = original_grid". This worked and the copy_grid has got the same values as the original_grid.
Then I tried "ds_grid_copy(copy_grid, original_grid)". This worked too.

But in both cases when I change the value of a cell in copy_grid the appropriate cell in original_grid changes too to the new value. It seems the copy_grid references to the original_grid. But I want to have a complete new grid.

I am sure I make a mistake but I dont know what.

Thanks in advance.
 

rIKmAN

Member
Hey guys!

I try to copy an "original_grid" to a "copy_grid". I thought it would be an easy task but it seems a little bit difficult to me.

First I tried "copy_grid = original_grid". This worked and the copy_grid has got the same values as the original_grid.
Then I tried "ds_grid_copy(copy_grid, original_grid)". This worked too.

But in both cases when I change the value of a cell in copy_grid the appropriate cell in original_grid changes too to the new value. It seems the copy_grid references to the original_grid. But I want to have a complete new grid.

I am sure I make a mistake but I dont know what.

Thanks in advance.
Create a second grid, and use ds_grid_copy() to copy the contents from one to the other.

There's also a code example in the manual.
 

rIKmAN

Member
As I wrote I already tried this :)
No you didn't, you said you made a copy of the original grid by just assigning it to another variable, which means both variables point to the same grid.

Create a grid, fill it with your data.
Create a second grid, then use ds_grid_copy() to copy the contents of the original grid into it.

You now have two grids that are seperate entities, not one grid with 2 variables that both point to it.

Code:
grid1 = ds_grid_create(10, 10)
for (var i = 0; i < ds_grid_width(grid1); ++i) 
{
    for (var j = 0; j < ds_grid_height(grid1); ++j) 
    {
        grid1[# i, j] = 1;
    }
}

grid2 = ds_grid_create(10, 10)
ds_grid_copy(grid2, grid1)
grid2[# 0, 0] = 999;

show_debug_message("grid1[0,0] = " + string(grid1[# 0, 0]))
show_debug_message("grid2[0,0] = " + string(grid2[# 0, 0]))
Outputs:
Code:
grid1[0,0] = 1
grid2[0,0] = 999
 
J

Jaydabblju

Guest
Code:
grid1 = ds_grid_create(1, 1);

list1 = ds_list_create();
list2 = ds_list_create();
list2[| 0] = "grid1_list2_0";
list2[| 1] = "grid1_list2_1";
list1[| 0] = "grid1_list1_0";
list1[| 1] = list2;

grid1[# 0, 0] = list1;

grid2 = ds_grid_create(1, 1);

ds_grid_copy(grid2, grid1);

list2[| 0] = "grid2_list2_0";
list2[| 1] = "grid2_list2_1";
list1[| 0] = "grid2_list1_0";
list1[| 1] = list2;

grid2[# 0, 0] = list1;
(see screenshot)
 

Attachments

You must be doing something other than what you want then, because nothing you're doing is causing unintended behavior. Variables aren't data structures, they can only contain pointers to data structures. When you change a value in a nested data structure, it's going to carry over because it's still the same data structure you're reading/writing from and ds_grid_copy() doesn't create any new data structures.
 

Rob

Member
Code:
grid1 = ds_grid_create(1, 1);

list1 = ds_list_create();
list2 = ds_list_create();
list2[| 0] = "grid1_list2_0";
list2[| 1] = "grid1_list2_1";
list1[| 0] = "grid1_list1_0";
list1[| 1] = list2;

grid1[# 0, 0] = list1;

grid2 = ds_grid_create(1, 1);

ds_grid_copy(grid2, grid1);

list2[| 0] = "grid2_list2_0";
list2[| 1] = "grid2_list2_1";
list1[| 0] = "grid2_list1_0";
list1[| 1] = list2;

grid2[# 0, 0] = list1;
(see screenshot)
What are you expecting to see differently? The list pointers are stored in both grids so if you change the contents of those lists both grids will show the same thing.
 

TheouAegis

Member
You're modifying the values of list1 and list2, you're not modifying the values of the grids. Like , they ssaid, it's working exactly as you have coded, you're just not writing the correct code.
 
C

Catastrophe

Guest
If what these guys have written has confused you and you just want to copy some data, you can still do it. You'll just need to double for loop your ds_grid, create a new ds_list in each spot for the new grid and loop through the the grid's ds_lists and copy those values into the new ds_lists. Triple for loops are everyone's friend. Or instead of the third for loop, do a create and copy the old list.
 
Last edited by a moderator:
Top