I have code that procedurally generates a cave level using autotiles. I have a player instantiated and can move around the caves fine.

I have tried to use erase_cell(pos) to erase a cell when digging is triggered by keypress and this works fine. My problem comes with the tilemap not automatically reconfiguring when I remove one of the tiles. What I have noticed is the tile positions for the tilemap are stored in an array and after the tile is removed, it is still present in the array.

``extends Node2D

@onready var tilemap = $TileMapLayer

Map dimensions

const MAP_WIDTH = 96
const MAP_HEIGHT = 40

Noise parameters

var noise: FastNoiseLite
@export var noise_scale: float = 0.05 # Reduced for more variation
@export var noise_threshold: float = 0.3 # Increased to create more solid areas

var earth_cells = []
var rock_cells = []

Signal for generation completion

signal generation_completed

func _ready():

Initialize noise

noise = FastNoiseLite.new()
noise.seed = 100 # Set a random seed
noise.frequency = noise_scale

func generate_cave():
print("Starting cave generation...")

Clear existing tiles

tilemap.clear()

# Generate initial noise-based cave
var cells = generate_noise_cave()
emit_signal("generation_completed")

func generate_noise_cave():

var earth_cells = []

var rock_cells = []

# Generate a random transition height for each column
var transition_heights = []
var min_transition = 6
var max_transition = 8
var rng = RandomNumberGenerator.new()
for x in range(MAP_WIDTH):
	transition_heights.append(rng.randi_range(min_transition, max_transition))

# Generate cave using noise
for x in range(MAP_WIDTH):
	for y in range(MAP_HEIGHT):
		# Skip top few rows to keep surface clear
		if y < 5:
			continue
		
		# Get noise value
		var noise_value = noise.get_noise_2d(x,y)
		
		# Add cell if below threshold
		if noise_value < noise_threshold:
			# Use Earth tileset above transition, Rock below
			if y < transition_heights[x]:
				earth_cells.append(Vector2i(x, y))
			else:
				rock_cells.append(Vector2i(x, y))

# Set tiles with correct source IDs (0 = Rock, 1 = Earth)
tilemap.set_cells_terrain_connect(earth_cells, 1, 0)  # Earth tileset (source_id 1)
tilemap.set_cells_terrain_connect(rock_cells, 0, 0)   # Rock tileset (source_id 0)

print(earth_cells)
#print(rock_cells)

Function to remove a tile at a specific position

func remove_tile(pos: Vector2i) -> void:

Remove the target tile

print("Removing tile : ",pos)
tilemap.erase_cell(pos)
print(earth_cells)