[BLANK_AUDIO] Continuing on, learning about how we can use player input in our XNA games. We finished up with mice last time and we're going to start on the 360 controllers this time. Here are the module learning objectives. So, we want to start using a controller for our input in our XNA games. And normal people use a controller like this, perhaps even not with a wire on it. Super geeky people like me use a controller like this, with hyper responsive buttons, and in fact, tension adjusters on the thumb sticks so that you can modify how much resistance those thumb sticks provide to you. Yes, geeky, but you know, you gotta do something. Right? Okay. So, we're going to look at how we can use, we'll start with using a thumbstick, the left thumbstick, to drive the character around the screen. Remember last time when we were using a mouse, we used the mouse to used the mouse location to move the character around. Now we're going to use the thumbstick instead. So let's go to the code that we had before, and we will gradually convert this code to using a controller instead of a mouse. So this time, we're going to use the thumbstick to move the character. Next time, we'll use the A button to change the character, just like we used the left mouse button before. Okay. So we want to make the thumbstick move the character. So as you can see here, I commented out the code we had before. And I've moved the mouse GetState thing down here, so this will be a really weird game where you move with the thumb stick, but you change with the left mouse. That is a temporary thing. We would never make a game like that all the way to the end. Okay. So the issue though, is instead of getting a MouseState we need to get a GamePadState. And I'll just call it gamepad. And we'll call the GamePad.GetState, we'll call the GamePad.GetState method instead of the MouseGetState method. As soon as I open the parenthesis though, we discover that we have to provide one argument when we call the GetState. And that argument needs to say, well, which gamepad do you mean? By default, when you plug a 360 controller into your laptop or PC of any kind, it is player index one, but of course if you're building an xbox game or if you have multiple 360 controllers plugged in, it is, you know. There's a broader range. By the way, gamepad and controller mean the same thing. So, within the XNA framework, it's always gamepad, but most people call it a 360 controller or a controller. So I will interchange those different pieces of terminology, but they're the same thing. So we're going to do it for player index one. So, one of the things that we have really talked about, even though we've been using it a lot, is this syntax that I'm using to call GetState, and the syntax I used down here to call GetState on the mouse. Because we've talked about calling method by providing an object name, some lower case variable, dot, and the method name. And so those are what are called instance methods, because we're calling the method on a particular instance of the class on a particular object. And by and large, that's what you're going to use all the time. However, all the time is a little too strong, because we have discovered ourselves using this other syntax, even all the way back on our first C# program, where we used console.writeline. Console, we weren't holding a console object, we were providing a class name not an object name. And so there are these special kinds of methods that we call, that are called static methods. And we call static methods by putting the class name followed by the method, followed by a dot, followed by the method name rather than a particular object's name. So, I just said a whole bunch of words, you should go do an in-lecture quiz to see if you figured out what those words mean. I jumped ahead a little bit, so the in-lecture quiz you're about to do is about static methods, and that's awesome. So we'll talk about those, and then I'll have you do another in-lecture quiz about actually using a 360 controller. So, if we look at this slide, I've extracted from the documentation information about the GamePad Members. So the GamePad members are these three things. We can call GetCapabilities, we call GetState and we can call SetVibration. How do we know if a particular method is static or not. We know because on the documentation there's a big S. That doesn't stand for super method, that stands for static. And so, if we ever see S on a particular method in the documentation, that means we call that method by using the class name rather than an instance name. Let's take a look at another example where the documentation actually shows us both kinds. So let's look at rectangle. So we've got a whole bunch of rectangle fields, so I'm going to navigate around for a little while. Rectangle structure, I want rectangle members, so here we go. You'll notice, if you look at the public methods here, and this applies to properties too. If we look at the public method, remember we used intersects with an S on a particular rectangle object to see if it intersects with another one. But we could use intersect without the S, notice its a static method cause it has that S. So we call this one Rectangle.Intersect and we'd hand it to two rectangles, and it returns another rectangle back to us. So, these static methods that we call by putting the class name first, are particularly useful, it's just a slightly different syntax for calling. Okay, so backing up to the in lecture quiz that I went blasting by, tell me how you feel about using a 360 controller as an input device. Okay. So let's now fire up the debugger. Notice that I do have a 360 controller plugged in right now, but let's fire up the debugger, and I've F5'd. So I am now going to extract or get the GamePadState. And if I press F10, I step over this as it executes this line of code and moves onto the next one. So now I can hover over gamepad and see that it's connected, and there's a whole bunch of other stuff available to me, but this is connected thing really matters. And you should go do another in-lecture quiz to tell me about how that is connected thing really matters. Okay, now that you're back, we're going to actually start moving the character around based on the thumbstick. Well, unfortunately the thumbstick we don't get a location like we got for the mouse. There is no x and y for the thumbstick. It's not a pointing device like the mouse is. It's just a device that we deflect to get action in our game. So we can't use, there's no such thing as gamepad.x and gamepad.y or leftthumbstick.x or anything like that. So we can't use that to move our character around. Instead, what happens is the thumbstick. Well, we can look at the documentation and see what happens. So, let's look at GamePadState like so, because that's what comes back from our GetState method. We can see, we can access a structure of thumbsticks. And those thumbsticks are gamepad thumbsticks, so let's take a look at what those are. So, gamepad thumbsticks, that structure has a left and a right property, so we can get at the left thumbstick or the right thumbstick. And finally, if we click on the left property, we get a vector two. And the vector two says, it's the position of the left thumbstick, and we get floating point values. So, left and right, negative 1 is all the way to the left, positive 1 is all the way to the right. Up and down. So that's the x component of the Vector two. Vector two's have an x property and a y property. And up and down, negative 1 is all the way down, positive 1 is all the way up. So we're going to access some range, some range of deflection from negative 1 to 1, for both x and y on the thumbstick, and we're going to have to use that somehow to actually change the location of the character. So, think of it as not following the thumbstick around so much as using the thumb stick to apply thrust to the character. So, we'll use the thumb stick to push the character around, because we're being that way. So we're going to push the character around instead of trying to make it follow an x and y. So let's go back to the code to see how we can do that. I'm going to go up to the top of game one, and you'll see we have a thumbstick deflection to my entirety declared this constant, and I've set it to 100. So if the thumbstick is banged all the way to the right, for example, on an particular update, we'll end up moving the character 100 pixels to the right. We might find that I can't quite control that as much as we'd like but we'll start there. Okay. So what we can do is down here in our update, we can change the rock, to drawRectangle. The drawRectangle's x location, and we'll add to it remember plus equal. We'll add to it the gamepad, left thumbstick, the gamepad.ThumbSticks.Left.x. So here's what we have, right? We have the gamepad, that's the GamePadState. The thumbsticks is the sum, thumbsticks structure which lets us access the left and thumbsticks. The left property gives us a vector to which has x and y property. And so finally, this whole big thing here is the deflection on the left thumbstick in the x direction, which ranges from negative 1 to 1. So we'll take that and we'll multiply it by the thumbstick deflection amount, that constant I just showed you, and we'll get a compilation error. And when we F6, it says we can't compla, convert float to int. So this whole thing is a float, and drawRectangle.x is an int, so we have to do a typecast, and we'll do the typecast. And now I'm going to make another deliberate mistake. We'll say great. We'll do the same down thing for drawRectangle.Y, and I run it and I grab my 360 controller. And I can drive with the thumbstick very fast as you can see, but you can't tell. But as I push the thumbstick up, the character goes down. Now, some people, right, some gamers love that, right. That's exactly how you like to aim me use it, sort of opposite of the way it works and so on. But let's actually take care of many people, and we'll have to fix this by changing this to minus equal. So, remember that the real issue here is that the thumbstick negative go, is low and positive is high. But in our window, y is numbered starting at zero, the pixels, or numbered starting at zero at the top and they increase as we go down. So we have to reverse it, in this basic example. Building a real game, you can actually find out from the player profile which they prefer and set it appropriately, and all those things. So, so we can make it much more customizable for the player. We just not going to bother to do that here. The other thing we will do, is I am going to change this deflection amount to be like 20 not 120, 20 instead. So now, I can move much more reasonably. And because we actually get a floating point number in the left value, I get better control. I can move slowly, or I can move quickly, and it just depends on how much I've deflected it. So that's all good. Alright. That is, and in fact, that is it for the code that we're writing for today. So, I should point out that I just lied to you moments ago, that's not all it for the code we're writing for today. Because, we've skipped one critical thing down here in using the thumbstick. Here's the thing, it is certainly possible that we try to get GamePadState, but this particular gamepad is not connected. So, go do an in-lecture quiz about what you think happens when that's occasional come back and fix that last thing. Okay. So what we should really do in the code, is we should only respond to game pad input, if infact the gamepad is connected. And I'm going to get rid of this commented mouse state code because that was just temporary stuff. That was really interesting, sorry about that. So it's important if you going to to use game pad input that you make sure that in fact the gamepad is connected before you use any of that input. Okay. Because it's invalid, we don't know what it is. It's just whatever happens to get return. So now, I'm no longer lying to you, that is all the code we're writing for today. So as a recap, we learned how to get the GamePadState, we learned about static method, and we learned about how to use a thumbstick to move the character around. Next lecture, we'll learn how to use a GamePad button to actually change the characters just like we used the left mouse button before.