+1 vote

I am playing around with a diablo-style grid inventory using a TextureRect for the grid. My _gui_input is as follows:

var drag = {"item": null, "original_position": null}

func _gui_input(event):
 if event is InputEventMouseButton and event.button_index == BUTTON_LEFT:
        #Try to pick up and item
        if event.pressed and drag.item == null:
            var slot = get_slot_from_vector(event.position);
            if is_slot_in_bounds(slot):
                drag.item = occupied_grid[slot.x][slot.y];
                if drag.item:
                    drag.original_position = slot
                    remove_item_at(drag.item, drag.original_position);

        #Drop held item into the grid
        if !event.pressed and drag.item:
            var slot = get_slot_from_vector(event.position);

            #These should be identical
            print(event.position)
            print(get_local_mouse_position())

            #If we can't place it here, move it back to the original spot
            #Otherwise, move it into slot
            if check_space_occupied(drag.item, slot):
                add_item_at(drag.item, drag.original_position);
            else:
                add_item_at(drag.item, slot);

            drag.item = null;

Since the node containing this script is centered at (0,0) alongside the camera, my mouse position and event.position should always line up. This is the case until I add some additional code that sticks the currently dragged item to the mouse.

func _input(event):
 if drag.item:
    drag.item.set_position(event.position)

Once this code is added, I find that when I let go of the mouse, the event.position value for the !pressed event is always double what the get_local_mouse_position() is. This obviously causes items to get dropped in the wrong place.

The ONLY thing I am changing between these runs is the addition of the _input function, but I cannot figure out why it is causing the event.position in _gui_input to be doubled.

asked Nov 11, 2019 in Engine by JohnTheFisherman (13 points)

I have band-aid fixed the bug by using getlocalmouse_position() instead of event.position, since the former appears to accurately represent where the mouse is, whereas event.position just seems wrong. I am not sure if this is a bug or not, so if anyone figures out why this be, I would be super interested.

1 Answer

+1 vote

I wish I could help you, but I am likewise having a similar problem with newly spawned instances. The 2D position seems to always be scaled twice to what I set it as. I eagerly await a response for this one and wish the best of luck to us both!

answered Nov 11, 2019 by Spuz (16 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.