How to get the TileMap used in a cell?

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

I use different tilemaps and have to know what tilemap is used in a specific cell.

following doesn’t work:

var position = get_cell(x,y)
print( position.get_tileset())

this will only generate an error witch say that that function isnt available in base int

:bust_in_silhouette: Reply From: Zylann

get_cell returns the ID of the tile, it’s an integer, so of course it doesn’t have the tileset function on it. Tilemap does.

You cannot use get_cell without already having a tilemap to call it on. So that means if you are able to call get_cell, you already have access to the tileset, just using a different function. You don’t even need to call get_cell for that.

If you wrote get_cell(x, y), I assume your script extends Tilemap already, so you can just access the tile_set property directly. However this will give you the tileset of the tilemap your script is attached to (and likewise, get_cell will give you only tiles from that same tilemap).

To access other tilemaps, you’d likely need to get them with get_node and use get_cell on them instead.

# Getting tile and tileset from the tilemap this script is attached onto.
# Note: depending on how you structure your scene tree with
# your multiple tilemaps, you might not need to do it this way after all
var t0 = get_cell(x, y)
var tileset0 = tile_set

# Accessing other tilemaps
var tilemap1 = get_node("Path/To/Tilemap1")
var tilemap2 = get_node("Path/To/Tilemap2")

# Getting tile and tileset from first tilemap
var t1 = tilemap1.get_cell(x, y)
var tileset1 = tilemap1.tile_set

# Getting tile and tileset from second tilemap
var t2 = tilemap2.get_cell(x, y)
var tileset2 = tilemap2.tile_set

If you have this problem within a collision callback, you can get the tilemap you collided with (it will be the “body” or “collider” depending on the case).