Making a tilemap's tile react with a custom action

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By elek95eel
:warning: Old Version Published before Godot 3 was released.

I’ve been trying to figure out how best to make a tile clickable and so far I’ve come up with this:

Ready function

func _ready():
_tileset = MapBox.get_tileset()
set_process_input(true)
start()

Input function

func _input(event):
var tile_pos = MapBox.world_to_map(event.pos)
if event.is_action_pressed("ui_clicked_left"):
	LEFT_CLICK = true
	MapBox.set_cellv(tile_pos, _tileset.find_tile_by_name("blank_tile"))

The only thing is that it only seems to work in the top-left part of the selected tile without affecting neighbouring tiles.

How would I properly get the tile to react with in its dedicated region, if that makes any sense?

:bust_in_silhouette: Reply From: Zylann

Well clicking on a tile does what you want, which is to set the tile I guess. If you want to modify the neighboring tiles… then set the neighboring tiles by doing +1 or -1 on tile_pos.x and tile_pos.y.

You can’t put a script on tiles individually if that’s what you are implying. However, you can get the tile ID and execute different code in your script depending on that ID.

What do you mean by “affects top left part only”? A tilemap is a grid, world_to_map converts coordinates to that grid. If only the top-left part of a tile reacts, it’s probably because your tiles are bigger than grid cells and you should fix that.

Thank you for your help thus far:

The red box represents an estimate where the input actually happens to affect tile 6 but I want it so it’s like this:

elek95eel | 2017-06-09 00:28

I think tiles in your tilemap aren’t centered correctly. First make sure the tilemap is in (0,0), then make sure the tiles in your tileset have centered sprites, then they should line up exactly with the grid when you paint them on the tilemap.

Zylann | 2017-06-09 00:52

Ah, I didn’t take into consideration tilemaps actually start from (0,0) and not what the first tile drawn is so I had to incorporate vector math to get the effect I want. Thank you so much :).

elek95eel | 2017-06-09 01:50