Hello and welcome to another episode in the Godot basics tutorial series. In this episode we will be taking a look into the input event class in the previous episode we talked about the input propagation. Now to recap with a short summary let's say are seen tree has 10 nodes and all the nodes have input and unhindered input events. Well what we do is one we call all 10 nodes and we call their input events
And then after
All 10 have been called in after all the input virtual methods have been called. We then move on to in this case unhindered input event and then we call all 10 nodes. And that's basically it. In this case we make 20 calls in total 10 calls input event 1 call for each node and 10 calls for unhindered input event 1 call for each node. But the thing I glossed over in last episode was the input event class as a matter of fact all input events or rather all input virtual methods pass you.
The programmer and event but it just so happens to be an input event.
So let's go ahead and take a look at that. So what exactly is an input event. Well an input event is just a class. As a matter of fact that is the base class for all sorts of input data when dealing with inputs.
Godot does basically magic under the hood. But basically what Godot does is it converts the user's input data and passes down a specific class to your input virtual method. And that class holds relevant data that you can use and interpret in order to do some action for example when a user presses a button. You may want to act upon that. And so as a matter of fact, the Godot engine provides you with the input event class and the input event class has subclasses and depending on the user input Godot will pass you a very specific subclass that inherits from the input event class.
I'm gonna try something different in this episode I'm going to look at the API as a matter of fact I'm gonna post the API down in the description down below so you can take a look at all the different classes that kiddo passes to you and I recommend taking a look at that because when you're building your game you may want to use some specific type of user input in order for your game to work for example maybe your game only works on controller or mouse and keyboard. However it's always a good idea to look at the API to see what Godot is able to provide you. And we're gonna go over that together in this episode we're gonna do something different.
We're just going to look at the API especially as a beginner you may find it troubling to look at the API. You may feel intimidated but in this episode I will guide you on how to read the API in this case we're gonna work together to see how to handle keyboard input. So let's begin. So again we're in the input event class. We can see what we're inheriting from as a matter of fact our input event inherits from the resource class which is also a reference. And on top of that input event has subclasses.
Now even though we have a lot of subclasses and we have some pretty cool things like for example midi and we also have for example touch events for iPhones for example which would be the input event screen touch. In this episode I just want to focus on three subclasses the first to the joy pad button and the joy pad motion. We will deal at the end of the episode but we'll go over it briefly. However as a beginner you're most likely going to work with input event with modifiers subclass and that's because the input event with modifiers subclass deals with keyboard mouses and if you're using a laptop your trackpad.
So let's take a look at the input event with modifiers class. Now if you go in the API and you actually click on input event with modifiers you'll be taken to the input with modifiers page API page and you're gonna see that you inherit from input event but you also have three subclasses input event gestures input event key and input event mouse input event gesture is track pad gestures input event mouse is basically mouse inputs such as clicking and scrolling the scroll wheel on your mouse and input event key is basically keyboard input basically button presses over in this episode we're only going to focus on the input event key so on your API page you go ahead and click here we're gonna be taken to the API page of our input event key class and you're gonna see two things properties and methods we can ignore methods and most of the properties in this case we just want to know when a player or user has pressed a specific key if you want to know or react upon a user pressing a specific key such as moving right with the right arrow key or moving right with the letter D then we'll we're going to look into is the property called scan code.
And so you're gonna go ahead and click scan code. We're gonna be taken down in the same page we're not changing pages however you're gonna see scan code and there are two things you're gonna notice with your API. A setter and a getter setter sets a value for us at this point in time that is completely useless. However we're more interested in the get her function and to get her function lets us know that in order to receive a value the scan code value which is an integer value we have to use the get scanned code method. Now towards the bottom you're gonna see something and it's gonna tell you the key scan code which corresponds to one of the key list constants and that's the most important thing in the input event.
Key class is the key list.
So you're gonna go ahead and click key list and when you do we're gonna be taken to a different page.
It's called The Key List global in enums. Now, Godot provides us with singletons and global enums and the key list happens to be one of those global enums. As a matter of fact this is how we can tell what a user pressed. For example if the user pressed the @ key the integer value being returned by get scanned code will be 64. If the user presses the letter A on the keyboard the integer value being returned by the get scanned code. Function slash method will be sixty five and it's a list and it's a big extensive list and this gives us complete control over how we can react to certain player inputs in the keyboard and as a beginner you're gonna most likely just react to keyboard inputs and that's why I wanted to make this episode.
OK so moving on we have our global enums. However let's say for our game we just want to move right. We want to do something when the player presses the right button and the right button key right is just that right arrow key. Now the integer value being passed is this long integer value 1 6 7 7 7 2 3 3. And it even tells you right arrow key. So let's go ahead and create our function. So in the last episode we have the input now four key presses. It would be an handled input but for this basic game it really doesn't matter where.
However I would recommend to put it in the oven handle key input. But I wanted to show you how we would handle key presses if we put our code inside the input virtual method. Okay. So we have the event parameter which could dough is passing us an input event class the input event class can be anything it could be a joystick it could be a trackpad it could be button mouse MIDI touch screen etc.. However when we receive the event we need to filter it. We need to basically tell Godot that we don't care about the other events.
As a matter of fact we need to tell it that if what we're receiving in the event is an input event key class that we can go ahead and run a specific code. However if the event happens to be for example a joystick class then just go ahead and print that to the screen in your production game. You're not going to do anything as a matter of fact this code. I'm going to upload to get hub for you to play around with. OK. So how do we filter what class we're receiving from Godot. In the event parameter. Well first we use an if statement if our event is an input event key and basically what that means is if the event happens to be the class input event key we're gonna go ahead and run our code.
Now when we run this first line of code event could be Q W E one two three etc. However we only care about one specific key which is the right arrow key and so because of that we need to write a second if statement and so we say if our event gets scanned code is equivalent to 1 6 7 7 7 2 3 3 which is the integer value for the red arrow key we are going to print to screen right key press and there is in fact an easier way to handle key inputs and we're gonna look at that at a later episode.
However if you wanted to get down and dirty with code this would be the way to do it. However we can make this easier because we don't need this integer value and so instead of the integer value we can replace it with the e new value. And if we go back or enum value was key underscore right. All capital. So remember it's case sensitive these global enums. So as you can see here none of the code has changed. However instead of using the raw integer value we instead use the enum name and this code will do the same thing.
If we press the right keyboard we're going to print this screen right keep press. Else if it's any other value it's going to be printing to our console. However there is a third way and it's not a parent. When you read the API documentation if you follow it along with this episode. However if you read other code in the Godot documentation you may see something like this event that's and code event that scan code is exactly the same as event.
Get scan code and really we're still getting that integer value that Godot is passing to us.
So Godot again is passing us an event whether that's mouse movement or joy pad. Button pressing and then our first if statement filters because we only care about the input event key class and then in the input event key class we only care about the specific scan code of the key right or the right arrow key being press. And as a matter of fact event that scan code is another way to do get underscore scan code so get underscore scan code event that scan code and then after if this passes we print to the screen and as you can see here no matter which way you write your code we're going to print to the screen when we press the right arrow key.
Right keep pressed. And then when I move my mouse for example like this you're gonna see this and put event mouse motion, and that's basically the else statement. So again input event mouse motion is the s statement. And that's rather this is one way that we can write input detection from the player. And this is only specific to key presses. We can get as complex as we want to or we can make it easier on ourselves and we'll look at that at a later episode because as a matter of fact the allows us to use the project settings to set our own key input mappings.
So Lastly, let's take a look at controller some I received maybe two questions about controllers and it's fairly straightforward. However it's a little complex if you don't know how to look. I certainly did not know where to look when I started with Godot. So basically controllers will include P.S. 4 controllers, x box, steam controllers etc.. I'm not sure about how P.S. 4 works at least for me. Three years ago it required at least for my computer a plug in. I had to download that which the computer would handle the conversion but I do know for X bucks and steam controllers plug and play a support it.
I may be wrong if I'm wrong please post something in the comments section down below. However when you're dealing with controllers there are two classes we need to look at input event Joy pad button an input event droit pad motion the button handles button presses on your controller and then the joy pad motion handles. Basically your joysticks and so as a matter of fact if you connect to your computer and you run this code you're gonna see these classes being called in the print statement for the else statement. So again go ahead download that from get hub and play around with different controllers perhaps plug in your midi keyboard.
Anyway moving on something you may not know as a beginner especially if you're a beginner is that not all controllers are supported by Godot. For example when I use my end video controller on Mac OS the left trigger and right trigger buttons do not work but everything else is fine and that's just because there's so many controllers and so many different operating systems and devices that Godot doesn't have them all or needs to update certain things. And so in order to see if your controller is compatible with Godot I'm gonna post a link down below and it's basically going to be a list that lets you know which controllers work with which devices we won't go into too much detail about controllers in this episode.
However just keep that in mind. Not all controllers will work for every operating system. However if you know what you're doing and I plan on doing this later in the future is that you can submit something. Very simple mapping that lets Godot know which device you're using on which controller or rather there's a way to submit. I won't go too much into submission so if you for example download the github file and you play around with your controllers and it doesn't work. Check the link down below because it may not be your programming skills it may just be that Godot doesn't support your controller with your specific device slash operating system.
Well that's all I have for you in this episode. Again if you're a beginner don't forget to download the get hub file and player around with input controls or input of and controls. Anyway thank you so much for joining me in this episode. Thank you for clicking the Subscribe button. Thank you for clicking the Like button. I truly appreciate your support. If you have any questions or comments please feel free to leave them in the comments section down below and I look forward to seeing you in the next episode. Have an amazing day.