How to place tiles with gdscript?

I want to create a specific geometric shape using tiles, and I need to place these using code. However, I’m having trouble figuring out how to do this. I have already created a tilemap with a specified tileset. From the documentation, I believe that the set_cell function is the one I should use to place tiles on the map, but I’m not sure how to implement it.

It depends if you want the autotile or not.

Without autotile :

$TileMap.set_cell(layerid, position_in_tilemap, sourceid, atlas_coordinates)

where

  • layerid is the id of the layer you want to draw in.
  • position_in_tilemap is a vector2i defining the position where to draw the tile.
  • sourceid is the id of the tileset texture you want to use.
  • atlas_coordinates is a vector2i defining the position in the tileset texture you need to draw

With autotile :

$TileMap.set_cells_terrain_connect(layerid, list_of_cell_position, terrain_set_id, terrain_id);

where

  • layerid is the id of the layer you want to draw in.
  • list_of_cell_position is a list of vector2i defining the positions where to draw the tiles.
  • terrain_set_id is the id of the terrain set (the first thing you have to select when painting a terrain bitmask)
  • terrain_id is the id of the terrain in the terrain set (the second thing you have to select when painting a terrain bitmask)
1 Like