• 2D
  • Autotile through code

I want to fill a certain set of tiles in a TileMap with an autotile. Since this set will be changed dynamically, I want to do this through code. If I try to do something like

for tile in set: $TileMap.set_cell(tile.x, tile.y, 0)

only the first tile of the tileset will be used. As can be expected. How can I tell my TileMap to use the proper sub-tile of the autotile? Is there a flag in set_cell() or somewhere else that I'm not aware of?

9 days later

from the docs When you use set_cell, you can set the autotile_coord by setting all the parameters: set_cell ( int x, int y, int tile, bool flip_x=false, bool flip_y=false, bool transpose=false, Vector2 autotile_coord =Vector2( 0, 0 ) )

The vectors are set like you assigned in the tileset texture. If unsure, you can just save he tileset to a .tres file and open it in a text editor and look under [resource] > [number of the main tile of this autotile set]/autotile/bitmask_flags

14 days later

I had a similar problem to you, the simplest answer is to use update_bitmask_region(), calling it after the tiles have been generated, with the first parameter being a Vector2 for the first corner, the second is a Vector2 with the coordinates the opposite corner.

    4 years later