How to draw a randomly generated tilemap?

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

I’m new to Godot and have just started working through the beginner tutorials to get a feel for how things work. My objective is to port a couple of 2D ‘world generation’ routines that I’ve written in other languages. They generate data in the form of a 2D arrays of objects, describing the tile/spritesheet frame, movement speed, passability etc. Pretty much like a random map in a ‘roguelike’.

I’m sure bringing the code into GDScript wouldn’t be too difficult but what type of node should I attach the script(s) to and how would I access the array from elsewhere when it comes to drawing the tiles? Would it make sense to put the world generation into its own scene and if so can variables be accessed from other scenes or do they need to be global (if such a concept exists, I haven’t got as far as learning about scope in Godot yet!).

I’ve skimmed through this page Using tilemaps — Godot Engine (3.0) documentation in English but that deals with pre-defined/hand-drawn maps. I also glanced at a Godot roguelike ‘tutorial’ on github but it seemed to be a bunch of source files rather than any sort of explanation.

Any help appreciated :smiley:

:bust_in_silhouette: Reply From: Zylann

You can use TileMap as a visual and physical representation of your 2D world grid (since it handles rendering and collision, amongst a few other things). For other properties such as “passability” or “movement speed”, you can store them in a grid you make yourself in your script, which you will size the same as the tilemap (or use chunks if it’s infinite).

When you make a TileSet, each tile will be given a name and an ID. Godot uses those IDs for storage in the grid (you can get ID from name with tile_set.find_tile_by_name()).

Then, it is possible to set tiles of the tilemap from code, using set_cell: TileMap — Godot Engine (3.0) documentation in English

Thanks for that, sounds promising, I’ll read up on it and experiment!

picnic | 2018-06-22 17:40