Use _Input(event) or _physics_process(delta) - Performance? - Input lag isn't a problem

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

Hey guys,

should I put like everything that is possible into _Input(event) or in _physics_process(delta) (which is called 60 times a second in the project). I have a couple of keys that are never hit or only hit once in a game session (a key to toggle fullscreen for example). Input lag isn’t a problem for these keys, so I wondered about the performance even if there’s only a tiny difference I’m mostly curious.


:bust_in_silhouette: Reply From: leo-pnt

I think it’s your responsability to put you key in the right category

If the action is continuous in time, you can create a function like this

func apply_inputs(delta):
    if Input.is_action_pressed("your action"):
        # do things

And then you call it in your _process() or _physics_process() if the thing you want to do is related to physics i.e. body movement etc…
It just makes your code input-dependant so performance is only impacted by the ‘ifs’ I guess

func _process(delta):

If it’s just an event that happens only one time (like shooting a rocket) you can use _input()so that your code doesn’t have to check every frame process if the key is pressed

func _input(event):
    if event.is_action_pressed("your action"):
        # do things
        OS.window_fullscreen = !OS.window_fullscreen # for instance

So, it’s better for performance because all of the ifs aren’t checked 60 times a second? I didn’t knew about _input() and put everything in physics_process and just wanted to check if it would be better if I put it in _input() even if it is just a tiny bit more performant.
My thought is just: Pretty much all the time one key is pressed in the game (for walking) - but won’t it check _Input() then and go through the whole if tree there and now all the time even if the walking is still in _physics_process? Instead of 60 times a second?

Thank you very much for your answer!

SmokeSomeFrogs | 2020-12-16 12:04

Yes _input() is called only one time at each event instead of each frame. If you consider your key as a single event, use _input(), else, use _process()

If you have hundreds of key to check at each frame, this will have impact on performance

I just found this post where it is well explained

leo-pnt | 2020-12-17 15:31

Thank you for your time!

SmokeSomeFrogs | 2020-12-20 19:39