How to detect empty space with script.

Godot Version

4.6.2

Question

How do I detect empty space in godot with script? I’m building a sandbox game kind of like neal.fun’s Sandboxels, and I need the sand and gravel to land in a clump when dropped in a straight line.

This is my physics_process function, is_cell_empty is just a passed function, I need help coding that.

func _physics_process(delta):
	var DiagonalLeft = self.position + Vector2(7, 7)
	var DiagonalRight = self.position + Vector2(-7, 7)
	if not is_on_floor():
		self.velocity.y += GRAVITY * delta
		self.velocity.x = 0
	if is_on_floor() and is_cell_empty(DiagonalRight):
		self.position = DiagonalRight
	if is_on_floor() and is_cell_empty(DiagonalLeft):
		self.position = DiagonalLeft
		
	move_and_slide()
	

Assuming you’re using TileMapLayer you can use its method get_cell_tile_data(). If it returns null the cell is empty.