In my project I have a GUI panel with a few buttons that appears when (in Switch Pro controller terms) Start is pressed. This pauses the game. After that the menu can be closed by either pressing Start again or by selecting an option by pressing A.

So far so good, everything works great. But the problem is that A is also mapped to player character's jump. So when I press A the menu closes and at the same time the character jumps too.

The menu is on the bottom of the tree and is handling inputs in a func _input(event) function, and if an event is registered and actions applied, it finishes with
get_viewport().set_input_as_handled()

The player is handling the input in a _physics_process(delta) using various
if Input.is_action_just_pressed("button_action")

I thought that by marking an event as handled, it will not propagate to the player, but obviously I have a fundamental misconception of how this works. So I tried various combinations with "_unhandled_input" and similar, but can't make it work properly.

How can I prevent input from the menu to affect the player too?

  • xyz replied to this.

    guduq Querying the Input singleton will not obey handled/unhandled state. That's why it's not a good idea to mix event based input and reading from Input directly. You can either adapt your code so it uses events only or if you can't live without Input, you can introduce your custom "input handled" flag that's set by the gui code, and check it before accessing Input in player code.

    Thanks, I'll try not to do it like this in the future (this is my first ever Godot project, still discovering how it all works… or doesn't 😃 ). For this particular case I'll just try to convert it to events only and see how it goes.

    Maybe something along those lines:

    func _unhandled_input(event):
        if event.is_action_pressed("jump"):
            jump()
            get_viewport().set_input_as_handled()

      Toxe Shouldn't I (in this case at least) mark the input as handled in GUI (not in player)? Since GUI happens before the player (and the rest of the game is paused while the GUI is active).

      I guess the first thing I should try is to rewrite the player input to use unhandled_input instead of the current setup (where player is reading inputs from within a "process" function that contains various "is_input_just_pressed").

      Thanks for the answers guys!