Input handling with Joypad

Godot Version

4.3.stable.mono

Question

Hello everyone, I am having a problem with input handling. Right now I am handling input for abilities by overriding _UnhandledInput. Like this

 public override void _UnhandledInput(InputEvent @event) 
	{
        if(assigned_button != null)
        {
           
            if(@event.IsActionPressed(assigned_button))
            {
                button_pressed = true;
                frames_held = 1;
                button_released = false;
            }
            if(@event.IsActionReleased(assigned_button) && CheckCross())
            {
                button_pressed = false;
                button_released = true;

            }
        }
		
	}

For joypad buttons that have a binary strength this seems to work fine as when the button is pressed and release only one event happens. However joypad triggers (like left and right trigger) set off multiple events. This maybe a poor way to handle input, so if anyone has a better way please let me know. It seems to me that there must be a better way of handling this.

Is the problem you are facing that frames_held is reset? If so could you add an extra check?

if(!button_pressed && @event.IsActionPressed(assigned_button))

What do you intend to happen vs what really happens?

What I intend to happen is when the assigned_button is pressed that it sets button_pressed to true, and button_released to false, and the opposite when the assigned_button is release. I have checks for if button_pressed == true, and if it is then it executes an ability. If the check is true and the ability is used it sets button pressed to false. What happens with the joypad triggers is that multiple events are called. So the trigger is press it sets button_pressed to true, the ability executes, and sets button pressed to false, but then another event happens and resets button pressed to true which triggers the ability again.

Probably best to not reset button_pressed anywhere else, if it’s not input related code it shouldn’t alter your inputs.

Maybe I should change the name to ability pressed? The reason its reset is because I don’t want the ability triggering it twice, it isn’t altering the actual input

So I switched to using Input.IsActionJustPressed(assigned_button) to handle input for abilities. But I have a queue/check systems that makes sure the ability can be used, in that system I check to see if the assigned_button is a button that changes the state of the UI (e.g. exits the UI). I think that has solved my problem.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.