+2 votes

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?

in Engine by (1,547 points)
edited by

1 Answer

+2 votes
Best answer

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.

by (76 points)
selected by

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?

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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.