• 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!
  • Hello [name]! Thanks for joining the GMC. Before making any posts in the Tech Support forum, can we suggest you read the forum rules? These are simple guidelines that we ask you to follow so that you can get the best help possible for your issue.

Windows Placing Tiles in Rooms in GM:S 2

D

Dragon Ferocity

Guest
Okay, I'm not exactly a noob here, but I can't figure out how to place tiles in the room using code.
Now before you post anything, I'm not looking for how to do tile collisions here, just how to draw tiles in a room.
If any of my code snippets are wrong below, please tell me.

I've looked through the manual, and it just confuses me.

To start with, I'm creating a new layer, and then creating a new tilemap on that new layer
Code:
GROUND_LAYER = layer_create(-10, "Ground_Tiles");
GROUND_LAYER_TILES = layer_tilemap_create(GROUND_LAYER, 0, 0, tl_dirt, room_width, room_height);
Then, I'm wanting to draw multiple tiles in the room, and not place them by hand, because my levels need to be procedural generated.

Now here's where I get the info of the tilemap. At least i think that's what this code does? I'm really not sure what the point of this code is, but I saw it in the manual, so I figured I needed it.

Code:
var dirtLayer = layer_get_id("Ground_Tiles");
var dirtMap = layer_tilemap_get_id(dirtLayer);
var dirtData = tilemap_get(dirtMap, 0, 0); //What does this even do? What's the point of this function?
Now this next piece of code is where the tiles are supposed to be place on the map that is created globaly.
Code:
for (var i = 0; i < numRows; i++) {
  var numX = array_length_2d(xx, i);
  for (var k = 0; k < numX; k++) {
      tilemap_set_at_pixel(dirtMap, dirtData, xx[i, k], yy[i, k]);
  }
}
xx is a global array that stores x values for each tile that I want to place, similarly yy is the y values.
The value of numRows is 300.

However, when I run this code, I get an error in the console
Code:
tilemap_get() - couldn't find specified tilemap
Code:
tilemap_set_at_pixel() - couldn't find specified tilemap
I literally have no idea what is wrong, or what I'm missing. The documentation doesn't help me either, and after searching around on the forums, I can't find any help for how to place tiles in a room using code. I can only find how to do tile collisions, which I don't want to do at this moment.

Please someone help me, and if there is a tutorial that explains how you actually place tiles in a room using code that would be marvelous.

PS, I also looked through the dungeon demo, it doesn't place tiles using code from what I could tell. If it did, I couldn't follow what the heck the code was doing.

Thanks,
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
If you don't want to set rotation or flip or anything then setting a tile is relatively simple. Get the layer ID, get the tilemap ID then use "tilemap_set" or "tilemap_set_at_pixel" to set the tile to the index you want. If you need to rotate the tile or something then first you need to get the tiledata "blob" using tilemap_get... so:

Code:
GROUND_LAYER = layer_create(-10, "Ground_Tiles");
GROUND_LAYER_TILES = layer_tilemap_create(GROUND_LAYER, 0, 0, tl_dirt, room_width, room_height);
for (var i = 0; i < (room_width / CELL_SIZE); i++) // Note that CELL_SIZE is the size in pixels of whatever the tileset you use is.
{
for (var j = 0; j < (room_height / CELL_SIZE); j++)
    {
    tilemap_set(GROUND_LAYER_TILES, 1, i, j);
    }
}
The above code will populate the entire tilemap with the tile indexed at position 1 in the tileset (position 0 is the empty tile and so used to delete the tile from the map). If you need to manipulate the tiledata in any way then you'd do:

Code:
GROUND_LAYER = layer_create(-10, "Ground_Tiles");
GROUND_LAYER_TILES = layer_tilemap_create(GROUND_LAYER, 0, 0, tl_dirt, room_width, room_height);
for (var i = 0; i < (room_width / CELL_SIZE); i++)
{
for (var j = 0; j < (room_height / CELL_SIZE); j++)
    {
    var _td = tilemap_get(GROUND_LAYER_TILES, i, j);
    _td = tile_set_index(td, 10);
    _td = tile_set_rotate(td, true);
    tilemap_set(GROUND_LAYER_TILES, _td, i, j);
    }
}
Hope that helps!
 
Last edited:
D

Dragon Ferocity

Guest
That actually helps a lot. Thanks.

One more question though. Is tilemap_set supposed to draw the tile on the layer? tilemap_set_at_pixel does draw the tile on the layer, but tilemap_set appears to not be doing so.
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
Tilemap set and tilemap set at pixel should both do the same thing. One sets the tile using the cell positions, while the other sets the tile using room positions, but they should both set the tile to the correct index and draw it... So, say you have tiles that are 32x32 and want to set the tile at position 64x64 to tile index 2. You could do it with either of the following:

Code:
tilemap_set_at_pixel(TileMap, 2, 64, 64);
tilemap_set(TileMap, 2, 2, 2);
I say this because if tilemap_set isn't working then I would suspect that maybe you are using pixel position instead of cell position...?
 
D

Dragon Ferocity

Guest
Okay, yes. That makes a whole lot more sense. Thanks.

So, if you use draw_tile, is that same as just drawing say a sprite somewhere in the room? It doesn't actually place a tile, just it's picture?
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
Yes, exactly. draw_tile simply draws it, but it's NOT a "tile", it's simply drawing a tile. :)
 
Top