GMS 2 [SOLVED] Placing Tiles on layers in GML

T

Tempus

Guest
Hello together

I try to place a tilemap in GML in game maker 2 but I can’t figure it out how to program it. I read the docs for game maker 2 but I it doesn´t help me.

I have programed a random dungeon with a ds_grid.

The ds_grid looks like

000000
011110
000010
011110
000000

1 -> wall => object on separate layer (I have made an obj_wall)
0 -> floor => tile on separate layer (I have made a tileset with 2*5 different tiles)
Tiles and obj_wall are 32*24 Pixels

Now i go through the ds_grid with a for next loop.
If it is a floor
· take a tile form the tileset (a tile from tileset position 2; 3).
· multiply the position on the ds_grid with the height and width of the tile.
· Place the tile on the layer for the floor
If it is a wall
· Take the obj_wall
· multiply the position on the ds_grid with the height and width of the obj_wall.
· Place the tile on the layer for the wall.

Can you give me a code example how to create the layers and place a specific tile on the tile_layer in GML?

Thanks
 

Perseus

Not Medusa
Forum Staff
Moderator
You can use tile_set_index() to modify the tile data to define the position of the tile in the tileset. In GMS 2, we've got "empty" tiles, so you'll have to get the tile data using tilemap_get_at_pixel() or tilemap_get() before you modify it using the said function. Once that is done, you can use timemap_set_at_pixel() or tilemap_set() to modify the properties of the empty tile.

Code:
var lay = layer_get_id("tile_layer");
var tile_map = layer_tilemap_get_id(lay);
for (var i = 0; i < ds_grid_width(grid); i += 1) {
   for(var j = 0; j < ds_grid_height(grid); j += 1) {
      var num = grid[# i, j];
      if (num == 0) {
         var data = tilemap_get_at_pixel(tile_map, i * 32, j * 24);
         tile_set_index(data, 6);
         tilemap_set_at_pixel(tile_map, data, i * 32, j * 24);
      }
      else if (num == 1) {
         instance_create_layer(i * 32, j * 24, "Instances", obj_Wall);
      }
   }
}
The code might be a bit difficult for you to follow if you don't know tilemaps work yet, so I'll suggest using some tutorials. A number of those on YouTube and around the forums.
 
T

Tempus

Guest
Thanks a lot!
This is exactly what I needed.

Do I understand it right?
tilemap_get_at_pixel() -> does this say to the computer: look at this place (in the room on the given layer) we´re gonna do something with it. If there is a tile we can manipulate it (like flip it). If there is no tile we can place one.

What I don´t understand is:
I tell him hey here is the tile_layer and look at positon x,y (var data = tilemap_get_at_pixel(tile_map, i * 32, j * 24);)
Then take numer 6 of the tileset (tile_set_index(data, 6);)
and place it at the position x,y. (tilemap_set_at_pixel(tile_map, data, i * 32, j * 24);)

Where do I say the computer which tilesets he has to use?
 
T

Tempus

Guest
Hi,

i don´t get it...

this is my code
upload_2017-2-20_21-21-55.png

All i get is a blank screen for the tiles. No tile is drawn
The obj are drawn and working as they should.
 
No tiles are ever set because you're checking for (num < 0). But you want to check for (num == 0)
Also, you could (but don't have to) switch to setting the cells directly instead of getting them by x/y coords. So that would be without the _at_pixel and *32 and *24. Personally, I would prefer that because you're basically just turning a grid into another grid-like structure.
 
T

Tempus

Guest
The num<0 is ok. my grid has negative numbers for the floor.
so the negative num is not the problem.

can you give me a code example for your solution?

Code:
var lay = layer_get_id("floor");
var tile_map = layer_tilemap_get_id(lay);

for (var i = 0; i < ds_grid_width(grid_2); i += 1) {
   for(var j = 0; j < ds_grid_height(grid_2); j += 1) {
      var num = grid_2[# i, j];
      if (num < 0) {
         var data = tilemap_get_at_pixel(tile_map, i * 32, j * 24);
         tile_set_index(data, 3);
         tilemap_set_at_pixel(tile_map, data, i * 32, j * 24);   
      }
      else if (num >= 1) {
      }
   }
}
 
Make sure there's an actual tileset assigned to the tilemap. Either in the IDE or through code.
Code:
var tile_map = layer_tilemap_get_id(layer_get_id("floor"));
tilemap_tileset(tile_map, tileset_name_here)

for (var i = 0; i < ds_grid_width(grid_2); i++) {
   for (var j = 0; j < ds_grid_height(grid_2); j++) {
      var num = grid_2[# i, j];
      if (num < 0) {
         var data = tilemap_get(tile_map, i, j);
         tile_set_index(data, 3);
         tilemap_set(tile_map, data, i, j); 
      }
      else if (num >= 1) {
      }
   }
}
 
T

Tempus

Guest
I´m sorry. still no tiles.
"ts_floor" is assigned to the layer "floor" as shown in my previous screenshot ans i can place tiles in the room editor.
i can´t see the mistake i make...
 
Top