Placing Tile set scenes via code

Godot Version

4.3

Question

I want to be able to randomly place Tile set scenes via code. I have a tester tile map that has a scenes collection with two scenes inside of it. I want to be able to place the scenes at random locations.

Thank you :slight_smile:

If you know the tile’s ID, you can call TileMapLayer.set_cell with the arguments
["some random position", "ID of the TileSetSource that has the two scenes", Vector2(0, 0), "tile ID"]. You can get a random position by getting to random integers that are inside the bounds of the tilemap and using them in a Vector2i. For example:

var x: int = randi() % tile_map_width
var y: int = randi() % tile_map_height
var vector: Vector2i = Vector2i(x, y)

You can get the tile set’s ID in the TileSet panel in the editor: