How to access the properties of a tile in a TileMap from code?

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

I am working in Godot 3.1 beta. I have a TileMap with collision I am trying to determine if the tile I am standing on has shape_one_way = true.

So far, I have managed to determine the index of the tile I am standing on, but I don’t know how to go about using the index to access the tile’s properties from code.

if bottomleft_ray.is_colliding() or bottomright_ray.is_colliding():
	var collider
	var collision_point
	if bottomleft_ray.is_colliding():
		collider = bottomleft_ray.get_collider()
		collision_point = bottomleft_ray.get_collision_point()
	elif bottomright_ray.is_colliding():
		collider = bottomright_ray.get_collider()
		collision_point = bottomright_ray.get_collision_point()

	print( # prints "TileMap"

	var cell_location = collider.world_to_map(collision_point)
	var cell = collider.get_cell(cell_location.x, cell_location.y)

    # if cell has shape_one_way = true, do stuff...

This works as far as it goes, and cell is the index of the particular tile in the TileMap that my player is standing on (in this case, it’s 1).

But how can I go about accessing shape_one_way for this tile to see if it’s true?

:bust_in_silhouette: Reply From: kidscancode

The tile_set property contains the TileMap’s tile data resource, which gives you a TileSet object. At which point you could do:

if collider.tile_set.tile_get_shape_one_way(cell, 0):

Note this assumes there’s only one shape on the tile. If your tiles have multiple shapes, you’ll have to use tile_get_shapes() and iterate through them.

Thanks! This also appears to work: collider.tile_set.get(String(cell) + "/shape_one_way")

Diet Estus | 2019-01-20 21:05

I suppose, but pure get() calls like that look uglier and reduce readability, in my opinion.

kidscancode | 2019-01-20 21:10

I think you’re right! Good call.

Diet Estus | 2019-01-20 21:11