0 votes

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,

~FortunePilot

in Engine by (53 points)

2 Answers

+2 votes
Best answer

It is possible, using the TileMap functions: https://docs.godotengine.org/en/stable/classes/class_tilemap.html#tilemap

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 https://docs.godotengine.org/en/stable/classes/class_tileset.html#class-tileset-method-find-tile-by-name

by (27,952 points)
selected by

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

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.

+2 votes

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.

by (10,327 points)

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.