Input versus _input(event)?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Diet Estus

I have been using a custom input processing function that references the Input singleton:

func input_processing():
    if Input.is_action_pressed("right"):
        ...

I call this function in _physics_process() before my physics code.

I could achieve the same effect using the virtual function _input(event). For example,

func _input(event):
    if event.is_action_pressed("right"):
        ...

Is there an advantage to using _input(event) over the custom function which references the Input singleton?

:bust_in_silhouette: Reply From: alfandango

Input is an OS specific global singleton object created by the system when the game is loaded. It stores the input_map and (AFAIK) binds the system InputEvent(s) object so that it can track inputs from the system during the game loop. It is independent from any specific Node or Scene.

_input() is a node specific method which is fired only when an input is detected and does something. It can not for example keep track of whether a key or mouse button for example is being held down or (I assume) when it is released.

This is only my understanding so far,I hope it helps.

It can not for example keep track of whether a key or mouse button for example is being held down or (I assume) when it is released.

I’m not sure this is entirely accurate. event.is_action_pressed(..) has an optional bool for allow_echo, a method event.is_echo(), and there is also a method event.is_action_released(..).

Is there more information on how “Node specific method” vs “OS specific global singleton” which “is independent from any specific Node or Scene” could affect things?

batrocity | 2020-06-10 14:11

Just just want to point out, that it looks like allow_echo and is_echo are only for InputEventKey which are keyboard events. Not mouse or joypads.

So for example, you can’t have a joy button held down and press another joy button at the same time if you use _input(event), however it will with Input.

roboeast | 2021-05-31 05:44