The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

0 votes

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 "oncursormoved" function)

Thank you!

Godot version 3.4.4.stable
in Engine by (12 points)

1 Answer

0 votes

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)
by (64 points)

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!

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 Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.