Check if joy button is just pressed

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

I’m making a local multiplayer game and I don’t want to have to have input events for action for every player. Instead I resorted to using


for my various input. This led to some problems though because you can hold down the attack button, for example. Is there an easy way to implement an “is joy button just pressed” function or what is the best way to program this?

Thank you in advance!

:bust_in_silhouette: Reply From: jgodfrey

So, you just want to ensure the button is released prior to another press? In that case, you just need some sort of gatekeeper mechanism. Here’s a simple (though untested) example:

var joy_button_pressed = false

func _process(delta):
    if Input.is_joy_button_pressed():
        if !joy_button_pressed:
            joy_button_pressed = true 
            # do button pressed processing here 
        joy_button_pressed = false

If you need to manage multiple buttons independently, you’d want to use an Array or Dictionary to track the current state of each instead of the single joy_button_pressed bool in the above example.

That was really similar to the solution I was about to use! I just wanted to make sure that there was a better way first. Thank you!

Nathcra | 2022-05-03 15:21