All Questions

Community

O
Rabbit One

_unhandled_input(...) vs _physics_process(...) to use is_action_pressed

At min 3:50 of the video you use these two codes:

func _unhandled_input(event: InputEvent) -> void:
  if event.is_action_pressed("click"):
    ...

and

func _physics_process(delta: float) -> void:
  if Input.is_action_pressed("click"):
    ...

So, what is the difference of using event.is_action_pressed in the first piece of code against using Input.is_action_pressed for the second code?

Does any difference to use _unhandled_input or _physics_process to deal with InputEvents?


Thanks!

  • Nathan Lovato replied
    Solution

    Here is the difference:

    In physics process, we poll for input every physics frame, 60 times per second by default. Input.is_action_pressed checks if one of the buttons or inputs in the corresponding action are pressed that key.

    The _input callback functions, on the other hand, receive each key press and inputs separately. For example, once when you press the left mouse button down, and another time when you release it.

    In the input callback, event.is_action_pressed checks if you clicked and if the button is down, as opposed to being released. It only happens once upon pressing the key, giving us a nice check to start or trigger an action.

    There is an equivalent method on the Input singleton you can use in physics_process, Input.is_action_just_pressed. But a limitation of physics_process is you can't mark input events as handled, so they'll propagate through the scene tree. That's why you generally want to process input events using the input callbacks, unless you need to check for some input every frame (like when calculating a character's move direction).