_gui_input event position is doubled under weird circumstances

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: 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
			#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);
				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:

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

:bust_in_silhouette: Reply From: Spuz

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!