0 votes

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

in Engine by (824 points)

1 Answer

+1 vote
Best answer

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.

by (281 points)
selected by

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

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.