Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add has_custom_data() to TileData #11317

Closed
popcar2 opened this issue Dec 8, 2024 · 0 comments · Fixed by godotengine/godot#101488
Closed

Add has_custom_data() to TileData #11317

popcar2 opened this issue Dec 8, 2024 · 0 comments · Fixed by godotengine/godot#101488
Labels
Milestone

Comments

@popcar2
Copy link

popcar2 commented Dec 8, 2024

Describe the project you are working on

A 2D platformer character that checks the tile under it for custom data, for things like having different footstep noises.

Describe the problem or limitation you are having in your project

The way to get a custom data layer from a tile I'm currently touching is by doing this (the final line is the most relevant):

for i in range(get_slide_collision_count()):
	var slide_collision: KinematicCollision2D = get_slide_collision(i)
	if slide_collision.get_collider() is TileMapLayer:
		var tilemaplayer: TileMapLayer = slide_collision.get_collider()
		var tile_coords: Vector2i = tilemaplayer.get_coords_for_body_rid(slide_collision.get_collider_rid())
		var tile_data: TileData = tilemaplayer.get_cell_tile_data(tile_coords)
		var is_metal_tile: Variant = tile_data.get_custom_data("is_metal_floor")

There's one major problem with this: tile_data.get_custom_data("is_metal_floor") throws an error if the custom data layer doesn't exist on the current tile. The problem is that there's no way to check via the TileData if a data layer exists on it or not, which is weird and unintuitive.

Image

This becomes confusing for people trying to just fetch custom tile data without causing errors. See: https://forum.godotengine.org/t/can-you-determine-if-a-custom-data-layer-exists-for-a-tilemaplayer/85891

Describe the feature / enhancement and how it helps to overcome the problem or limitation

Add a function has_custom_data(layer_name: String) which would allow me to check if it exists before trying to get it.

Alternatively, maybe the function shouldn't cause an error in the first place and just let me check if it's null or not.

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

...
if tile_data.has_custom_data("is_metal_floor"):
    if tile_data.get_custom_data("is_metal_floor") == true:
        [do stuff without getting errors]

If this enhancement will not be used often, can it be worked around with a few lines of script?

Yes, but you would need to check if the data layer exists in the TileSet first rather than TileData, which is cumbersome and probably not always accessible. If TileData can get the custom data layer, it should also tell me whether it exists or not.

See: https://forum.godotengine.org/t/can-you-determine-if-a-custom-data-layer-exists-for-a-tilemaplayer/85891/5

Is there a reason why this should be core and not an add-on in the asset library?

It's core functionality.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants