 I've got a fresh Godot 4 project with nothing in it. I thought that'd be a nice place to start by showing how to handle player input. Player input can happen in a variety of ways, whether it's happening with a mouse and keyboard or a gamepad controller. We're going to cover a couple of those things, specifically a keyboard input and a gamepad input to illustrate how to handle both of those. So we're going to start by adding a node 2D scene. This is just going to be our main scene. We'll go ahead and save that as main. In your new Godot projects, there's an icon to SVG that comes with it. You can just drag it right into the viewport in the 2D editor. Save that again and we'll just run our game. Select the current one as the main scene. So we have our little SVG icon showing right there. And now let's make it so that it rotates and then we'll use player input to pause and stop rotation. We'll go ahead and we'll add a script to main. We'll just keep it called main. And you can see it fills out some things already. I have type hinting on, so you won't see things like float and void if you have type hinting turned off. I made a separate video on that and why I use it. And I think it's helpful for screen casts in particular because you can see what is happening. So we have our image. Let me actually collapse this or icon just so we know the name of it. And to make it rotate, we'll just say icon dot rotate has a rotate method that you can call on it. And it takes in a float of radians. We'll just pass in Delta Delta is the time between each frame. So it's a pretty small value. We can go ahead and just rotate it by Delta every frame. And we don't even need ready. So we'll delete that. Save that. Run the game again. You'll see that our icon now spins. We're going to add some basic player input to pause and resume, aka toggle the rotating of the icon. So the first thing when handling input you need to do is you go to project, project settings, and how you get there might be different on Windows and Linux, but this interface will be the same regardless. And when you go to project settings, there you can configure the name, the descriptions, etc. You're going to click the input map tab. This allows you to add new actions and bind keys and inputs to them. If you toggle this checkbox show built-in actions, you'll see that there are already a bunch of them already. And you might even see examples that use UI accept, UI select, UI cancel and UI up and down. I use those sometimes quickly for games where it doesn't really matter. But for our case, we're going to add our own action. So here focus this input and for add new action, we're going to call it toggle rotation and click the add button. This adds a new action. Don't worry about dead zone. That's for things like analog sticks or triggers that have a variable input. And this just says when should it trigger the action based upon that dead zone. We'll just keep it at 0.5 because we're going to be doing key presses. So you click the plus button and it shows you this interface. You need to select an event and it says listening for input. So if I press the space bar, it detects that and knows, okay, the space bar should toggle rotation. You could also filter here type space and it comes up and shows you options. That's really helpful to find something like a joypad button that might not, you might not have connected, but you still want to add support for. But for our case and for most of the case, I like pressing the actual button on my keyboard and letting it listen for it to make sure I get it right. So go ahead. Let me press space again and you click. Okay. Now you'll see here. Look, those got some weird focus going on. You'll see here space is now assigned to it. You can edit that if you want, but we'll keep it. And then I'm going to click plus again. I'm going to turn on my ps4 controller. So here's my controller. I just have a ps4 controller connected. I'm going to make the X button toggle it and you'll see it assigns it. Godot has this generic buttons called button zero, which refers to the bottom action button, the X button or cross on Sony controllers, A on Xbox controllers and B on Nintendo controllers. You just click. Okay. If you change the device here, that'll only map it to the device at that index. So that might be useful for like player one, player two, that kind of thing. But for us, we'll leave it to all devices. So whether they're they have three controllers plugged in or just one or none, they can use that. Then you click close and that we have our binding. Now what we do in our process function, which that's getting called every single, you know, a bunch of times every second in our game loop, we're going to go ahead and we're going to say if input dot just is action just press. And then it actually shows you an auto complete all the options, including the built in ones and the one we added toggle rotation. So we'll just select toggle rotation there at our colon. And here what we're going to do is we're going to actually toggle a variable. So we'll say rotate icon equals false and we don't have the variable rotate icon false. So we'll say rotate icon is true because we'll rotate it by default. That creates a variable that we can then access in our functions and we'll set it to false. Well, actually we'll set it to the inverse of rotate icon so that if we click it, it'll toggle it and change it. And then here instead of just always doing icon dot rotate, we'll say if rotate icon then we'll rotate the icon. Let's go ahead and play our game again. Press the space bar. It stops rotating. I've got my controller. I just press the X button. And now we have input that is checking based on controller keyboard. You can easily map and add actions there. So let's go ahead and close this. I don't think there's any code I want to change here. But I do want to talk a little bit about the input singleton. So you have a bunch of actions here. In particular, is action pressed? Is action just released? So just pressed means it was pressed this frame but not being held down. So useful for here. You can say is action pressed? That means it's being pressed across multiple frames. There are a ton of different functions there for that singleton. I would really recommend reading through input. Input's a really high use API in Godot. And I highly recommend it. But for our case, let's go ahead and add some more input actions that will show some alternate ways to handle this. Because you've got a few different options here. So we'll go back to project settings. We'll add a new action. It's really good to get in the habit of this. You can start doing it pretty quickly. We'll add one that's move up, move down, move left, move right. And you can just add them all and press enter and keep adding ones pretty quickly, which is really helpful. Then just go in and bind your config. So I pressed up on the D-pad. And now I'm going to press up on the analog stick on my controller. Move down. I'm pressing the down key. I also really like to use W-A-S-D. Support W-A-S-D and the arrow keys on the controller because I have a weird ergonomic keyboard where the arrow keys I can't use in the normal gaming sense. So W-A-S-D support really helps. We'll do the controller again. Sorry, this is a little boring. But it's just good to see it. And then you have this for your whole game. Once you set these up and you can just access them as needed, you can get really granular with how you define actions. And the more specific you are, I think the better because it'll make your code a lot more clear. But you could probably get by with pretty simple games where you have the directional inputs and then you have a primary and a secondary input. And that'll get you pretty far. That's for smaller games is what I do. Now that we have our movement actions, let's go ahead and make it so we can move our icon. So we'll say if input, we'll say is action pressed? So that means is it pressed once or held down? We'll say move down. We'll take the icon. It's position.y. I'll say plus equals 10. Go ahead and save that. Run it and press your down key. I'm pressing S, pressing down. I'm pressing my controller gamepad. Okay, wow. We just pushed it off the screen. Now we can go ahead and we can say if input that is action pressed UI up. We'll just copy this. But instead of adding to the Y position, we'll subtract to it. Okay. Whoops. I meant to say move up. Run that again. And yeah, that's working. You can go up and down. Now if we had made this just pressed, run it again. You don't even have to quit it and run it again. It'll just reload. You can't hold it and get the input. You have to keep tapping it. So that's the big difference between is action just pressed and is action pressed is you can hold it when it's is action pressed versus just pressed. So if you want to press a button, have an action happen, use just pressed. If you want to be able to hold it down, you do that. Now go ahead and add these for move left and right. You would change the exposition. I'm not going to do that because that shows what it is. I'm not going to go ahead and add move up and move down. That's something that you can do on your own. But there's some things we could do to improve this. Like we could say speed and set it to 10, make it a variable or you make it a constant, whatever you want. So you got options there. I'm going to talk about the input callback, which is an important aspect to all of this. So in process, when you're checking for actions being pressed, being held down, that's a really good place to put them. But there's also this thing called the input callback. Or anytime an input event gets triggered, you can inspect that event. You can say event dot is action pressed and we'll say toggle rotation. And instead of changing it in the process function and checking it every time, we'll just check every single input event and say if it's our action that was pressed, toggle the rotation. And this is nice because you have this isolation from the process code where it's like, when action happens, I generally try to use the input callback when it's a just press type thing. When it's process and something that's being pressed down, I'll put it in there because that's getting checked all the time. So I don't know if there's any other reasons yet to use the input callback versus this. It's nice for monitoring and reacting to events that happen, is what I've found. Maybe there's more reasons to it, but for me, that's what I've found. Hopefully this helps show the different actions and how to handle player input. In summary, you go ahead and go to project settings, create your input map, you can assign different keys. Then in your code, you can check those input actions accordingly and change your game logic as needed. There's also the input callback, which you can use to check events and react accordingly as well. That's it for this one. I'll do some more advanced input videos in the future, and if you have any questions, let me know. Thanks. See you. Bye.