GameMaker Incrementing x & y positions not working as intended in for loop

W

Will

Guest
My level generator code looks like this:

Code:
randomize();
grid_size = irandom_range(3,4);
level_grid = ds_grid_create(grid_size,grid_size);
ds_grid_set_region(level_grid,0,0,grid_size - 1,grid_size - 1,-1);
for (var row = 0;row < ds_grid_width(level_grid);row ++)
{
    for (var col = 0;col < ds_grid_height(level_grid);col ++)
    {
        if level_grid[# row,col] = -1
        {
            var block = irandom_range(1,3)
            scr_tilemap((128) + (512*row),(128) + (320*col),block);
            level_grid[# row,col] = block;
        }
    }
}
The result looks like:

It calls scr_tilemap(x,y,block) to populate the positions within the level_grid with the appropriate pre-determined blocks. This is a very early and basic version, and this technically works as intended. I'm having some trouble though, and it's probably my own oversight, but I'm trying to separate the blocks by 128 on every side.

I tried the following code (adding the x_offset and y_offset vars):
Code:
randomize();
grid_size = irandom_range(3,4);
level_grid = ds_grid_create(grid_size,grid_size);
ds_grid_set_region(level_grid,0,0,grid_size - 1,grid_size - 1,-1);
var offset_x = 0;
var offset_y = 0;
for (var row = 0;row < ds_grid_width(level_grid);row ++)
{
    for (var col = 0;col < ds_grid_height(level_grid);col ++)
    {
        if level_grid[# row,col] = -1
        {
            var block = irandom_range(1,3)
            scr_tilemap((128*offset_x) + (512*row),(128 * offset_y) + (320*col),block);
            level_grid[# row,col] = block;
            offset_x ++;
            offset_y ++;
        }
    }
}
But this was my result:


Where do the x&y offsets need to be to keep them aligned? I don't want to put them in scr_tilemap, because these positions start at (0,0) and then are adjusted by the code to determine where to draw them.

If anyone can help, that would be greatly appreciated, and if you need more clarification let me know. This really should be a quick fix, but I've been staring for a while and can't figure it out.
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
You need to set the y offset and xoffset in different places, not in the same place. The x offset gets incremented only when the row gets incremented, and the y offset only when the columns get updates... so:

Code:
randomize();
grid_size = irandom_range(3,4);
level_grid = ds_grid_create(grid_size,grid_size);
ds_grid_set_region(level_grid,0,0,grid_size - 1,grid_size - 1,-1);
var offset_x = 128;
var offset_y = 128;
for (var row = 0;row < ds_grid_width(level_grid);row ++)
{
    for (var col = 0;col < ds_grid_height(level_grid);col ++)
    {
        if level_grid[# row,col] = -1
        {
            var block = irandom_range(1,3)
            scr_tilemap((128*offset_x) + (512*row),(128 * offset_y) + (320*col),block);
            level_grid[# row,col] = block;
            offset_y ++;
        }
    offset_x ++;
    offset_y = 0;
    }
}
:)
 
W

Will

Guest
EDIT: I got this working by removing offset_x & offset_y and just using row & col in their place. It worked just fine!

You need to set the y offset and xoffset in different places, not in the same place. The x offset gets incremented only when the row gets incremented, and the y offset only when the columns get updates... so:

Code:
randomize();
grid_size = irandom_range(3,4);
level_grid = ds_grid_create(grid_size,grid_size);
ds_grid_set_region(level_grid,0,0,grid_size - 1,grid_size - 1,-1);
var offset_x = 128;
var offset_y = 128;
for (var row = 0;row < ds_grid_width(level_grid);row ++)
{
    for (var col = 0;col < ds_grid_height(level_grid);col ++)
    {
        if level_grid[# row,col] = -1
        {
            var block = irandom_range(1,3)
            scr_tilemap((128*offset_x) + (512*row),(128 * offset_y) + (320*col),block);
            level_grid[# row,col] = block;
            offset_y ++;
        }
    offset_x ++;
    offset_y = 0;
    }
}
:)
Thanks for the response, quick and helpful as always! While this made it look better with the slight tweaks (I had to initialize the offset_x & offset_y to 0 to make it work properly, added the 128's in manually later), I get this:
Code:
randomize();
grid_size = irandom_range(3,4);
level_grid = ds_grid_create(grid_size,grid_size);
ds_grid_set_region(level_grid,0,0,grid_size - 1,grid_size - 1,-1);
var offset_x = 0;
var offset_y = 0;
for (var row = 0;row < ds_grid_width(level_grid);row ++)
{
    for (var col = 0;col < ds_grid_height(level_grid);col ++)
    {
        if level_grid[# row,col] = -1
        {
            var block = irandom_range(1,3)
            scr_tilemap(128 + (128 * offset_x) + (512*row),128 + (128 * offset_y) + (320*col),block);
            level_grid[# row,col] = block;
            offset_y ++;
        }
        offset_x ++;
        offset_y = 0;
    }
}

What I'm going for looks like this:


The best I've been able to do is getting the first column aligned, but I messed up and can't figure out how I did that again.
 
Last edited by a moderator:
Top