Get discreet input from analog stick

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By ardklaw

Hi!

My game has an inventory screen. You can hover a cursor over each item in the inventory screen. This works fine with the keyboard, but with the gamepad’s analog stick, the cursor doesn’t move one item at a time - it moves several items at a time, very quickly.

enter image description here

I want to get discreet inputs from the gamepad’s analog stick. So, for example, when the player holds right on the analog stick, the cursor will move right exactly once, and will only move again when the player holds the stick in another direction.

Here’s the code that handles the player’s input for the cursor:

func _input(event):
	var input := Vector2.ZERO

	if is_visible():
		if event.is_action_pressed("mup"):
			input.y -= 1
			emit_signal("cursor_moved", input)
		if event.is_action_pressed("mdown"):
			input.y += 1
			emit_signal("cursor_moved", input)
		if event.is_action_pressed("mleft"):
			input.x -= 1
			emit_signal("cursor_moved", input)
		if event.is_action_pressed("mright"):
			input.x += 1
			emit_signal("cursor_moved", input)

(The cursor’s actual movement is handled in the “on__cursor__moved” function)

Thank you!

:bust_in_silhouette: Reply From: aeh_keine_ahnung

Try using Input.is_action_just_pressed("your_action")
instead of event.is_action_pressed(your_action)

func _input(event):
var input := Vector2.ZERO
if is_visible():
	if Input.is_action_just_pressed("mup"):
		input.y -= 1
		emit_signal("cursor_moved", input)
	if Input.is_action_just_pressed("mup"):
		input.y += 1
		emit_signal("cursor_moved", input)
	if Input.is_action_just_pressed("mup"):
		input.x -= 1
		emit_signal("cursor_moved", input)
	if Input.is_action_just_pressed("mup"):
		input.x += 1
		emit_signal("cursor_moved", input)

Thank you for the response!

That’s a great idea. I tried it, and the cursor no longer slides around wildly, though unfortunately, it still won’t move one item at a time - it wants to move two items at a time for some reason. Weird!

ardklaw | 2022-08-13 20:22