How do I get TileData from atlas coordinates?

Godot Version

v4.4.1

Question

How can I get TileData directly from atlas coordinates? Right now the only way I know how to place a tile and then use get_cell_tile_data. But obviously this is pretty kludgy.

func get_cell_tile_data_from_atlas_coords(atlas_coords: Vector2i) -> TileData:
	#what's the right way to do this?
	var tmp_cell = Vector2i(10000, 10000)
	set_cell(tmp_cell, 1, atlas_coords)
	var data = get_cell_tile_data(tmp_cell)
	set_cell(tmp_cell, 1, Vector2i(-1,-1))
	return data

You need to get TileSet data from your TileMapLayer.
And then you can access the TileData with atlas_coord.
Something like this:

func get_cell_tile_data_from_atlas_coords(atlas_coords: Vector2i) -> TileData:
	#tile_map_layer holds your TileMapLayer
	var tile_set = tile_map_layer.get_tile_set()
	
	#assuming tile set source id is 0
	var tile_set_source = tile_set.get_source(0)
	
	#assuming that tile set source is TileSetAtlasSource, not TileSetScenesCollectionSource
	#and assuming that you need the original tile data (0), not the alternative
	return tile_set_source.get_tile_data(atlas_coords, 0)

If you want it more modular, you can also pass source_id and alternative_tile_id to the function, and use it.

func get_cell_tile_data_from_atlas_coords(atlas_coords: Vector2i, source_id: int = 0, alt_tile_id: int = 0) -> TileData:
	#tile_map_layer holds your TileMapLayer
	var tile_set = tile_map_layer.get_tile_set()
	
	var tile_set_source = tile_set.get_source(source_id)
	
	#assuming that tile set source is TileSetAtlasSource, not TileSetScenesCollectionSource
	return tile_set_source.get_tile_data(atlas_coords, alt_tile_id)
1 Like

That is exactly what I want. Thanks so much!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.