1. Hey! Guest! The 35th GMC Jam will take place between November 28th, 12:00 UTC - December 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice

Copy a ds_grid to another ds_grid

Discussion in 'Programming' started by Jaydabblju, Nov 9, 2019.

  1. Jaydabblju

    Jaydabblju Member

    Joined:
    Oct 12, 2019
    Posts:
    9
    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.
     
  2. rIKmAN

    rIKmAN Member

    Joined:
    Sep 6, 2016
    Posts:
    4,646
    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.
     
  3. Jaydabblju

    Jaydabblju Member

    Joined:
    Oct 12, 2019
    Posts:
    9
    As I wrote I already tried this :)
     
  4. rIKmAN

    rIKmAN Member

    Joined:
    Sep 6, 2016
    Posts:
    4,646
    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
    
     
  5. Jaydabblju

    Jaydabblju Member

    Joined:
    Oct 12, 2019
    Posts:
    9
    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)
     

    Attached Files:

  6. nacho_chicken

    nacho_chicken Member

    Joined:
    Jun 21, 2016
    Posts:
    404
    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.
     
    Nocturne likes this.
  7. Rob

    Rob Member

    Joined:
    Jul 12, 2016
    Posts:
    687
    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.
     
    Nocturne likes this.
  8. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    7,018
    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.
     
    Nocturne likes this.
  9. Catastrophe

    Catastrophe Member

    Joined:
    Sep 22, 2019
    Posts:
    106
    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: Nov 10, 2019 at 11:33 AM

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice