[SOLVED] Copying a smaller ds_grid into a region of a ds_grid woes

W

whale_cancer

Guest
Hello!
It seems like after a week or so of good progress without needing help, I've started to run into problems again. Bah! Anyway, I think this is me probably just making some small error somewhere. Any help appreciated!

I am trying to copy three (eventually five) small ds_grids read from an included file into a larger ds_grid. Through debugging, it seems as if the grids are being copied correctly? Or at least being referenced correctly? But when I use them to output the tile data they contain, they output tiles as if they were outputting background 0, with a top value of 0, and a left value of 0 (i.e. uninitialized ds_grid space).

My initial loading of the ds_grid data:

Code:
//Create mega-data structures here
global.tile_bgmap = ds_grid_create(32 * 27, 32 * 27);
global.tile_left = ds_grid_create(32 * 27, 32 * 27);
global.tile_top = ds_grid_create(32 * 27, 32 * 27);
global.tile_collision = ds_grid_create(32 * 27, 32 * 27);
global.tile_vision = ds_grid_create(32 * 27, 32 * 27);

//Spawn the player
//<!-- change to spawn at a marker
//instance_create(1 * 512, 1 * 512, obj_player);

for (ww = 0; ww < 27; ww += 1)
{
    for (hh = 0; hh < 27; hh += 1)
    {
        //an array that determines which chunks are loaded
        //global.chunk_loaded[ww, hh] = false;
      
        //load just the 0000 chunk for now
        if (ww = 00) && (hh == 00)
        {
            //load the chunk
            file = file_text_open_read('CHUNKS\0000.chunk')
  
            //
            temp_bg_grid = ds_grid_create(32, 32);
            temp_left_grid = ds_grid_create(32, 32);
            temp_top_grid = ds_grid_create(32, 32);
          
            //GFX grids
            temp_string = file_text_read_string(file);
            ds_grid_read(temp_bg_grid, temp_string);
            file_text_readln(file);
          
            temp_string = file_text_read_string(file);
            ds_grid_read(temp_left_grid, temp_string);
            file_text_readln(file);
          
            temp_string = file_text_read_string(file);
            ds_grid_read(temp_top_grid, temp_string);
            file_text_readln(file);
                                              
            //debug
            show_debug_message('---------------------------------------------------------------');
            for (w = 0; w < 32; w += 1)
            {
                for (h = 0; h < 32; h += 1)
                {
                    bg = ds_grid_get(temp_bg_grid, w, h);
                    left = ds_grid_get(temp_left_grid, w, h);
                    top = ds_grid_get(temp_top_grid, w, h);     
                    
                    show_debug_message('Temp grid value at '+string(w)+','+string(h)+' is '+background_get_name(bg)+' with left '+string(left)+' and top '+string(top))               
                } 
            }
            show_debug_message('Copying chunk '+string(ww)+string(hh)+' to Worldspace grid.');
          
            for (w = 0; w < 32; w += 1)
            {
                for (h = 0; h < 32; h += 1)
                {
                    bg = ds_grid_get(temp_bg_grid, w, h);
                    left = ds_grid_get(temp_left_grid, w, h);
                    top = ds_grid_get(temp_top_grid, w, h);
                  
                    ds_grid_set(global.tile_bgmap, w * ww, h * hh, bg)
                    ds_grid_set(global.tile_left, w * ww, h * hh, left)
                    ds_grid_set(global.tile_top, w * ww, h * hh, top)         
                    
                    show_debug_message('Setting grid at '+string(w*ww)+','+string(h*hh)+' to '+background_get_name(bg)+' '+string(left)+','+string(top))               
                } 
            }
            //destroy temp grids
            ds_grid_destroy(temp_bg_grid)
            ds_grid_destroy(temp_left_grid)
            ds_grid_destroy(temp_top_grid)
            //close the file
            file_text_close(file)
        }
    } 
}
You can see I use loops to output all the tile data. That can be found here. I can confirm that those values correspond to the correct tile values (they are just random tiles I threw down from different tilesets in my map editor for testing purposes).

Here is the code for creating the actual tiles (remember, this outputs tiles that are all the same and correspond to background asset 0, top 0, and left 0 values):
Code:
for (ww = 0; ww < 27; ww += 1)
{
    for (hh = 0; hh < 27; hh += 1)
    {
        //an array that determines which chunks are loaded
        //global.chunk_loaded[ww, hh] = false;
      
        //load just the starter cell
        if (ww = 00) && (hh == 00)
        {
            //GFX information
            for (w = 0; w < 32; w += 1)
            {
                for (h = 0; h < 32; h += 1)
                {
                    bg = ds_grid_get(global.tile_bgmap,  (ww * 32) + w, (hh * 32) + h);
                    left = ds_grid_get(global.tile_left, (ww * 32) + w, (hh * 32) + h);
                    top = ds_grid_get(global.tile_top,   (ww * 32) + w, (hh * 32) + h);
                  
                    show_debug_message('Placing tile at '+string(w*ww)+','+string(h*hh)+' to '+background_get_name(bg)+' '+string(left)+','+string(top)) 
                  
                    tile_add(bg, left, top, 16, 16, (ww * 512) + (w * 16), (hh * 512) + (h * 16), 1000000);
                } 
            }
        }
    }
}
The pastebin link also shows this debugging information as outputting 0, 0, 0.

Any idea what is going wrong? Any help greatly appreciated!

Edit: 0000.chunk file
 

jo-thijs

Member
I don't understand this part:
Code:
            for (w = 0; w < 32; w += 1)
            {
                for (h = 0; h < 32; h += 1)
                {
                    bg = ds_grid_get(temp_bg_grid, w, h);
                    left = ds_grid_get(temp_left_grid, w, h);
                    top = ds_grid_get(temp_top_grid, w, h);
                  
                    ds_grid_set(global.tile_bgmap, w * ww, h * hh, bg)
                    ds_grid_set(global.tile_left, w * ww, h * hh, left)
                    ds_grid_set(global.tile_top, w * ww, h * hh, top)         
                    
                    show_debug_message('Setting grid at '+string(w*ww)+','+string(h*hh)+' to '+background_get_name(bg)+' '+string(left)+','+string(top))               
                }
            }
Definitely if you also know both ww and hh are 0 when this is executed.
Why do you use w*ww and h*hh?
Shouldn't it be ww*32+w and hh*32+h or something like that?

Also, wouldn't using this function: https://docs.yoyogames.com/source/d...uctures/ds grids/ds_grid_set_grid_region.html
be way better?
 
W

whale_cancer

Guest
Definitely if you also know both ww and hh are 0 when this is executed.
I know ww and hh are both 0 because I am only loading one chunk out of a 27 x 27 chunk grid (no point in getting the whole system up and running if one chunk won't load properly).

Why do you use w*ww and h*hh?
Shouldn't it be ww*32+w and hh*32+h or something like that?
Edit: wait, let me look at that. I did say it was probably some small error...

YUP THIS WAS IT. I think I just went crazy trying to fix this and I just got developed a blindness for this part of the code. Thank you very much @jo-thijs!

Actually... probably. When I was looking through the ds_grid functions I only noticed the ones that would set a given region to have all the same value... I will try to rewrite using that, but I think the problem must be elsewhere, as my janky copy function seems to work based on the posted debugging information.
 
Last edited:
Top