I have a piece of code that detects the passability of surrounding tiles from where the player stands.
Passability is determined by a Custom Data Layer called “Passability” and has either a “YES” value or “NO”. Having a null value or not having tile_data means that it is not passable.
Here is the code
func _get_passability_of_target_tile(current_position:Vector2, direction: Vector2) -> bool:
var target_position = current_position + direction * tile_size
var local_coords = local_to_map(target_position)
var cell_coords = get_cell_atlas_coords(local_coords)
var tile_data = get_cell_tile_data(cell_coords)
return tile_data.get_custom_data("Passability") == "YES" if tile_data else false
And here is pic of the tileset with the Custom Data Layer values:
Actually right now I have the problem that get_cell_tile_data sets tile_data to null i.e. according to the docs “Returns the TileData object associated with the given cell, or null if the cell does not exist or is not a TileSetAtlasSource.”
current_position is global.
target_position as well.
This is a screenshot from the debugger.
It gets a weird local coords = (2, 2)
However get_cell_atlas_coords returns a correct (5, 3)
I was incorrect it was finding tiledata for some tiles and does not for others.
For example:
On the right side of the character if finds the cell tile coords correctly but finds Passability value of YES where it should find NO.
For the tile to the left if finds the cell tile coords correctly but finds no tile data.
Edit:
It finds tile data for the tile to the right with value YES that is actually NO and also for the tile to bottom with value NO which is correct.
The current_position is the center position of the collision_layer rect that is visible in the screenshot. The target_position is the center of the next tile.
And If I was passing incorrect values I would not get the correct coordinates from the atlas, but I do, in all cases.
I decided to change my approach and use raycasts and collision detection to control allowed movement and it works like a charm.
Still worried why tile_data was not working as expected.
In version 4.1 it was working fine, I hope it isn’t an issue of 4.3.