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

mp_grid_add_cell is adding cells from unwanted tilemap

Bentley

Member
Hello. I'm using an mp_grid and marking cells forbidden based on where I placed tiles in the room. I only want to mark tiles forbidden from the "Collisions" tilemap, but, for some reason, cells from the "Items" tilemap are also marked forbidden.
Code:
global.grid = mp_grid_create(0, 0, COLS, ROWS, TS, TS);

var lay_id, tilemap_id;
lay_id = layer_get_id("Collisions");
tilemap_id = layer_tilemap_get_id(lay_id);

for (var i = 0; i < COLS; i++)
{
    for (var j = 0; j < ROWS; j++)
    {
        if (tilemap_get_at_pixel(tilemap_id, i * TS, j * TS) != 0) // FIX: This line includes the "Items" tilemap
        {
            mp_grid_add_cell(global.grid, i, j);
        }
    }
}
The above code keeps Pacman from entering cells with tiles from the "Items" tilemap".

Temporary Fix (runs below the above code)
Code:
// Unmark cells that I accidently marked forbidden
var lay_id, tilemap_id;
lay_id = layer_get_id("Items");
tilemap_id = layer_tilemap_get_id(lay_id);
for (var i = 0; i < COLS; i++)
{
    for (var j = 0; j < ROWS; j++)
    {
        if (tilemap_get_at_pixel(tilemap_id, i * TS, j * TS) == TILE_PELLET)
        {
            mp_grid_clear_cell(global.grid, i, j);
        }
    }
}
Does anyone know where I'm going wrong? I don't know why the loop also marks cells forbidden from the "Items" tilemap.

Thanks for reading.
 
Last edited:

Rob

Member
I can't see the problem in the code you posted. It looks very similar to the code I'm using (which works as intended). I actually have collisions on both layers and I use an array to see if the player can move to that cell or not:

1:Can't Move
0:Can Move

Code:
if (room == rm_world_map) || (room = rm_test){
    lay_id = layer_get_id("Tiles_Floor");
    map_id= layer_tilemap_get_id(lay_id);

    for (var yy = 0; yy < vcells; yy ++){
        for (var xx = 0; xx < hcells; xx ++){
            if (tilemap_get(map_id, xx, yy) == 1){
                a_grid[xx, yy] = 0;
            }else{
                a_grid[xx, yy] = 1;  
            }
        }
    }

   
    lay_id = layer_get_id("Tiles_Terrain");
    map_id= layer_tilemap_get_id(lay_id);

    for (var yy = 0; yy < vcells; yy ++){
        for (var xx = 0; xx < hcells; xx ++){
            if (tilemap_get(map_id, xx, yy) == 3){
                a_grid[xx, yy] = 1;  
            }
        }
    }
   
}
 
M

MirthCastle

Guest
try changing the names of your variables there.. I think tilemap_id is a built-in variable.

also - do you use pixel perfect movement?

just use tilemap_get if you don't (save all those maths) and you can just use the I and J to test a location.

check your item objects - make sure they are not putting themselves on the collision layer, or you added an invisible sprite to the collisions layer by accident.
 

Jakylgamer

Member
try tossing it in a script
script - return_tile:
Code:
var lay_id = layer_get_id(argument[0]);
var map_id = layer_tilemap_get_id(lay_id);
var data = tilemap_get_at_pixel(map_id,argument[1],argument[2])
return data;
then using like this
Code:
for (var i=0; i< width; i++) {
   for(var j=0; j< height; j++) {
       if return_tile_index("Collisions",i*TS,j*TS) !=0 {
           mp_grid_add_cell(global.grid,i,j);
       }
       else {
          //clear it so there isnt any unwanted
           mp_grid_clear_cell(global.grid,i,j)
       }
   }
}
this works for me
 
Last edited:
try tossing it in a script
script - return_tile:
Code:
var lay_id = layer_get_id(argument[0]);
var map_id = layer_tilemap_get_id(lay_id);
var data = tilemap_get_at_pixel(map_id,argument[1],argument[2])
return data;



then using like this
Code:
for (var i=0; i< width; i++) {
   for(var j=0; j< height; j++) {
       if return_tile_index("Collisions",i*TS,j*TS) !=0 {
           mp_grid_add_cell(global.grid,i,j);
       }
       else {
          //clear it so there isnt any unwanted
           mp_grid_clear_cell(global.grid,i,j)
       }
   }
}
this works for me

UPDATE 2: tilemap_get_at_pixel you clown!! got it working now. It's too late in the day....

for those future beings out there wondering where:-

