All Questions

Community

M
marko95
posted to: Air movement

Event "jump" and unhandled_input

Hello :D

Why did you use unhandled_input fun to handle "jump"? Why didn't you use physics_process for that?

Also can you explain when it is better to use  _unhandled_input and when _physics_process to handle an Input event?

  • Nathan Lovato replied

    I'll answer your two questions at once.

    Inputs like jump are one-time triggers. The moment the player is on the ground and presses jump, you want the character to jump, right?

    But the engine calls _physics_process() every physics frame, so 60 times per second. So an expression like Input.is_action_pressed("jump") would be true every frame the player presses the jump key.

    For that reason, since Godot 1.0, the way to handle jump was to use built-in virtual functions like _unhandled_input().

    This function gets called once the moment a specific input happens, like once when pressing the jump key down, and once upon releasing the key.

    Since a recent Godot version, you can check if a key was just pressed that frame in _physics_process() by calling Input.is_action_just_pressed(...), so you could handle inputs like jump with it.

    In other words, using either _unhandled_input() or _physics_process() to handle jumps is fine these days. You can get the same result easily in both cases.

    I tend to still use _unhandled_input() for one-time inputs as this function offers some more flexibility, like you get information about the type of input event that occurred, etc.

    But this guideline's benefits are difficult to see in a tutorial, it becomes useful as your projects grow big.

  • M
    marko95 replied

    It's clearer now :) thanks