Set tileset atlas subtile from code

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

I have a tileset that has several tiles in them, each has an atlas that cuts up the large image into tiles. In the editor, I can select a tile and one of the sub-tiles easily. In code I can set the tile I want to use very easily by using set_cell. How can I set the sub-tile from code? I’d also like to get a list of available sub-tiles so that I could pick one of them randomly.

:bust_in_silhouette: Reply From: GammaGames

Turns out they don’t really have an easy way to do it. You have to use set_cell and pass in the autotile_coord (which also means you have to pass in the other values as default). I wrote a little function that returned a random subtile coord given a tile’s id:

func _get_subtile_coord(id):
    var tiles = $TileMap.tile_set
    var rect = tilemap.tile_set.tile_get_region(id)
    var x = randi() % int(rect.size.x / tiles.autotile_get_size(id).x)
    var y = randi() % int(rect.size.y / tiles.autotile_get_size(id).y)
    return Vector2(x, y)

Using this, I was able to randomize my plants, ground, and water tiles. Using mushy,


enter image description here