Code:
if (tilemap_get_at_pixel(mapid, i * TILESIZE, j * TILESIZE) != 0) {

UPDATE: I got the oBlock to work using - mp_grid_add_instances(global.grid, oBlock, 1); but still want the tiles in my tilemap added like this...



Late to this but been trying to create my own tilemap collision using mp_grid_add_cell. Was trying to use tile_index != 0 but tried using your variation but still not getting any result for myself.

My object either wanders off right or doesn't move at all, at present it isn't moving.

Only time I can get my object to move is if I don't try to use mp_grid_add_cell()

Anyone able to see what's happening?

GML:
//Macro
#macro TILESIZE 32


//oGrid Room Start Event

var rW = room_width >> 5;
var rH = room_height >> 5;

global.grid = mp_grid_create(0,0,rW,rH,TILESIZE,TILESIZE);
global.aipath = path_add();

var layid = layer_get_id("Collision");
var mapid = layer_tilemap_get_id(layid);


for (var i = 0; i < rW; i++) {
    for (var j = 0; j < rH; j++) {
        //var _tile = tilemap_get(mapid, i, j);
        //var _index = tile_get_index(_tile);
        //if (_index != 0) {
        if (tilemap_get(mapid, i * TILESIZE, j * TILESIZE) != 0) {
            //Mark Tile as Forbidden to global.grid
           mp_grid_add_cell(global.grid, i, j);
        } else {
            //Mark Empty
            mp_grid_clear_cell(global.grid, i, j);
        }
    }
}

//oGrid Game End Event

mp_grid_destroy(global.grid);

// oMoving Step Event

movespeed = 1;

if (mp_grid_path(global.grid, global.aipath, x, y, oStationary.x, oStationary.y, 1)) {
    path_start(global.aipath, movespeed, path_action_stop, false);

//Even tried using an object in the oGrid Room Start Event:

with (oBlock) { mp_grid_add_cell(global.grid, x >> 5, y >> 5); }
}
In either case, tilemap or oBlock, nothing is working, with the oBlock, the oMoving just walks right through it. oBlock is ticked as solid.

I know this post is a few years old now and GM has updated several times since. Not sure if the way code is written above is out of date or to be written differently.

TIA!
 
Last edited:
UPDATE 2: tilemap_get_at_pixel you clown!! got it working now. It's too late in the day....

for those future beings out there wondering where:-

Code:
if (tilemap_get_at_pixel(mapid, i * TILESIZE, j * TILESIZE) != 0) {

UPDATE: I got the oBlock to work using - mp_grid_add_instances(global.grid, oBlock, 1); but still want the tiles in my tilemap added like this...



Late to this but been trying to create my own tilemap collision using mp_grid_add_cell. Was trying to use tile_index != 0 but tried using your variation but still not getting any result for myself.

My object either wanders off right or doesn't move at all, at present it isn't moving.

Only time I can get my object to move is if I don't try to use mp_grid_add_cell()

Anyone able to see what's happening?

GML:
//Macro
#macro TILESIZE 32


//oGrid Room Start Event

var rW = room_width >> 5;
var rH = room_height >> 5;

global.grid = mp_grid_create(0,0,rW,rH,TILESIZE,TILESIZE);
global.aipath = path_add();

var layid = layer_get_id("Collision");
var mapid = layer_tilemap_get_id(layid);


for (var i = 0; i < rW; i++) {
    for (var j = 0; j < rH; j++) {
        //var _tile = tilemap_get(mapid, i, j);
        //var _index = tile_get_index(_tile);
        //if (_index != 0) {
        if (tilemap_get(mapid, i * TILESIZE, j * TILESIZE) != 0) {
            //Mark Tile as Forbidden to global.grid
           mp_grid_add_cell(global.grid, i, j);
        } else {
            //Mark Empty
            mp_grid_clear_cell(global.grid, i, j);
        }
    }
}

//oGrid Game End Event

mp_grid_destroy(global.grid);

// oMoving Step Event

movespeed = 1;

if (mp_grid_path(global.grid, global.aipath, x, y, oStationary.x, oStationary.y, 1)) {
    path_start(global.aipath, movespeed, path_action_stop, false);

//Even tried using an object in the oGrid Room Start Event:

with (oBlock) { mp_grid_add_cell(global.grid, x >> 5, y >> 5); }
}
In either case, tilemap or oBlock, nothing is working, with the oBlock, the oMoving just walks right through it. oBlock is ticked as solid.

I know this post is a few years old now and GM has updated several times since. Not sure if the way code is written above is out of date or to be written differently.

TIA!
The tile map is based on cells, not pixels, so don’t multiply by TILESIZE when getting or setting them (you would only do that for tilemap_get_at_pixel()).
 
Top