How to get tile from tilemap by it's index?

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

This code returns index of clicked tile:

func _input(event):
	if event is InputEventMouseButton:
		if event.button_index == BUTTON_LEFT:
			var click_pos = get_global_mouse_position()
			
			var tilemap = $TileMap
			var tile_pos = tilemap.world_to_map(click_pos)
			var cell = tilemap.get_cellv(tile_pos)
			var tile = tilemap.get_cell(tile_pos.x, tile_pos.y)

			print("TM pos: ", click_pos)
			print("cell: ", cell)
			print("tile: ", tile)

But how can I get Tile object by it’s index?

:bust_in_silhouette: Reply From: exuin

There is no such thing as Tile object. Tilesets are the resources that have tiles in them, but Tiles aren’t objects by themselves. You can use Tileset methods to modify tiles, but since they’re not actually objects you can’t get them.

So, how to detect is it water tile, or grass tile, or lava tile?

Robotex | 2021-02-26 12:01

You will need to use the tile’s index. If you’re in the tileset editor you can hold alt to view the index of the tile.

exuin | 2021-02-26 14:15

Oh, I understood

Robotex | 2021-02-26 21:11

:bust_in_silhouette: Reply From: Inces

get-used-cells-by-id(index) returns array with all cells of that index

Is there can be many cells on the same index?

Robotex | 2021-02-25 08:03

Yes, tile index is a number for type of tile you created in editor, for example you made grass tile, floor tile and obstacle tile, and their indexes are 0,1 and 2.
If you are looking for unique variable determining unique tile, than it is get cell and get cellv

Inces | 2021-02-25 11:38

Oh, that index is what I seek.

Robotex | 2021-02-26 21:11