Get tile name in godot

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

If I have a bullet, I want to get the tile name if it collides with any tile , anyone??

var tilename1 = $map/TileMap.tileset.tilegetname($map/TileMap.get_cell(myposontilemap.x, myposontilemap.y))

if (npc.tilestandingon == “water” or npc.tilestandingon == “waterdeep” or npc.tilestandingon == “deepwater”) and npc.doing != “swimming” and npc.doing != “standingInWater” and npc.doing != “drowning” and npc.doing != “coma” and npc.doing != “dead”:
npc.doing = “standingInWater”

My NPC’s run around the map and constantly look for what Tile they are standing on.
This seems to be the lines of code that do this for me.
www.customcabinetsnc.com
https://godotengine.org/qa/user/customcabinetsnc

customcabinetsnc | 2022-11-28 02:26

I’ll see if I can pull all my related code - I get the Tile name all the time.
Sorry if this is messy - want to try to pull some code for you quickly…

var tilename0 = $map/TileMap.tileset.tilegetname($map/TileMap.get_cell(myposontilemap.x, myposontilemap.y))

if (npc.tilestandingon == “water” or npc.tilestandingon == “waterdeep” or npc.tilestandingon == “deepwater”) and npc.doing != “swimming” and npc.doing != “standingInWater” and npc.doing != “drowning” and npc.doing != “coma” and npc.doing != “dead”:
npc.doing = “standingInWater”

My NPC’s run around the map and constantly look for what Tile they are standing on.
This seems to be the lines of code that do this for me. (Sorry, wrote over a year ago so not completely familiar with right now). check out this: User kaylamarquez - Godot Engine - Q&A

kaylamarquez | 2023-02-08 21:37

I’ll see if I can pull all my related code - I get the Tile name all the time.
Sorry if this is messy - want to try to pull some code for you quickly…

var tilename0 = $map/TileMap.tileset.tilegetname($map/TileMap.get_cell(myposontilemap.x, myposontilemap.y))

if (npc.tilestandingon == “water” or npc.tilestandingon == “waterdeep” or npc.tilestandingon == “deepwater”) and npc.doing != “swimming” and npc.doing != “standingInWater” and npc.doing != “drowning” and npc.doing != “coma” and npc.doing != “dead”:
npc.doing = “standingInWater”

My NPC’s run around the map and constantly look for what Tile they are standing on.
This seems to be the lines of code that do this for me. (Sorry, wrote over a year ago so not completely familiar with right now).

User joyceconley - Godot Engine - Q&A

joyceconley | 2023-02-09 00:05

'll see if I can pull all my related code - I get the Tile name all the time.
Sorry if this is messy - want to try to pull some code for you quickly…

var tilename0 = $map/TileMap.tileset.tilegetname($map/TileMap.get_cell(myposontilemap.x, myposontilemap.y))

if (npc.tilestandingon == “water” or npc.tilestandingon == “waterdeep” or npc.tilestandingon == “deepwater”) and npc.doing != “swimming” and npc.doing != "stan like User DiannHays - Godot Engine - Q&A

DiannHays | 2023-02-09 01:40

:bust_in_silhouette: Reply From: exuin

You can’t get the tile name. Tiles are identified by their index which you can view by holding down alt in the Tileset editor. Use the world_to_map() and get_cell() methods of the TileMap class.

Edit: As it turns out, you can get the tile name, but you have to get it from the Tileset’s tile_get_name() method and not from the TileMap. You’ll still have to get the index of the tile as well first.

I’ll see if I can pull all my related code - I get the Tile name all the time.
Sorry if this is messy - want to try to pull some code for you quickly…

var tile_name0 = $map/TileMap.tile_set.tile_get_name($map/TileMap.get_cell(myposontilemap.x, myposontilemap.y))

if (npc.tilestandingon == “water” or npc.tilestandingon == “waterdeep” or npc.tilestandingon == “deepwater”) and npc.doing != “swimming” and npc.doing != “standingInWater” and npc.doing != “drowning” and npc.doing != “coma” and npc.doing != “dead”:
npc.doing = “standingInWater”

My NPC’s run around the map and constantly look for what Tile they are standing on.
This seems to be the lines of code that do this for me. (Sorry, wrote over a year ago so not completely familiar with right now).

mattkw80 | 2021-03-31 14:08

Huh, I didn’t see that in the Tileset docs. Whoops! I’ll edit my answer.

exuin | 2021-03-31 16:23

I just hope I understand the OP question properly.

In my game… every tile counts… SAND, MUD, ROCK, SKY, etc.
I seem to be able to pull the ‘names’. Really have not looked at that area of the game for over a year.

mattkw80 | 2021-03-31 17:44

If you have the tile location as a vector2i, then you can grab the tile name using some Godot magic:

tile_location is a Vector2i of the cell location
local_tile_map is the TileMap that the cell is located in

var tile_data = local_tile_map.get_cell_tile_data(0, tile_location)
var tile_source = local_tile_map.get_cell_source_id(0, tile_location)
var tile_set = local_tile_map.tile_set
var tile_set_source = tile_set.get_source(tile_source)
var tile_name = tile_set_source.resource_name