+1 vote

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!

Godot version v3.2.3-stable_win64
in Engine by (942 points)

1 Answer

+1 vote
Best answer

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)

by (642 points)
selected by

But how will I change cell value?

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

Thanks! it works!

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 Frequently asked questions and 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 [email protected] with your username.