Help with erasing tiles with Area2D

Version: Godot 4.3 stable

So, my character has a spin/punch/whatever move that enables an area2d node for a short period of time. That’s not the issue, the thing is i’m also trying to make it delete certain tiles when it detects them.

And I came up with this and it doesn’t delete the tiles.

func _on_punch_area_body_entered(body) -> void:
	print("check")
	if body is TileMapLayer:
		print("check 2")
		if body.name=="Breakable":
			print("breakable")
			var tile = body.local_to_map(to_local(body.position))
			print(tile)
			body.erase_cell(tile)

I also tried grabbing atlas coordinates and doing set_cell instead but still doesnt do anything.
ANY help would be appreciated, i’ve been stuck for 3 days straight trying to do this.

Good of you to ask for help. This is also a skill.

There are multiple ways of doing this, but I wouldn’t recommend an area2d. I think your problem is not with setting or erasing a cell, but with getting the cells you need.

  1. if you know the source_id of the cell, you can use TileMapLayer — Godot Engine (stable) documentation in English to get all positions of cells with that id. You can then do something like:
	# Function to check if a cell position is within range
	func is_position_in_range(global_pos: Vector2, range: int) -> bool:
		var player_cell_pos: Vector2 = get_player_cell_pos() #You'd need to implement this function
		return global_pos.x >= player_cell_pos.x-range and global_pos.x < player_cell_pos.x+range and \
			   global_pos.y >= player_cell_pos.y-range and global_pos.y < player_cell_pos.y+range


func cells_in_range(cell_pos_list: Array):
  var cellsinrange:Array = []
  for cellpos in cell_pos_list:
    if is_position_in_range(cellpos, 3):
      cellsinrange.append(cellpos)
  return cellsinrange

You would then call cells_in_range and pass in the output of get_used_cells_by_id

  1. The other approach involves getting all cells in a range around the player position and checking each one to see if they are of the type of cell you want to delete. This looks something like:
    Dimensionfall/Scripts/Helper/overmap_manager.gd at 4957b2b41a75f50020b6d916b20967ed2b7055c1 · Khaligufzel/Dimensionfall · GitHub

You would need to make adjustments so that cellpos is the position of the cell that the player is in and define a radius to replace load_radius in that function.

Once you have the cells you want to delete, I’d call set_cell on them and make them black or something.

What exactly should I make “get_player_cell_pos()” do?

Edit: By the way, I’m trying to set cell with the output of cells in range and it says that it’s an array instead of vector2i. Am I doing something wrong?

Also I just tried doing this with move_and_collide inside the main physics proccess function, and it still doesnt delete the blocks, so something is up with the tileset itself…?

var collision_info = move_and_collide(velocity * delta)
	if collision_info:
		var collider = collision_info.get_collider()
		if collider is TileMapLayer:
			var collision_position = collision_info.get_position()
			var map_position = collider.local_to_map(collision_position)
			if collider.name=="Breakable":
				print("brakeabel CHECK!!")
				var atlas_pos = collider.get_cell_atlas_coords(map_position)
				collider.set_cell(map_position,0,atlas_pos,0)

OK, so I literally tested deleting the tile with global mouse position since, you know, it’s the most direct way to detect a tile’s location, and it STILL doesn’t erase the tile. Something’s really wrong in here