All Questions

Community

S
nodeg

set_input_as_handled()

What exactly is the purpose of set_input_as_handled()? I mean, I read the docs, which don't say much more than the function name.  I'm really curious.  I've used similar event systems, and I've never once used this.  I also removed it from the project code, and everything seemed to work fine with no errors. I imagine that it leads to removing the input from the event system after processing?  I'm just curious what the advantages are.

  • Razoric replied

    When Godot sends an input event, it goes through a certain lifecycle:

    It begins at input, then goes to gui input, then goes to unhandled input. If the event is not handled by the GUI automatically, it will carry on from child to parent until something goes handle it, or it reaches unhandled input.

    Calling set_input_as_handled() forces the event to get handled so that it will not carry on any further than that script.

    As for the advantages, imagine a game where you use gamepad to move the player. You bring up a menu in the game, like dialog or something, and you want to also use the gamepad to control the GUI. But the game is still running in the background! You could explicitly handle it by going "if not menu.is_open" all over your player's code.

    Or you could handle the input event for the gamepad inside of your dialog GUI so it doesn't manage to reach the player.

  • S
    nodeg replied

    Thanks.  This clarifies things nicely.