0 votes

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()
asked Jan 6 in Engine by Robster (808 points)

1 Answer

0 votes
Best answer

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.

answered Jan 6 by Robster (808 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.