Godot Version
Godot 4.3
TileMapLayer.Set_cell()
Hi there, a beginner at Godot here,
I wanted to make a real simple game where you placed a farm tile, watched it grow then harvested it, as practice for programming with GDScript, but I fell at the first hurdle and I have no idea why…
As the title implies, for whatever reason, when I get the mouse position from the input and translate that to the isometric grid I have, then try to place a tile at that grid position, nothing happens. I tried with the default set_cell
values and the tile isn’t even erased.
I’ve tried printing the coordinates out and they seem to be mapping to the grid perfectly fine, I believe it to be a problem with how I’m setting the cell, but I can’t figure out what the problem actually is.
Can anyone tell me what I’m doing wrong?
(and sorry for any terrible GDScript text styling crimes, I’m still getting used to it)
extends Node
@onready var tile_map_layer: TileMapLayer = $"../TileMapLayer"
@export var farm_tile_id: int = 4 # The ID of the farm tile
func _input(event):
if event is InputEventMouseButton and event.pressed:
# Get the global mouse position
var mouse_position = get_viewport().get_mouse_position()
# Convert the global mouse position to local position in the TileMapLayer
var local_position = tile_map_layer.to_local(mouse_position)
# Convert local position to map coordinates
var tile_position: Vector2i = tile_map_layer.local_to_map(local_position)
# Set the tile at the tile_position in the TileMapLayer
var source_id = tile_map_layer.get_cell_source_id(tile_position)
print("Tile source ID at position ", tile_position, ": ", source_id)
tile_map_layer.set_cell(tile_position, source_id, Vector2i(0, 5), 0)
# Tried to force an update to ensure changes are rendered
tile_map_layer.update_internals()