Saving values in a ds_grid

Juanla

Member
Hello, I am trying rebuild an ds_grid from an .ini file string. But I don't know why my ds_grid gets initializated to 0 again.

My grid is an 9x9 cells grid. Each one of this cells have inside a 3x3 grid. The values saved inside are integers. I have this in my code:

GML:
    //Prepare an empty grid 9x9 with a 3x3 grid inside each cell
    var grid_to_return = ds_grid_create(9,9);
    ds_grid_clear(grid_to_return, ds_grid_create(3,3));

//VAL only for DEBUG
val = "1,2,3,0,0,0,0,0,0,1,0,0,0,5,0,0,0,9,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,4,0,6,0,8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,5,0,0,8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,0,5,0,7,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0";
    

    //Split the debug value in a large array of integers
    val = scr_string_split(",", val, true);
   
    //Create an independient index to get values of these array
    var index=0;

    //This first pair of loops go across the main grid (9x9)
    for (var row = 0; row < 9; row++) {
        for (var col = 0; col < 9; col++) {

            //This second pair of loops go across the child grid (3x3) inside each cell of the main grid
            for (var ii = 0; ii < 3; ii++) {
                for (var jj = 0; jj < 3; jj++) {

                    //Save the current "val" value usin the index counter inside the child cell in order
                    grid_to_return[# row, col][# ii, jj] = floor(real(val[index]));

                    //This is a help message that I was using to see if something goes wrong
                    show_debug_message(
                        "row: " + string(row) + " col: " + string(col) + " ii: " + string(ii) +
                        " jj: " + string(jj) + " val: " + string(val[index]) + " grid_val: " + string(grid_to_return[# row, col][# ii, jj])
                    );

                    //We increment index value to continue looping across
                    index++;

                }
            }
            //End of the second pairs of loops

        }
    }
//End of the first pairs of loops
In this case, I have the next messages:
GML:
row: 0 col: 0 ii: 0 jj: 0 val: 1 grid_val: 1 -> OK! "grid_val" have same value that "val"
row: 0 col: 0 ii: 0 jj: 1 val: 2 grid_val: 2 -> OK! "grid_val" have same value that "val"
row: 0 col: 0 ii: 0 jj: 2 val: 3 grid_val: 3 -> OK! "grid_val" have same value that "val"
row: 0 col: 0 ii: 1 jj: 0 val: 0 grid_val: 0
row: 0 col: 0 ii: 1 jj: 1 val: 0 grid_val: 0
row: 0 col: 0 ii: 1 jj: 2 val: 0 grid_val: 0
row: 0 col: 0 ii: 2 jj: 0 val: 0 grid_val: 0
row: 0 col: 0 ii: 2 jj: 1 val: 0 grid_val: 0
row: 0 col: 0 ii: 2 jj: 2 val: 0 grid_val: 0
row: 0 col: 1 ii: 0 jj: 0 val: 1 grid_val: 1 -> OK! "grid_val" have same value that "val"
row: 0 col: 1 ii: 0 jj: 1 val: 0 grid_val: 0
...
Anyway, when I tried read again this grid all values are gone and only have 0 in all cells. Below these for loops I have this code:
Code:
    var rs2 = "Grid:\n";
    for (var i = 0; i < 9; i++){
        for (var j = 0; j < 9; j++){
            var grid_notes_cell = grid_to_return[# i, j];
            rs2 += "i:" +string(i) + "j:" +string(j) + " -> ";
            for (var ii = 0; ii < 3; ii++){
                for (var jj = 0; jj < 3; jj++){           
                    var grid_notes_loop = string(grid_notes_cell[# ii, jj]) + " / ";
                    rs2 += grid_notes_loop;
                }
            }
            rs2 += " \n";
        }
    }
    show_debug_message(rs2);
And at this moment (next to the set loops) I have all with a value of zero.
Code:
i:0j:0 -> 0 / 0 / 0 / 0 / 0 / 0 / 0 / 0 / 0 /  --> This should be 1 /2 /3 / 0 / 0 / 0 / 0 / 0 / 0 / 
i:0j:1 -> 0 / 0 / 0 / 0 / 0 / 0 / 0 / 0 / 0 / --> This should be 1 /0 /0 / 0 / 5 / 0 / 0 / 0 / 9 / 
i:0j:2 -> 0 / 0 / 0 / 0 / 0 / 0 / 0 / 0 / 0 / 
i:0j:3 -> 0 / 0 / 0 / 0 / 0 / 0 / 0 / 0 / 0 / 
i:0j:4 -> 0 / 0 / 0 / 0 / 0 / 0 / 0 / 0 / 0 / 
i:0j:5 -> 0 / 0 / 0 / 0 / 0 / 0 / 0 / 0 / 0 / 
i:0j:6 -> 0 / 0 / 0 / 0 / 0 / 0 / 0 / 0 / 0 / 
i:0j:7 -> 0 / 0 / 0 / 0 / 0 / 0 / 0 / 0 / 0 / 
i:0j:8 -> 0 / 0 / 0 / 0 / 0 / 0 / 0 / 0 / 0 / 
i:1j:0 -> 0 / 0 / 0 / 0 / 0 / 0 / 0 / 0 / 0 / 
i:1j:1 -> 0 / 0 / 0 / 0 / 0 / 0 / 0 / 0 / 0 /
...
I don't understand why the first set in the grid is not persisted when I am out of the loops. Any idea? I am missing something?
 

Nidoking

Member
You're not creating 81 3x3 grids. You're only creating one 3x3 grid and putting its ID into every cell of your 9x9 grid.
 

TsukaYuriko

☄️
Forum Staff
Moderator
To elaborate, the argument you pass to ds_grid_clear is not vectorized. As in, if it is a function call, it will run exactly once (when it is passed to the function) and the return value of that single call is used throughout the entire body of the function. Loop through your grid in two dimensions and individually create grids in each cell instead.
 

Juanla

Member
You're not creating 81 3x3 grids. You're only creating one 3x3 grid and putting its ID into every cell of your 9x9 grid.
To elaborate, the argument you pass to ds_grid_clear is not vectorized. As in, if it is a function call, it will run exactly once (when it is passed to the function) and the return value of that single call is used throughout the entire body of the function. Loop through your grid in two dimensions and individually create grids in each cell instead.
Yes! You have Right, I think that I should sleep a little more when I have this problems :(

Thanks for the help!
 
Top