Making an previous placed tile delete within some seconds in script

Question

Guys do anyone know how to delete an 1 tile in script (previous placed tile) within some seconds? I paste the current script bellow. Any help will be appreciated.

Script

extends Node2D

@onready var shadow: AnimatedSprite2D = $Shadow
@onready var tile_map: TileMap = $TileMap

var groundL = 0

func _input(event: InputEvent) → void:
if not Input.is_action_pressed(“MBL”) or not Input.is_action_pressed(“MBR”):

	var mousePos = get_global_mouse_position()
	var tileMousePos = tile_map.local_to_map(mousePos) + Vector2i(-2, 0)
	var sourceId : int = 1
	var tileAtlasCoor: Vector2i = Vector2i(0,0)
	
	tile_map.modulate.a8 = 100
	tile_map.set_cell(groundL, tileMousePos, sourceId, tileAtlasCoor)
	
if Input.is_action_pressed("MBL"):
	var mousePos = get_global_mouse_position()
	var tileMousePos = tile_map.local_to_map(mousePos) + Vector2i(-2, 0)
	var sourceId : int = 1
	var tileAtlasCoor: Vector2i = Vector2i(0,0)
	tile_map.modulate.a8 = 1000
	tile_map.set_cell(groundL, tileMousePos, sourceId, tileAtlasCoor)
	
if Input.is_action_pressed("MBR"):
	var mousePos = get_global_mouse_position()
	var tileMousePos = tile_map.local_to_map(mousePos) + Vector2i(-2, 0)
	var sourceId : int = -1
	var tileAtlasCoor: Vector2i = Vector2i(-1,-1)
	tile_map.modulate.a8 = 1000
	tile_map.set_cell(groundL, tileMousePos, sourceId, tileAtlasCoor)

You can use this function to erase a tile after it has been set:

func   cell_timeout(layer: int, coords: Vector2i)
	# Create a timer to hide the feedback label after 0.5 seconds
	var timer = Timer.new()
	timer.wait_time = 0.5 # Adjust to your requirement
	timer.one_shot = true
	timer.timeout.connect(func():
		tile_map.erase_cell(layer, coords)
		timer.queue_free()  # Properly free the timer node
	)
	add_child(timer)  # Add the timer to the scene tree
	timer.start()

Here is an example for your “MLB” action:


if Input.is_action_pressed("MBL"):
	var mousePos = get_global_mouse_position()
	var tileMousePos = tile_map.local_to_map(mousePos) + Vector2i(-2, 0)
	var sourceId : int = 1
	var tileAtlasCoor: Vector2i = Vector2i(0,0)
	tile_map.modulate.a8 = 1000
	tile_map.set_cell(groundL, tileMousePos, sourceId, tileAtlasCoor)
	cell_timeout(groundL,tileMousePos)
1 Like

Thanks for help but i wanted to make that the tile will disapear when the mouse moved to another cell coordinations, like in some automation/ factory games. Do you know how to do that?
And even when the tile is placed then the tile dissapear after placing the correct in pressing LMB.Oh and i forgot i made another if statement for that.

I can help you if you describe your problem clearly and in detail. If you want to erase a tile on mousemove, you can use this in your _input function:

if event is InputEventMouseMotion:
	var mousePos = get_global_mouse_position()
	var tileMousePos = tile_map.local_to_map(mousePos) + Vector2i(-2, 0)
	tile_map.erase_cell(groundL, tileMousePos)

This will delete any cell that the mouse moves over. If you need a more specific implementation, you would need to track tileMousePos in a global variable and write some code to respond when it changes.

1 Like

So you said that it will delete any cells on the current layer but i dont think the “erase_cell” function work for me beacuse its not easing cells. Maybe i did something wrong.Here you have my current code to look for errors.
Edit: I didnt saw the “iI can help you if you describe your problem clearly and in detail”. So i want to make an conveyor placing sytem like in ShapeZ, Starground. And i need to make an “shadow” for the tiles before placing it(image bellow). If you need more simply ask. Thanks.
Zrzut ekranu (22)

extends Node2D

@onready var tile_map: TileMap = $TileMap

var groundL = 0
var conveyorL = 1
var shadowL = 2
var placing = false
func  cell_timeout(layer: int, coords: Vector2i):
	
	var timer = Timer.new()
	timer.wait_time = 0.05
	timer.one_shot = true
	timer.timeout.connect(func():
		tile_map.erase_cell(layer, coords)
		timer.queue_free()
	)
	add_child(timer)
	timer.start()

func _input(event: InputEvent) -> void:
	
	if event is InputEventMouseMotion:
		var mousePos = get_global_mouse_position()
		var tileMousePos = tile_map.local_to_map(mousePos) + Vector2i(-2, 0)
		tile_map.erase_cell(shadowL, tileMousePos)
	
	if Input.is_action_pressed("MBL"):
		var mousePos = get_global_mouse_position()
		var tileMousePos = tile_map.local_to_map(mousePos) + Vector2i(-2, 0)
		var sourceId : int = 1
		var tileAtlasCoor: Vector2i = Vector2i(0,0)
		placing = true
		
		tile_map.set_cell(conveyorL, tileMousePos, sourceId, tileAtlasCoor)
		
	else:
		placing = false
	if Input.is_action_pressed("MBR"):
		var mousePos = get_global_mouse_position()
		var tileMousePos = tile_map.local_to_map(mousePos) + Vector2i(-2, 0)
		var sourceId : int = -1
		var tileAtlasCoor: Vector2i = Vector2i(-1,-1)
		
		tile_map.set_cell(conveyorL, tileMousePos, sourceId, tileAtlasCoor)
func _physics_process(delta: float) -> void:
	if placing == false:
		var mousePos = get_global_mouse_position()
		var tileMousePos = tile_map.local_to_map(mousePos) + Vector2i(-2, 0)
		var sourceId : int = 0
		var tileAtlasCoor: Vector2i = Vector2i(0,0)
		
		tile_map.set_cell(shadowL, tileMousePos, sourceId, tileAtlasCoor)
		
		


	

Are you online?

You say it’s not erasing cells from your tile_map. Can you tell me what parts of your code are working and what parts are not?

Try putting a breakpoint here and see if it is called and with what values. You may need to add some print statements to follow along with what the script does.

1 Like

Just the erase cell function does not work.Heres the breakpoint info.

So you’re saying there is a tile on position (0,1) on the shadow layer and it’s not being erased?

If that’s the case, try printing the output of tilemap.get_cell_source_id(shadowL,tileMousePos) and see what value is returned. Then we’ll know if it can reach the expected tile.

Tilemousepos is not declared in current scope

I think you made a typo. You are using tileMousePos in your script so you should write it like that instead of Tilemousepos

I was trying to do that but i didnt work.