0 votes

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?

in Engine by (274 points)

2 Answers

+1 vote

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.

by (8,528 points)

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

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.

Oh, I understood

+2 votes

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

by (7,925 points)

Is there can be many cells on the same index?

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

Oh, that index is what I seek.

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.