There are not many things more essential to a video game, than the user input. In fact, it is what differentiates video games from any other medium. So obviously, input plays a major role in game development. Now in this post, I want to talk about the different ways, in which you can handle input in Godot. Also, we want to take a look at the different input methods too. So, essentially there are two main ways to handle input: Either you will handle the input inside the _process(delta) method, or you handle the input event inside the _input(event) method. There are some differences when it comes to the two approaches. But we will touch on them later on.
How to Handle Input in Godot? With Input Map!
Generally, I would recommend to always work with the Input Map in Godot. If you don’t know what it is and how to work with it, please check out this tutorial. An Input Map is useful for a variety of reasons. For example, you may want different buttons to perform the same action. This is no problem with the Input Map. Or if you want your players to customize their controls, you also should use the Input Map (check the aforementioned tutorial for that too)
There is not really an argument for not using the Input Map, as far as I am concerned. But for the sake of completion, I will also show, how you can do it with the key directly.
So, the first way to handle input in Godot is to poll an event inside the _process(delta) method. For example, you can print something, as long as a button is pressed (let’s say the shift-key). I will show you the code for the key directly, as well as via the Input Map. For the Input Map to work, you first want to name a new input event, in our case we call it “STING”. Again, if you are not sure how to do that, please have a look here first. When that is done, you can refer to it like this:
# Key Directly
# Input Map ("STING" has to be defined in the Input Map)
There are different possibilities when it comes to how exactly you want to handle the inputs. For example, you can use the following code to refer to the input only once in the first frame and the last frame respectively (do not forget to set up the aforementioned Input Map).
print("ka...") if Input.is_action_just_released("STING"):
Check out the official documentation for more information on the different methods.
There is also the possibility to register mouse inputs. You can either assign a mouse input to actions in your Input Map, or you can use this code to handle input from your mouse directly.
Again, when it comes to all the different input devices (for example mouse, gamepad, etc…), the easiest way is to use the Input Map.
The other possibility is to handle every input event, that gets registered. You can do that in the _input(event) method. Say, for example, you want to print “jump” whenever you press the space bar. You can do that like this (do not forget to create the “JUMP” action in the Input Map):
if event is InputEventKey:
if event.scancode == KEY_SPACE:
Here too you have different possibilities for how exactly the InputEvent should be handled. For example, you can use event.is_echo() to make sure, the event is only once registered. Further, event.is_pressed() can be used for only registering the first time the button is pressed. The same is true if you use event.is_action_pressed() or event.is_action_released() respectively. Try out the following code:
if event.is_action("JUMP") && !event.is_echo() && event.is_pressed():
print("Up and down!")
It may also be useful to check if the InputEvent is a certain kind of type. For example, you may want to check if the input event was caused by the mouse:
if event is InputEventMouseMotion:
print("Leave the mouse alone!")
How to Handle Input in Godot — Unhandled InputEvents
Also, if you want, you can catch all unhandled input events. You can use the following code to do that:
print("There was an unhandled event!!!")
So, I hope this was useful to you. If it was, let me know in the comments below. Also, check out other Godot topics here on my website.
Originally published at www.gotut.net.