A tile map is a collection of tile set tiles that are all considered to be at the same depth within the room.
It can be visualised as a grid where each cell stores some tile data: the index of the tile that should be drawn at that cell, along with how it should be drawn (rotated, flipped and/or mirrored).
Each tile map has its own unique element ID and properties.
When working with tiles, you use a few things that are related to one another as follows:
NOTE In the Room Editor you are limited to a single tile map per layer, which is created when you add a Tile Layer, but when working with them in code, you can have multiple tile maps assigned to a layer.
A "blob" of tile data is stored as a 32 bit value. It is made up of 19 bits that store the tile index in the tile set (bits 0 to 18) plus a few extra bits to indicate how the tile data should be drawn: bit 28 stores the mirror bit, bit 29 stores the flip bit, bit 30 stores the rotate bit and bit 31 is unused. Bits 19 to 27 are also unused. You can also use these unused bits to store your own data.
For more convenient access to certain bits you can use the values of the tile mask constant:
Constant | Description |
---|---|
tile_rotate | Used to set/get the rotate bit of a tile data blob. |
tile_mirror | Used to set/get the mirror bit of a tile data blob. |
tile_flip | Used to set/get the flip bit of a tile data blob. |
tile_index_mask | A special constant that is for "and"-ing with the tile data blob to extract the 19 bits of the tile index. |
You can get tile data at a specific map cell using tilemap_get or tilemap_get_at_pixel and set it using tilemap_set or tilemap_set_at_pixel.
You have the possibility of adding your own bit mask to a tile map in order to use bits of the tile data "blob" for your own use, although this is for advanced users only as you will be masking off bits that are reserved for the tile index. There are 19 bits reserved for your tile indices, but if you only use 8 of them, then you have eleven bits left over that can be used to create a custom mask. This mask can then be used and checked in code to create custom tile collisions or whatever.
The bit mask is "and"-ed & against the tile data when the tile map is drawn (so it doesn't affect any of the other functionality), meaning that bits which aren't in the mask are ignored, but can still be read and written to by the user for their own purposes.
However, you would normally not need to create your own tile data blobs, and would instead use the function tilemap_get to get the tile data, then you would manipulate it using the functions for Editing Individual Tiles, and then you would set it again using tilemap_set.
NOTE Aside from a mask per tile map, there is also a global one. The two are bitwise "and"-ed & together internally by GameMaker before being used for drawing.
To get an initial index mask value you take the number of tiles in the tile set and subtract 1, so a 16x16 tile set has 256 tiles and a mask value of 255 (or $ff in hexadecimal). If the tile set has a number of tiles that's not a power of two, then round up to the nearest power of two to get the mask, for example a tile set that is 20x20 has 400 tiles, which you would round up to 512, and so get a mask index value of 511 ($1ff in hexadecimal). Apart from the index mask, normally you would want to preserve the flip, rotate and mirror values that are assigned to a tile too, to help with that there are the constants under Tile Mask Constant that can be used. These can be bitwise "or"-ed | together with the index mask value to preserve those bits of data.
NOTE See Bitwise Operators for more information on how to work with binary and bit masks.