How to erase a Cell with AutoTiling?

Godot Version

v4.2.1.stable.official [b09f793f5]

Question

What happens is that I can’t delete a cell without updating the other cells when I press the mouse as if it were a Terrain in the Godot editor.

Hypothetical result, The cell is cleared and the other cells are updated:

Final result, The cell is not deleted and nothing happens:

Code:

extends Node2D

@onready var LevelTileMap = $LevelScene/TileMap
@onready var CameraNode = $Camera2D

enum {TileReferenceLayer, TerrainLayer}

var TerrainTiles = []

var TerrainMapSize = EditorVariables.EditorStates.TerrainMapSize
var WalkSpeed = 8

var TileMapSize = 16
var CellSize = 4

var ViewportSize
var CameraLimits
var TileMapRect

func _ready():
	for x in range(TerrainMapSize.x):
		for y in range(TerrainMapSize.y):
			LevelTileMap.set_cell(0, Vector2(x, y), 0, Vector2(25, 30))
	
	TileMapRect = LevelTileMap.get_used_rect()
	
	CameraLimits = (TileMapRect.position + TileMapRect.size) * (TileMapSize * CellSize)
	ViewportSize = get_viewport_rect().size
	
	CameraNode.position = Vector2((ViewportSize.x - 10) / 2, CameraLimits.y)
	print(CameraLimits)
	print()

func _process(delta):
	CameraLimits = (TileMapRect.position + TileMapRect.size) * (TileMapSize * CellSize)
	ViewportSize = get_viewport_rect().size
	
	CameraLimits.x = CameraLimits.x + 4
	CameraLimits.y = CameraLimits.y + 264
	
	if Input.is_action_pressed("Walk_Left"):
		CameraNode.position.x -= WalkSpeed
	if Input.is_action_pressed("Walk_Right"):
		CameraNode.position.x += WalkSpeed
	if Input.is_action_pressed("Walk_Up"):
		CameraNode.position.y -= WalkSpeed
	if Input.is_action_pressed("Walk_Down"):
		CameraNode.position.y += WalkSpeed
	
	CameraNode.position = CameraNode.position.clamp(Vector2((ViewportSize.x - 10) / 2, (ViewportSize.y - 12) / 2), Vector2(CameraLimits.x - ViewportSize.x / 2, CameraLimits.y - ViewportSize.y / 2))
	
	var MousePosition = get_global_mouse_position() / 4
	var TileMousePosition = LevelTileMap.local_to_map(MousePosition)
	
	print(EditorVariables.EditorStates.In_World_Mouse)
	
	if get_viewport().get_mouse_position().y > 386:
		EditorVariables.EditorStates.In_World_Mouse = false
	else:
		EditorVariables.EditorStates.In_World_Mouse = true
	
	if (EditorVariables.EditorStates.In_World_Mouse and (TileMousePosition.x > -1 and TileMousePosition.x < TerrainMapSize.x) and (TileMousePosition.y > -1 and TileMousePosition.y < TerrainMapSize.y)):
		if Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT):
			TerrainTiles.append(TileMousePosition)
			LevelTileMap.set_cells_terrain_connect(1, TerrainTiles, 0, 1)
		elif Input.is_mouse_button_pressed(MOUSE_BUTTON_RIGHT):
			TerrainTiles.erase(TileMousePosition)
			LevelTileMap.set_cells_terrain_connect(1, TerrainTiles, 0, 1)

You’ll need to set the terrain parameter of TileMap.set_cells_terrain_connect() or TileMap.set_cells_terrain_path() to -1 (no terrain)

In your code it should be like: LevelTileMap.set_cells_terrain_connect(1, TerrainTiles, 0, -1)

1 Like

It already works!, Thanks!

Fixed Code:

extends Node2D

@onready var LevelTileMap = $LevelScene/TileMap
@onready var CameraNode = $Camera2D

enum {TileReferenceLayer, TerrainLayer}

var TerrainTiles = []
var DelTiles = []

var TerrainMapSize = EditorVariables.EditorStates.TerrainMapSize
var WalkSpeed = 8

var TileMapSize = 16
var CellSize = 4

var ViewportSize
var CameraLimits
var TileMapRect

func _ready():
	for x in range(TerrainMapSize.x):
		for y in range(TerrainMapSize.y):
			LevelTileMap.set_cell(0, Vector2(x, y), 0, Vector2(25, 30))
	
	TileMapRect = LevelTileMap.get_used_rect()
	
	CameraLimits = (TileMapRect.position + TileMapRect.size) * (TileMapSize * CellSize)
	ViewportSize = get_viewport_rect().size
	
	CameraNode.position = Vector2((ViewportSize.x - 10) / 2, CameraLimits.y)
	print(CameraLimits)
	print()

func _process(delta):
	CameraLimits = (TileMapRect.position + TileMapRect.size) * (TileMapSize * CellSize)
	ViewportSize = get_viewport_rect().size
	
	CameraLimits.x = CameraLimits.x + 4
	CameraLimits.y = CameraLimits.y + 264
	
	if Input.is_action_pressed("Walk_Left"):
		CameraNode.position.x -= WalkSpeed
	if Input.is_action_pressed("Walk_Right"):
		CameraNode.position.x += WalkSpeed
	if Input.is_action_pressed("Walk_Up"):
		CameraNode.position.y -= WalkSpeed
	if Input.is_action_pressed("Walk_Down"):
		CameraNode.position.y += WalkSpeed
	
	CameraNode.position = CameraNode.position.clamp(Vector2((ViewportSize.x - 10) / 2, (ViewportSize.y - 12) / 2), Vector2(CameraLimits.x - ViewportSize.x / 2, CameraLimits.y - ViewportSize.y / 2))
	
	var MousePosition = get_global_mouse_position() / 4
	var TileMousePosition = LevelTileMap.local_to_map(MousePosition)
	
	print(EditorVariables.EditorStates.In_World_Mouse)
	
	if get_viewport().get_mouse_position().y > 386:
		EditorVariables.EditorStates.In_World_Mouse = false
	else:
		EditorVariables.EditorStates.In_World_Mouse = true
	
	if (EditorVariables.EditorStates.In_World_Mouse and (TileMousePosition.x > -1 and TileMousePosition.x < TerrainMapSize.x) and (TileMousePosition.y > -1 and TileMousePosition.y < TerrainMapSize.y)):
		if Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT):
			if TerrainTiles.find(TileMousePosition) == -1:
				TerrainTiles.append(TileMousePosition)
				DelTiles.erase(TileMousePosition)
				LevelTileMap.set_cells_terrain_connect(1, TerrainTiles, 0, 0)
		elif Input.is_mouse_button_pressed(MOUSE_BUTTON_RIGHT):
			if DelTiles.find(TileMousePosition) == -1:
				DelTiles.append(TileMousePosition)
				TerrainTiles.erase(TileMousePosition)
				LevelTileMap.set_cells_terrain_connect(1, DelTiles, 0, -1)


This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.