Godot 4.3: TileMapLayer.set_cell(), is it not working, or am I bad at programming?

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()

The print statement:

print_statement

The Hierarchy:

Hierarchy

The Atlas tile set:

What does your print statement return?

I assume you’ve already painted the cell onto the tile map layer before runtime.

Ah sorry, entirely slipped my mind to upload the images of my debug console etc. I’ll add them to the post. :sweat_smile:

After a little testing I couldn’t replicate your issue exactly, but I did find a possible fix.

mouse_position is getting the local mouse position within the viewport rather than global position of the mouse. You can use get_global_mouse_position() instead.

You’ll need to call it from a node which inherits from CanvasItem though. Node doesn’t, but your script is attached to a Control node which does. You’d just need to update the extends statement:

extends Control

func _input(event):
    if event is InputEventMouseButton and event.pressed:
        var mouse_position = get_global_mouse_position()
2 Likes

Thanks, that works perfectly, i was tearing out my hair trying to figure out what I was doing wrong.

1 Like

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