Let's go ahead and take a look at the homework. So for the homework assignment I decided to create four different variables each one representing a limit on either the top bottom left or right side of the screen. I also have a variable to hold the delta value that can be passed from the process or rather the physics process virtual method and allow b delta value to be used inside the UN handled input virtual method. If you decided to put your players input inside the unhindered input method that is a great place to start.
In this case we have a simple unhindered input chain. First we make sure if we're getting an input event came then we're making sure that if we have an event pressed and if our event scan code is corresponding to the correct key that we would like to use. We then go ahead and call a function that handles the moving of our game object and we pass city Delta copy value. In this case we're just passing a delta value and in physics process we're just taking the delta value that physics process gives us and we're passing it to a class variable.
In this case Delta copy and this if statement chain is used for each movement. So right left up and down and we have our functions that move or value we take in a delta value we return a void and two if checks the first FS check is to make sure that as long as our position is within bounds we're allowed to move. And if we go beyond the bounds we just keep our value at the maximum value possible. And we do that for the right left up and down functions very similar. I'm gonna go ahead and upload the first example to get hub and if you decided to do something like this you'll find that
Your game object doesn't behave the way you're expecting it to. So let's go ahead and see what happens. If we decide to use unhindered input to handle physics based movement as you can see here we have our game objects center to the middle of the screen. And when we move right or left you'll notice that we're not really moving smoothly. And the reason for this is input virtual methods. Basically the three different types of input virtual methods that are provided to us are not really built to handle physics based movement on its own.
For example we're not past the delta value we have to in this case pass a delta value to a global class value and then pass that down to unhindered input and unhindered input is now able to use a delta value. But when you press and hold a button in this case I'm moving right. Notice how it sort of has this pause on pause kind of feeling to it. And if you decided to code in such a way where you got this behavior that's okay. So what exactly can we do to fix this issue. And it's quite simple.
All we have to do is create some sort of state. We haven't exactly gotten into state just yet over using state allows us to bypass the problem of our game object moving. Bit by bit basically as though it's pausing on pausing its movement. So let's go ahead and take a look at how we bypass moving bit by bit and it's quite simple. First we have a physics process. And in the physics process we pass down. Our. Movement. Methods. And
Our movement methods. Changed slightly. In that we have an extra. If statement. That checks our current state. So in this example if we want to move right we need to make sure that our current state. Is equal to right and then we can go ahead and move. Our game object. And then in our unhindered input. Instead of directly calling the movement function inside our hand or input we instead change our game objects state.
So in this case every time we press the right button we change our state to right. If we press left we change state to left when we press up we change state to up and when we press down we change to down and if our event. Is released. We change our state to. Idle. However this. Solution also comes with its own problem. As a matter of fact it comes with two problems now the first problem. Is that we cannot move in two directions. At the same time and that's because of our state. We are only allowed to be in exactly
One state. So for example if you want to move diagonally. Up in right at the same time basically moving at a 45 degree angle you are unable to because our state can either be moving up or moving right. It cannot be moving right and technically there is a way you can do that but it's quite cumbersome compared to the third option I'm going to show you. However the most important problem I want to point out is that because of all unhindered input works there's always gonna be a pause in movement and that's not clearly seen.
Or rather it's not something I can clearly show you. You just have to test out the code yourself and basically it happens when you press keys really fast. Now you may be thinking it's probably perhaps because of the else if statement chain and to be fair you are right it is in fact the else if statement chain.
However without this else if we're not able to change our state to idle basically without this. Else if statement chain we're just gonna keep moving to the right or to the left. Up
Up and down without stopping even if we let go of a key. So something to keep in mind. The reason we have this also statement chain is to make sure that when we release a key we step our game object. However if we have this line of code we will always call this code every time we release a key even if we press another key really fast. And it's just something you're gonna have to play but let's go ahead and take a look. At an example. Now again we have our game object. And I'm gonna move really fast and you're gonna see something.
Notice how the game object continues to move
Right and Left. But notice how there's this. Pause. Every time. I move left and right. Sort of Jags left and then it breaks right. And the reason for that is that also statement chain and that's just something we cannot really avoid
However without this else if we're not able to change our state to idle basically without this. Else if statement chain we're just gonna keep moving to the right or to the left. Up
Up and down without stopping even if we let go of a key. So something to keep in mind. The reason we have this also statement chain is to make sure that when we release a key we step our game object. However if we have this line of code we will always call this code every time we release a key even if we press another key really fast. And it's just something you're gonna have to play but let's go ahead and take a look. At an example. Now again we have our game object. And I'm gonna move really fast and you're gonna see something.
Notice how the game object continues to move
Right and Left. But notice how there's this. Pause. Every time. I move left and right. Sort of Jags left and then it breaks right. And the reason for that is that also statement chain and that's just something we cannot really avoid
00:06:07:22 - 00:06:11:29
and it’s apparent when you see the output. Notice how
00:06:13:25 - 00:06:22:21
no matter what. Even if we change we’re always going to cause the pause because at some point we have to release a key. However there is an easier solution.
00:06:23:03 - 00:06:51:00
So again we have four variables that handles our limit. Now we feel moved to the physics process you can see that we’re using the global Singleton input and the global Singleton input is basically our way of having our input virtual method inside the physics process it comes with a method called is key pressed we can pass in a scan code. In this case up or excuse me right left up and down and inside each if statement.
00:06:51:02 - 00:06:59:10
We basically call the movement method. We would like to associate to a specific key press and our function
00:06:59:14 - 00:07:32:13
Move right left up and down have basically reverted themselves back to the first example removing the if statement check for the current state of our game object and there are two benefits to this. One is that we can have diagonal movement and two is that we don’t get that Jaggi in this that we got from the second example. Let’s go ahead and take a look at that. So as you can see here we have our game object and this time we have diagonal movement and nothing Jaggi and. Handling player input and say the physics presses virtual method is
00:07:33:07 - 00:07:33:22
The
00:07:33:27 - 00:07:43:21
Scenario You want to see yourself using. So as you can see through the game window we were able to move diagonally. And there is no genius no weird work around to fix that by the way.
00:07:43:23 - 00:07:53:04
And there really isn’t. You’re always gonna get that genius if you try to use states. Now. You can also do the same thing without using the Singleton input class.
00:07:53:06 - 00:08:02:12
You could for example do everything through the Gordo settings however we haven’t gone through that just yet. Saving that for a different episode.
00:08:02:14 - 00:08:08:06
However there is more than one way to handle input inside the physics process for your method.
00:08:08:08 - 00:08:10:24
Lastly there is an issue with the physics process.
00:08:10:26 - 00:08:19:11
If you decide to handle player input inside physics process and that is that we do not have those fancy input chain stoppers. Remember
00:08:19:13 - 00:08:49:16
Remember that the input virtual methods allow for propagation and we can also stop propagation where we don’t have that in physics process. Let me give you an example. Let’s say we have the spacebar Inside the Physics process virtual method input that is key press space or the name that catches the space bar. Well if we have another code inside of any of the input virtual methods. If we catch. For example if we were to catch an input press inside a physics process we are not going to stop.
00:08:49:18 - 00:09:05:01
We are still going to go through the input virtual methods. And so our game will basically call two different behaviors the behavior or whatever your spacebar is inside the physics process virtual method and the space bar behavior that is inside your
00:09:05:04 - 00:09:06:22
Input virtual method.
00:09:06:24 - 00:09:37:09
Now there is a solution. Basically we can pause our game object when dealing with anything. For example a pause menu. And so it’s not really a problem but just something to keep in mind. Because we cannot stop propagation in the physics process virtual method. We basically need to pause our game object when we don’t want a game object to act upon certain player inputs. Well that’s all I have for you in this episode. I’m gonna upload all three different sample files to get up so please feel free to download and play around with it.
00:09:37:11 - 00:09:49:03
Thank you so much for subscribing and thank you so much for clicking the Like button. If you have any questions or comments please feel free to leave them in the comments section down below. I look forward to seeing you in the next episode.