How to dynamically build tilemaps?

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

Hi all,

I’ve coded a random dungeon generator that creates a 2d matrix of 0’s and 1’s to represent void tiles and floor tiles respectively. I now want to create a visual representation of each dungeon level, and I figured a tilemap would best fit my needs, both with respect to visuals and collisions and performance-wise.

Is it possible to dynamically build a tilemap (visuals and collisions) at runtime, and if so, how?

Kind regards,


:bust_in_silhouette: Reply From: Zylann

It is possible, using the TileMap functions: TileMap — Godot Engine (stable) documentation in English

Get access to the tilemap node from your script, and use set_cell(x, y, tile_id) to place tiles.
tile_id is an internal number generated when you make the tileset, which you can obtain by name using var tile_id = tilemap.tile_set.find_tile_by_name("dungeon_wall") for example TileSet — Godot Engine (stable) documentation in English

This seems to work, but I can’t quite get the grasp of the naming of all the different tilemaps/tilesets/tilename to get the tile_id. Is there a mnemonic for this?

So far I’m testing with a single image resource, named “asf1.png”. I have created my tilemap node called “Dungeon”, and inside the tile selector (I assume it’s called, on the left of the inspector tab when I select my tilemap node) there’s one tile called “asf1.png 0”.

How would I correctly replace tilemap, tile_set, and the name of the tile?

PS when I get my tile_id now, it returns -1

Thanks for the help!


FortunePilot | 2020-04-23 23:15

Nevermind, found it!

For anyone who is having the same question:

in tilemap.tile_set.find_tile_by_name(tile_name)
tilemap is the node of the tilemap in the scene tree and tile_name the name of the tile in the tile selector once you select your tilemap in the scene tree.

FortunePilot | 2020-04-23 23:20

:bust_in_silhouette: Reply From: njamster

First you need to create a TileSet. That will give you the opportunity to define both the visuals and collision shapes of your tiles. Then you can assign each cell in your TileMap a tile with set_cell(x, y, tile_id) where x and y are the coordinates of the cell and tile_id is an unique integer describing which tile to use there.

Thanks for the help, it works now!
Helps me a lot too now that I killed two birds with one stone: I got exactly the collision behavior I was looking for! :))

Kind regards,


FortunePilot | 2020-04-23 23:21