GMS 2 Determining the Specific Tile From a Tileset

Cofefe

Member
So, suppose you were working on a tile collision engine. If you have a tileset with three tiles in it, is there a way you could use tilemap_get_at_pixel() to determine which specific tile from within that tileset it is at that pixel? I tried something with & tile_index_mask, but it didn't work properly. I think the main reason is probably because I don't properly understand or even know really what the tile index is or how to interpret it. :p

Any help with this subject would be greatly appreciated! Thanks!
 

Cofefe

Member
Hi TheouAegis!

Yes, I thought that would work, and I had already tried that. Here is a section of my code to get the jist of it:

I have several scripts for checking the points in my player's collision box, they're all quite similar, but one of which is tile_place_right. tl is the tilemap id, skew is a variable I can use to shift the collision left or right if I want to.
Code:
var tl, skew, crt, crc, crb;
tl = argument0;
skew = argument1;

crb = tilemap_get_at_pixel(tl, bbox_right + skew, bbox_bottom) & tile_index_mask;
crc = tilemap_get_at_pixel(tl, bbox_right + skew, y) & tile_index_mask;
crt = tilemap_get_at_pixel(tl, bbox_right + skew, bbox_top) & tile_index_mask;

if(crb != 0) return crb;  //This is the only point where tileID is relevant, all other collision points behave the same regardless of what the tile is.
if(crc != 0) return 1;
if(crt != 0) return 1;
return 0;
And in my horizontal_tile_collisions()
Code:
switch(tile_place_right(tile, 0))
    {
        case 1:
            x = (bbox_right & ~15) - 1 - sprite_bbox_right;
            xsp = 0;
            break;
           
        case 2:
            var localx = x - (bbox_right & ~15);
            y -= localx;
            break;
           
        default:
            //Nothing
            break;
    }
I thought tilemap_get_at_pixel(tl, bbox_right + skew, bbox_bottom) & tile_index_mask would return 0 for no tile, 1 for the first tile, and 2 for the second tile. Based on my game's behavior it seems this is not the case.

EDIT
tile and sprite bbox left are defined earlier in the create event as
Code:
alarm_set(0, grav);
health = 100;

///Tile Variables
tile = layer_tilemap_get_id("Collisions");
sprite_bbox_left = sprite_get_bbox_left(sprite_index) - sprite_get_xoffset(sprite_index);
sprite_bbox_right = sprite_get_bbox_right(sprite_index) - sprite_get_xoffset(sprite_index);
sprite_bbox_top = sprite_get_bbox_top(sprite_index) - sprite_get_yoffset(sprite_index);
sprite_bbox_bottom = sprite_get_bbox_bottom(sprite_index) - sprite_get_yoffset(sprite_index);
 

TheouAegis

Member
I thought tilemap_get_at_pixel(tl, bbox_right + skew, bbox_bottom) & tile_index_mask would return 0 for no tile, 1 for the first tile, and 2 for the second tile. Based on my game's behavior it seems this is not the case.
You may need to set your mask index. If your tile set contains less than 4 sprites, you can use

tilemap_set_mask(tile,3);

See if that resolves it.

Edit: Just a reminder, you can use almost any value for the second argument in tilemap_set_mask(), but unless you're psychotic, you should only use numbers from the set {1,3,7,15,31,63,127,255,511,1023,2047}.
 
Last edited:
Top