Trying to add place block functionality

Hi, I am very new to Godot and so far I managed to put mini terraria clone ( just walking and destroying blocks) but what I can’t figure out is placing blocks. I am trying to use set_cell function but I am getting error: Cannot pass a value of type “Vector2i” as “int”. I don’t understand why does it say that. I am using TileMap for my textures.
Code:
extends CharacterBody2D

const SPEED = 130.0
const JUMP_VELOCITY = -250.0
var range : float = 20

@onready var animated_sprite: AnimatedSprite2D = $AnimatedSprite2D
@onready var tile_map: TileMap = $“../../Tiles/TileMap”

@onready var player: CharacterBody2D = $“.”

var attacking := false

func _ready(): #setup function
animated_sprite.connect(“animation_finished”, _on_anim_finished)

func _physics_process(delta: float) → void:
# Add the gravity.
if not is_on_floor():
velocity += get_gravity() * delta

# Handle jump.
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
	velocity.y = JUMP_VELOCITY
if Input.is_action_just_pressed("attack") and not attacking:
	attacking = true
	animated_sprite.play("attack")

var direction := Input.get_axis("move_left", "move_right")
if direction:
	velocity.x = direction * SPEED
else:
	velocity.x = move_toward(velocity.x, 0, SPEED)
if direction > 0:
	animated_sprite.flip_h = false
if direction <0:
	animated_sprite.flip_h = true

	
if is_on_floor() and not attacking:
	if direction == 0:
		animated_sprite.play("idle")
	else:
		animated_sprite.play("run")
		
move_and_slide()

func _on_anim_finished():
if animated_sprite.animation == “attack”:
attacking = false

func _process(delta: float) → void:
var mouse_local = tile_map.to_local(get_global_mouse_position())
var tile_coords: Vector2i = tile_map.local_to_map(mouse_local)
var distance = player.global_position.distance_to(mouse_local)
# Layer 0 is assumed
var layer = 1

# Get the source ID of the tile under the mouse
var source_id = tile_map.get_cell_source_id(layer, tile_coords)

var tile_data = tile_map.get_cell_tile_data(layer, tile_coords)

	
if source_id != -1:
	if tile_data != null:
		# Example: print some custom property stored in tile data
		var block = tile_data.get_custom_data("Blocks")
		if Input.is_action_just_pressed("attack"):
			print("Block destroyed:", block.get("Name"))
			#tile_coords
			if distance <= range:
				tile_map.set_cell(1,tile_coords,-1)
			else:
				print("Too far")
		elif Input.is_action_just_pressed("put_block"):
			tile_map.set_cell(1,tile_coords,Vector2i(0,0))

TileMap source id is 0

I even made custom data in hope that It will work but I had no luck.

Appreciate any help :).

Screenshot 2025-11-24 165801

If you look into the docs, you can see all parameters for set_cell():
void set_cell(layer: int, coords: Vector2i, source_id: int = -1, atlas_coords: Vector2i = Vector2i(-1, -1), alternative_tile: int = 0)

The third argument needs to be your source_id 0, and as fourth argument you can give the atlas coords of the tile you want to place.

			tile_map.set_cell(1, tile_coords, 0, Vector2i(0,0))
1 Like

Thank you very much <3