Top-Down Dungeon Map: Procedural Tiles with TileMapLayer?

I’d like to create a large 2D dungeon map with a top-down view, where the tiles are procedurally drawn and positioned.
Nothing too complex: the tiles are simple polygons with solid colors and a darker border.

The AI (Chatgpt, Gemini, etc.) suggests me to use the TileMap node or the newer TileMapLayer.
This seems like the best choice for performance reasons and because it makes integrating pathfinding, physics, etc., easier.

However, I can’t figure out how to do it procedurally.
TileMapLayer seems designed for manually placing tiles via the Godot editor, not for procedural generation.

Am I missing something? Any advice would be appreciated!

Note: I’m a beginner, I apologize if the question may seem stupid :relieved:

I wouldn’t advise trusting AI much; it can lead you down dead ends easily.

TileMap is deprecated, you almost certainly don’t want to use it.

TileMapLayer might be what you want here, depending on what you mean by the tiles being simple polygons. TileMapLayer will do square, diamond or hex tiles (and possibly others, I can’t recall?), on a regularly tessellated grid. If you want arbitrary polygons, TileMapLayer probably isn’t suitable.

If you can work with a regularly tessellated grid, TileMapLayer has a set_cell() call that lets you set tiles at runtime. It expands as needed, so you could generate the entire map all at once at level start, or you could generate new terrain in whatever direction the player is walking during the game.

1 Like

From what I understand, set_cell() tells the TileMapLayer which tiles (from its TileSet) it has in its “collection” - the one you see at the bottom of the Godot editor. What I’m trying to do is grab a specific tile (cell) from a specific TileSet within that TileMapLayer’s collection at runtime and actually place it on the visible game map (the one the player sees)

set_cell() on TileMapLayer does what you want.

void set_cell(coords: Vector2i, source_id: int = -1, atlas_coords: Vector2i = Vector2i(-1, -1), alternative_tile: int = 0)```

coords is the position of the tile to be set in the layer

source_id is the tileset index

atlas_coords is where the tile is in the tileset

I’m using this for procedurally generated maps in my current game. Everything is being generated in code except the TileSet.

1 Like

You are absolutely right. I couldn’t see the tiles because I was assigning in set_cell() the global coords of the screen.
I didn’t realize that coords of set_cell() are multiplied by the tile_size of the TileSet :sweat_smile:

The local_to_map() and map_to_local() methods on TileMapLayer may be of interest to you here; they convert between world coordinates and tile coordinates.

1 Like