Why does getting event.position in Control._gui_input() obtain both the coordinates in the Viewport and the coordinates in the Control?

Godot Version

Godot4.3

Question

I implemented the dragging function of a Control node with the following code. However, I found that when using the signal gui_input to implement dragging, the dragged object will repeatedly jump between two coordinates. Then I used print(event), and the output result is as follows:

InputEventMouseMotion: button_mask=1 (Left Mouse Button), position=((114.5, 131)), relative=((1, 1)), velocity=((73.73748, 27.65156)), pressure=1.00, tilt=((0, 0)), pen_inverted=(false)
InputEventMouseMotion: button_mask=1 (Left Mouse Button), position=((1743.5, 883)), relative=((1, 0)), velocity=((73.73748, 27.65156)), pressure=1.00, tilt=((0, 0)), pen_inverted=(false)
InputEventMouseMotion: button_mask=1 (Left Mouse Button), position=((114.5, 132)), relative=((0, 1)), velocity=((73.73748, 27.65156)), pressure=1.00, tilt=((0, 0)), pen_inverted=(false)
InputEventMouseMotion: button_mask=1 (Left Mouse Button), position=((1744.5, 883)), relative=((1, 0)), velocity=((73.73748, 27.65156)), pressure=1.00, tilt=((0, 0)), pen_inverted=(false)

I consulted the documentation, when received in Control._gui_input(), event.position should return the mouse’s position in the Control using the local coordinate system of the Control. However, my output results suggest that it might be retrieving the positions from both the Viewport and the Control at the same time. Why is this issue occurring?

The following is my code.

func _on_card_ui_gui_input(event: InputEvent) -> void:
	if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT:
		$CardUI.accept_event() 
		if not dragging and event.is_pressed():
			dragging = true
			top_level = true
		elif dragging and not event.is_pressed():
			dragging = false
			top_level = false

	if event is InputEventMouseMotion and dragging:
		$CardUI.accept_event()
		position = event.position
		print(event)

2024-09-25 120449

My guess is that your script is receiving gui_input events from the child nodes. Try adding a print that shows where the event originated from.

Next, go trough your child nodes and make sure the mouse filter Control — Godot Engine (stable) documentation in English is set to ignore.

1 Like