Godot Version
4.3
Question
Please help me I am creating a game that uses a hexagnal grid using a TikeMapLayer. I have several units on the grid. If I click on one of the units the tile the unit is on along with the 6 surrounding tiles all their tile’s images should change to a highlight tile cell’s image which is part of the same tileset. If I click on the unit again a;; the highlighted tile images should go back to the original images they were. The problem is, once I click on the unit, the tile image the unit is on (not the unit’s image) along with the surrounding 6 tikes’s image all disappear. Even if I click on the unit again those tile images never return to their original images. I am inclucing code I use, please help me fix it? I am including a tet file with my code, please help me?
extends Node2D
@export var tilemap_layer: TileMapLayer
@export var highlight_tile_id: int
@onready var texture_button: TextureButton = $TextureButton
var original_tiles = {}
var is_highlighted = false
func _ready():
texture_button.connect(“pressed”, self, “_on_texture_button_pressed”)
func _on_texture_button_pressed():
var unit_position = global_position
var cell = tilemap_layer.local_to_map(unit_position)
var neighbors = get_hex_neighbors(cell) + [cell]
if is_highlighted:
restore_original_tiles(neighbors)
else:
highlight_tiles(neighbors)
is_highlighted = not is_highlighted
func get_hex_neighbors(cell):
var directions = [
Vector2i(1, 0), Vector2i(1, -1), Vector2i(0, -1),
Vector2i(-1, 0), Vector2i(-1, 1), Vector2i(0, 1)
]
var neighbors =
for dir in directions:
neighbors.append(cell + dir)
return neighbors
func highlight_tiles(cells):
for cell in cells:
if not original_tiles.has(cell):
original_tiles[cell] = tilemap_layer.get_cell_source_id(cell)
tilemap_layer.set_cell(cell, highlight_tile_id)
func restore_original_tiles(cells):
for cell in cells:
if original_tiles.has(cell):
tilemap_layer.set_cell(cell, original_tiles[cell])
original_tiles.erase(cell)