Getting mouse location with "event.position" makes tilemap's "world_to_map" give incorrect co-ords as the camera moves

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

Hi all,

I have a tilemap and when I click on it I want it to “paint”.
This is working, but there is an offset. The tiles paint beside and lower down from the mouse, not straight under it. Then, as the camera moves, the offset grows. Which makes me think the event.position is reporting incorrectly to the world_to_map due to some local space / world space issue. My code is below. What am I doing wrong? Thank you.

func _input(event):
    if globals.activity == "dig":
        var button = event.get_button_mask()
        # we do this so we can click and drag
        if (button == 1):
            var tile_pos = get_node("/root/GameLevel/TileMap").world_to_map(event.position - Vector2(get_node("/root/GameLevel/Camera2D").get_h_offset(), get_node("/root/GameLevel/Camera2D").get_v_offset()-10))
            print(tile_pos)
            var name = get_node("/root/GameLevel/TileMap").tile_set.tile_get_name(28)  # tile 28 is the colour in tile
            get_node("/root/GameLevel/TileMap").set_cellv(tile_pos, get_node("/root/GameLevel/TileMap").tile_set.find_tile_by_name(name))
            get_node("/root/GameLevel/TileMap").update_dirty_quadrants()
:bust_in_silhouette: Reply From: Robster

aaaand I found it!

var mousePosition = get_node("/root/GameLevel/TileMap").world_to_map(get_global_mouse_position())

		var name = get_node("/root/GameLevel/TileMap").tile_set.tile_get_name(28)  # tile 28 is the colour in tile

		get_node("/root/GameLevel/TileMap").set_cellv(mousePosition, get_node("/root/GameLevel/TileMap").tile_set.find_tile_by_name(name))

get_global_mouse_position was the answer.