TileMap questions

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Thrallherd

Hello, I am currently coding a hex grid, so I am using a TileMap with a half offset. I have two questions.

  1. Given tile [p, q], how is its index calculated?

  2. I opened the tilemap’s tscn file and I could see that the variable tile_data dictates which tiles are filled and how. How can I access tile_data inside a function?

Thank you.

:bust_in_silhouette: Reply From: njamster
  1. Given tile [p, q], how is its index calculated?

What do you mean by “index”? A cell in the TileMap is described by it’s position (x, y) and is associated with a unique tile id that determines which image you see. These id’s are simply counted up, so the first tile in the list is id 0, the second tile is id 1 and so on. If you delete tiles from your TileSet, there will remain gaps of unused ids.

  1. I opened the tilemap’s tscn file and I could see that the variable tile_data dictates which tiles are filled and how. How can I access tile_data inside a function?

That depends on what you want to do! If you are interested in all filled cells (no matter the tile), use get_used_cells. If you are only interested in a specific tile, you have to use get_used_cells_by_id and pass the tile’s id. Both of these will only give you a list of cell positions. If you interested in the tile id that a position is associated with, you can use get_cell (which expects x and y as arguments) or get_cellv (which expects a Vector2 describing the position as its argument). And if you’re using an Atlas- or AutoTile-tile you will need to use get_cell_autotile_coord.

Check out the documentation for further options.