This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
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()
in Engine by (838 points)

1 Answer

+2 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.

by (838 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 Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.