Remembering keyboard "states" for processing later (or) how can a function remember its last run variables?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Robster
:warning: Old Version Published before Godot 3 was released.

Hi all,

I’m trying to build my own keyboard input state machine. It’s my first ever state machine and warning: I’m a new programmer.

I need to differentiate between:

  • Key just pressed
  • Key pressed and held down
  • Key just released
  • Key not pressed

I’ve been using https://github.com/ndee85/gBot/blob/master/projectfolder/scripts/input_states.gd as a test to prototype but am finding it buggy and to be honest I want to learn how to do it myself.

I’m slowly getting to grips with his code but wanted to ask a question that I don’t understand in his code.

He is calling the check() function each time a key is pressed.
It appears check() is then remembering the last state so it can update its current state.
How can it do that if the function is being called anew each time?

I would have thought if I called a function like check() in his code it would simply come in to a fresh, newly access function and be resetting itself and its variables each time.

What am I missing here? I really want to understand this so I can create my own version and control it properly.

Any help or advice would be greatly appreciated. Rob

:bust_in_silhouette: Reply From: ludosar

The variables are not reseted because they are declared outside the function, at the beginning of the script.

Therefore, current_state has still the value from the previous call.

Of course! Thank you so much. You can see I’m new at this but it’s all forming shape. Appreciate your help.

Robster | 2016-08-25 21:10