![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | JohnTheFisherman |
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.
I have band-aid fixed the bug by using get_local_mouse_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.
JohnTheFisherman | 2019-11-12 13:59