I’m trying to change tiles in a tile map with code, I know how to do it with $TileMap.set_cell() but how do I get it to auto tile? I've tried using $TileMap.set_cells_terrain_connect() but have been unable to get it to work.

This is my code:

#I start by running a function with a 2d array with values on each tile(I've used this with #$TileMap.set_cells_terrain_connect() so I know this works as intended)

#this is the code in the “Data” script:
var tile_types = {
	"void":-1,
	"water":2,
	"grass":1,
	"forest":3,
	"sand":0
}

#In my Tilemap script:
for x in range(0,new.size()):
   for y in range(0,new[0].size()):
      $TileMap.set_cells_terrain_connect(0, Vector2i(x,y), 0, Data.tile_types[new[0][0]["name"]],true)

Any ideas as to what I am doing wrong?

  • I figured it out and this is how the function works.

    The function loops threw all the Vector2i’s in an array that you provide it, then it changes the corresponding cells to a specific tile type that you set.

    If you provide it with too many Vector2i’s as I did it will not run or at least run slowly, so if this is a problem you can split your array of Vector2i’s into smaller chunks and run them in small groups.

    Also, don’t forget to set up your tilemap/terrain in the editor or it will not work properly.

    set_cells_terrain_connect(a,b,c,d,e)

    a: layer: int, this sets the layer to draw to.
    b: cells: array of Vector2i, add all the cells you wish to change on your tilemap
    c: terrain_set: int, This selects the set of tiles you wish to use.
    d: terrain: int, This selects the tile in the terrain_set you wish to draw.
    e: ignore_empty_terrains: bool = true, (I'm not really sure I have not played with this but at a guess, I would say it has to do with drawing edges when next to empty cells)(let me know if I'm wrong)

    Eg:

    var list_of_tiles = [Vector2i(5,6),Vector2i(5,7),Vector2i(6,6)]
    $TileMap.set_cells_terrain_connect(0, list_of_tiles, 0, 1,true)

I figured it out and this is how the function works.

The function loops threw all the Vector2i’s in an array that you provide it, then it changes the corresponding cells to a specific tile type that you set.

If you provide it with too many Vector2i’s as I did it will not run or at least run slowly, so if this is a problem you can split your array of Vector2i’s into smaller chunks and run them in small groups.

Also, don’t forget to set up your tilemap/terrain in the editor or it will not work properly.

set_cells_terrain_connect(a,b,c,d,e)

a: layer: int, this sets the layer to draw to.
b: cells: array of Vector2i, add all the cells you wish to change on your tilemap
c: terrain_set: int, This selects the set of tiles you wish to use.
d: terrain: int, This selects the tile in the terrain_set you wish to draw.
e: ignore_empty_terrains: bool = true, (I'm not really sure I have not played with this but at a guess, I would say it has to do with drawing edges when next to empty cells)(let me know if I'm wrong)

Eg:

var list_of_tiles = [Vector2i(5,6),Vector2i(5,7),Vector2i(6,6)]
$TileMap.set_cells_terrain_connect(0, list_of_tiles, 0, 1,true)