Invalid type in function 'set_cell' in base 'TileMap'. Cannot convert argument 2 from int to Vector2i.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By saancch

Hello, I want to make it so that when I click on the LMB, I delete a tile from the tilemap, but I ran into the error “Invalid type in function ‘set_cell’ in base ‘TileMap’. Cannot convert argument 2 from int to Vector2i.”. What could be causing this and how can I fix it?

   extends Node2D

@onready var tiles = get_node("TileMap")

# Called when the node enters the scene tree for the first time.
func _ready():
	pass # Replace with function body.

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	var mp = get_global_mouse_position()
	var tp = tiles.local_to_map(mp)
	
	if Input.is_action_just_pressed("lb"):
		tiles.set_cell(tp.x, tp.y, -1)
	if Input.is_action_just_pressed("rb"):
		tiles.set_cellv(tp, 0)
:bust_in_silhouette: Reply From: HyperlinkYourHeart

If you look at the docs for the TileMap node, the signature for the method set_cell is:

void set_cell ( int layer, Vector2i coords, int source_id=-1, Vector2i atlas_coords=Vector2i(-1, -1), int alternative_tile=0 )

local_to_map returns the coordinates as a Vector2i, so it should be the second argument to set_cell, but you’re passing the x and y as the first and second argument. You also need to specify the layer - if you don’t have multiple layers then I believe the default is 0. So try this instead:

tiles.set_cell(0, tp, -1)

Also, set_cellv no longer exists, so you will have to convert that to a set_cell call as well.

Thanks a lot, helped me out.

saancch | 2023-06-14 13:54