How to change tiles in TileMap with codes

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

I have a TileMap node with 3 types of tiles (let them be a, b, c). When I click on the screen I want to change the tile on which I have clicked with another one. I don’t have any idea about it. Can anyone please help me. Thank you in advance!

:bust_in_silhouette: Reply From: clemens.tolboom

You can use TileMap.world_to_map for your mouse click get_global_mouse_position() to get to the cell value using TileMap.get_cellv()

var tilemap = $TileMap

var mouse :Vector2 = get_global_mouse_position()
var cell :Vector2 = tilemap.world_to_map(mouse)
var abc :int = tilemap.get_cellv(cell)
var new_abc :int = (abc + 1) % 3 # just an example plus 1 modules 3
tilemap.set_cellv(cell, new_abc)

But how will I change cell value?

Help me please | 2021-07-14 16:22

tilemap.set_cellv(cell, new_abc) … I’ve updated my answer … pleas mark answer a the answer if applicable :slight_smile:

clemens.tolboom | 2021-07-14 20:00

Thanks! it works!

Help me please | 2021-07-15 03:09