Hello and welcome to another episode in the Godot basics tutorial series. In this episode we will be taking a look at player inputs. So what exactly is a player input. Well a player input is basically any information that the player gives your game. Some of these inputs would include mouse movement mouse clicks keyboard inputs controller input even inputs provided by your VR headset. Now Godot is a very flexible application. It allows you to solve a problem in a variety of ways such as how you can program handling player input.
Now the easiest way to handle keyboard input is through the Godot application under Project settings you'll find a tab called input map and from there you can give your keyboard input a name and map that specific name to a specific keyboard input. We will take a look at this in another episode this episode we will be looking at the different functions that the Godot node class provides us in handling different types of inputs.
One thing to note is that the Godot application gives us four different virtual methods that allow us to handle inputs. They are the function input virtual method the geo AI input virtual method the Unhandled input virtual method and the unhandled key input method. As a matter of fact we'll take a look at this more in the next slide but you should note that there is a propagation for player input among these four virtual methods.
That means that we get an input from the player. We pass it first to our input virtual method. Then we pass it to our G U I input virtual method and as a matter of fact if I input virtual method gets called we stop the propagation chain. However if this does not get called we move on to the next function which is the unhandled input function we pass our input event here. Then lastly we pass our input event into our unhandled key input. Let's go ahead and take a look at that.
So we have our user and let's pretend our user presses a button or a key on the keyboard or that input gets passed to our OS class or OS class passes that input into the same tree the same tree passes that input event to the viewport and then our route viewport passes it to the nodes from a bottom to top order. So let's start with the first function the input function. All our nodes will get their input. Virtual method called and then activate whatever code logic is inside our input virtual method.
After all the virtual input methods have been called. We move on to the next thing and the next thing would be our GUI input. Just a quick note, GUI Inputs are only allowed to be used inside of nodes that inherit from the control node and control nodes or basically any node that handles graphic user interface. That would include buttons and text areas. Now going back to the diagram the second thing to be called will be argue I input. Now this is the very interesting thing. If
If G you I input catches an input event we stop the propagation chain. We don't go to 3 4 and 5 we just stop right here. However if we don't catch it in the GUI input we pass it along to the next function which is called the unhandled input virtual method. Now with the unhandled input virtual method, we will again call all the nodes with this function and then we pass our input event into the collision detection input event or basically any collision objects such as area.
We'll go more into this in later episodes but it's good to know that collision objects will or rather they have the ability to handle input events. And then lastly we go to the root viewport and we can ignore 4 and 5 because we don't need to worry too much about that. All we need to worry about is just the three virtual input function the virtual GUI input function and the unhandled input function. Now let's look at the bottom to top order or nodes will get called in. So as you can see here we have a root no child node some children and this is our control node.
Control nodes are easy to notice because of the green color that represents their little icons. In this case we have a script only at the bottom. However imagine if we had scripts everywhere.
As a matter of fact let me go ahead and change this.
Okay so now we have the scripts attached and basically we start from the bottom and we start with the input virtual method and we started GUI our control Class G Y and we move up.
So first we see if our GUI method has the input virtual method.
If it does we call it and we move up to the next node on top. And then we do the same thing we see if it has an input method call its input method move up and we basically keep doing that unless one of these nodes manually stops the propagation call we're gonna look at stopping the propagation call manually towards the end of the episode. It's just one line of code. So once we've caught all the overridden input virtual methods inside each node. We start from the bottom again but this time we look and see if the GUI input virtual method is overwritten.
So in this case we call GUI and we see if it has the virtual method. If it does we call the GUI input virtual method and then we stop the propagation chain. We don't go up. We don't do anything. However let's imagine that the GUI input virtual method has not been called. We keep moving. We're saying if there are any control nodes and because there aren't we just go back to the bottom and we start. The third function which is the unhandled input virtual method. So we start here again. GUI at the bottom we see if it has the unhandled input virtual method called and if it does we call the logic inside it.
We move to the next node on top. We call there unhandled input virtual method and we just keep going up till we hit root again. And that's basically it. That's basically the bottom to top order of how our input virtual methods get called moving on. So they function input which takes in an input event is the first input method that gets called out of all the input methods. Godot provides you as a matter of fact you can do a lot of things here you are free to put custom player movement code here however it's recommended to put that any different function.
Now keep in mind that if you do end up using an event and input you have to manually exit the input chain. If you do not exit the input chain the input event will get passed along to the other functions. The next input event that gets to use the player input is called the G UI input the G UI input is the second input function that gets called after the input virtual method and before the UN handled input virtual method.
Now this is a very special function because this function can only be used by classes that inherit or nodes that inherit from the control class. As a matter of fact if the G UI input uses the input event propagation of the event handling chain will be stopped here as a matter of fact. You do not need to manually handle stopping the input propagation. Now this reason is simple and we'll look at that next. As you can see here your control class inherits from the canvas item and canvas item inherits from node so that means 1 you do get the G UI input virtual method and 2 you get the virtual methods provided to you by the node class and that includes the input and unhandled input virtual methods.
Now the control class gives you cool graphic user interface features such as buttons text pop ups and a lot more. Now the reason why the UI input stops the propagation calls for your event chain is because when you're dealing with for example a button press you would most likely want to stop the ability to call on the other input functions in your input function propagation chain. So let's take a look at the propagation input chain again. First you go through the input function then you go through the control nodes.
Input function which is called the G UI input function. And if you do capture your event here you do not want to pass it to the UN handled input virtual method. And we're gonna go ahead and take a look at that next. I want to reiterate at least for the G I input as you can see up here we do not need to manually stop the propagation chain. Now the third input virtual method that gets called in the input virtual method chain is the UN handled input as you can see here it takes another input event and this is the third function that gets the chance to use the input event that gets passed to your function as a matter of fact the function called unhandled input is the best place to have your code that deals with your player input controls for example moving your hero up down left direct.
Now the reason you want to have your code and unhandled input is because this way the player controls does not interfere with the control node input handlers. So let's take a look at that again. So I'm back here in the diagram and first our input event is handled through the input function. Next we go through the control node input and that's the reason why I have this in yellow again is because if we are able to handle our input event and the control node input the G UI input our chain steps here.
However if our chain doesn't step here we move on to other handled inputs. So imagine this you have a hero you control it an unhandled input but you have this big menu for example you want to pause the game so you have a big menu and you want your user to click on items such as resume help etc.. Well when you're player clicks on something you would like it to for example activate a button but you would not want to activate shooting a gun if your player shoots a gun. When you click on the mouse button and that's one reason why we want player controls and handle input versus input because if you had your player controls and input and you had your for example your pause menu which would most likely be a control node object on your game screen your player will shoot the gun even though you want to click a button and that is probably something you don't want.
So to reiterate the way Godot handles input is 1 through the input function to through a g UI input function 3 on handled input and then of course it passes along to collision detection. And then lastly if nothing handles it it goes to the root viewport. Now it is for that reason I just described that the unhandled input is the best place for player controls. Now there is a second class called function on. Key input and that also takes an input event.
Now this will only get cold or rather this function will only get called if a key press is detected. And that means that it will only work with key inputs. So for example mouse movement does not activate this function. So on top of the UN handled input virtual method. Consider the UN handled key input virtual method. If your game only relies on key presses now we won't go into too much detail in this episode but the fourth input handler to be called is the input event given to you by collision objects.
Basically any collision object is a node class. It also gets inherited by area and physics body. So just keep that in mind. Collision objects have their own input events and we will worry about this in a later episode but just keep that in mind that this is the fourth input handler to be called and of course the last thing to be called will be your root viewport if input chain has been on handled the input event will be passed to the next viewport on the tree. Now one thing to keep in mind is outside of the control node input virtual method which again is called the G U I input virtual method.
We will not stop the propagation cause unless we manually stop at ourselves. So in order to stop the propagation of our input event chain we need to call a root viewport Singleton and actually call the set. Input is handled method. So that would look like the following. Make sure our node class is attached to the same tree so self to get tree get root that set input as handled method and what that will do is we will manually stop the event propagation chain.
Now we talked about input events and really the input event is just a class. However it has its own set of features that allow us to for example know when we've clicked a certain position in some cases other cases. If we press down on a specific key and we will go ahead and look at that in the next episode. Thank you so much for joining me. Thank you for subscribing and thank you for clicking the Like button. If you have any questions or comments please feel free to leave them in the comment section down below. I'm also uploading to get hub a project file that you can play around with when it comes to dealing with input handling so go ahead check it out see how the propagation calls are handled and I look forward to seeing you in the next episode.
Have an amazing day.