How to - Input

Whaddup and welcome to another tutorial. Today I’ll give you a basic rundown of Input and Input methods in #Godot.

I don’t have to tell you out there why User Input is important when it comes to games but there are so many methods of getting input from a user dependent of the platform of your game. I mean…. Think about it. If you develop a game for the Desktop PC market you have mice, keyboards, even game controllers as your input devices. But if you look at the Smartphone app market your options are limited to what you have on a phone, so a touchscreen, maybe a gyrometer…. That’s it.

Luckily it’s very veeery easy to target all of those input devices in Godot. So let’s get started. :) If you create your project you’ll have some preset values for input mappings in your project settings. In there you have a separate tab called “Input Map”, where you have every input option layed out with a respective name. This name will come into play later. :) Inside you can add, delete or modify your different input options.

You can add a new “group” by typing a name for it and click on the “Add” button. To add a new input option you can click on the “+” icon. A little dialog box pops up where you can choose your input option. Just as a little bit of information. The mouse option is also for your touchscreen input since a left mouse click is registered as the same input type for a phone.

If you chose your input option you can then enter your specific input, like a specific keyboard key, or a game controller button or even a mouse button. The specific dialog box depends on your input type. Then this part is done… You’ve added an input to the input map. Finally you can modify/delete your input map by clicking the corresponding pencil or trash can icon.

Now we can use this stuff for our code. In Godot there is a specific class called “Input”. In there you can reference different methods which register when you have pressed, released, just pressed and so on. There are waaay too many different methods to cover in this post so check them out for yourself. :) The main giveaway should be that there is this Input class, there are those methods but basically all of those different methods reference this “group name” I mentioned earlier with a simple String with the name.

But how can you use this? There are two main ways you could do this. First of all there is a own function called “_input(event)” which triggers every time ANY type of input happens from any device. In there you can check the type of the InputEvent object inside the parameter if it matches your input group with the methods I mentioned earlier, or if it is any other type of input. The possibilities are endless. And then you can execute your code that should happen in this input event.

At last you can use the “_process” function which gets called every frame. In there you can check with an if-Statement and the corresponding input method which by the way nearly all return boolean values if an input happens or not. I personally recomment the first method, since this separates your input logic from your game logic but you can choose yourself. :)

And that’s basically it. Now you can run your program and test it out. In the example video I just modified a demo project and as you can see, different events happen if you press a different button. So mission accomplished. :) There are as usual many other aspects but this should only cover the basics of input.

Have a great day. :)