Custom Data are not returned correctly

Godot Version

4.3

Question

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:

Any thoughts why I get an incorrect passability for tiles?
I verified the coordinates of the tiles on the Atlas.

can you print what it gets from

tile_data.get_custom_data("Passability") 

and your custom data layers has String type right?
image

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.

Not sure what I changed, but this is a new issue.

i wonder how your current_position get from, does it pass global_position or just position, that might be the cause of that problem

current_position is global.
target_position as well.
This is a screenshot from the debugger.
Screenshot from 2024-08-25 16-25-17
It gets a weird local coords = (2, 2)
However get_cell_atlas_coords returns a correct (5, 3)


Regardless, get_cell_tile_data for (5, 3) returns null…
This drives me crazy…

then you need to_local

var local_coords = local_to_map(to_local(target_position))

I did.
Still get the same values.
Screenshot from 2024-08-25 17-07-42

if you pass the current_position into the local_to_map(to_local(current_position))
will it give you the correct tiledata?

Good point. I’ll try it.

Yes, it does. Finds the same (5, 3) tile since it’s the same as the target one.

I was incorrect it was finding tiledata for some tiles and does not for others.
For example:
Screenshot from 2024-08-25 17-32-17
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.

i think you are passing the not centered global position, hence this happen

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.

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