I wanted to make the tiles on specific layer with higher y-position than player position semi-transparent.
I was thinking of using TileMap.get_used_cells(int layer) to retrieve all the cells and compare it with player y position.
But, it feels like I am checking y-position twice*, since tilemap y-sort is enabled, it would be already checking the position.
My question is. Is there some way to tap into tilemap y-sorting checking code, and just add my little logic to it so that I don’t have to check it twice.
As far as I know, y-sorting happens at the end before drawing the whole scene and gathering all the drawing information. TileMap is not doing any internal y-sort. So, try doing what you have in mind. Good luck!
extends TileMap
@export var player: Player
var wall_layer = 1
var player_coords: Vector2i
var cells_alpha = {}
func _tile_data_runtime_update(layer, coords, tile_data):
tile_data.modulate.a = cells_alpha.get(coords, 1.0)
#Warning: Make sure this function only return true when needed.
#Any tile processed at runtime without a need for it will imply a significant performance penalty.
func _use_tile_data_runtime_update(layer, coords):
return cells_alpha.has(coords)
func _process(delta):
player_coords = local_to_map(player.global_position)
var coords = get_used_cells(wall_layer)
for coord in coords:
print("coord:{0}, player:{1}".format([coord.y, player_coords.y]))
if coord.y >= player_coords.y:
cells_alpha[coord] = 0.3
else:
cells_alpha[coord] = 1.0
notify_runtime_tile_data_update(wall_layer)
Running the code in process worry me a bit but its good to know that this is possible. Thank you