+2 votes

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.

in Engine by (45 points)

1 Answer

+2 votes
Best answer

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,
Before:

autotiles

After:
enter image description here

by (45 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.