1
00:00:00,000 --> 00:00:08,106
[BLANK_AUDIO]

2
00:00:08,106 --> 00:00:14,780
Continuing on, learning about how we can
use player input in our XNA games.

3
00:00:14,780 --> 00:00:16,810
We finished up with mice last time and

4
00:00:16,810 --> 00:00:20,750
we're going to start on the 360
controllers this time.

5
00:00:20,750 --> 00:00:23,010
Here are the module learning objectives.

6
00:00:27,240 --> 00:00:33,160
So, we want to start using a controller
for our input in our XNA games.

7
00:00:33,160 --> 00:00:34,320
And normal people

8
00:00:37,620 --> 00:00:40,490
use a controller like this, perhaps even
not with a wire on it.

9
00:00:44,190 --> 00:00:48,790
Super geeky people like me use a
controller like this, with hyper

10
00:00:48,790 --> 00:00:54,870
responsive buttons, and in fact, tension
adjusters on the thumb sticks

11
00:00:54,870 --> 00:01:00,120
so that you can modify how much resistance
those thumb sticks provide to you.

12
00:01:00,120 --> 00:01:04,620
Yes, geeky, but you know, you gotta do
something.

13
00:01:04,620 --> 00:01:05,750
Right?

14
00:01:05,750 --> 00:01:09,430
Okay.
So, we're going to look at how we can

15
00:01:09,430 --> 00:01:13,490
use, we'll start with using a thumbstick,
the

16
00:01:13,490 --> 00:01:16,620
left thumbstick, to drive the character
around the screen.

17
00:01:16,620 --> 00:01:20,250
Remember last time when we were using a
mouse, we used

18
00:01:20,250 --> 00:01:24,298
the mouse to used the mouse location to
move the character around.

19
00:01:24,298 --> 00:01:26,500
Now we're going to use the thumbstick
instead.

20
00:01:26,500 --> 00:01:30,900
So let's go to the code that we had
before, and

21
00:01:30,900 --> 00:01:34,580
we will gradually convert this code to
using a controller instead

22
00:01:34,580 --> 00:01:35,100
of a mouse.

23
00:01:35,100 --> 00:01:38,750
So this time, we're going to use the
thumbstick to move the character.

24
00:01:38,750 --> 00:01:41,550
Next time, we'll use the A button to
change the

25
00:01:41,550 --> 00:01:45,050
character, just like we used the left
mouse button before.

26
00:01:45,050 --> 00:01:45,710
Okay.

27
00:01:45,710 --> 00:01:50,100
So we want to make the thumbstick move the
character.

28
00:01:50,100 --> 00:01:53,589
So as you can see here, I commented out
the code we had before.

29
00:01:55,950 --> 00:01:59,830
And I've moved the mouse GetState thing
down here, so this will be a really

30
00:01:59,830 --> 00:02:04,850
weird game where you move with the thumb
stick, but you change with the left mouse.

31
00:02:04,850 --> 00:02:06,530
That is a temporary thing.

32
00:02:06,530 --> 00:02:10,830
We would never make a game like that all
the way to the end.

33
00:02:10,830 --> 00:02:11,470
Okay.

34
00:02:11,470 --> 00:02:17,420
So the issue though, is instead of getting
a MouseState we need

35
00:02:20,460 --> 00:02:21,070
to

36
00:02:23,110 --> 00:02:24,530
get a GamePadState.
And I'll just call it gamepad.

37
00:02:28,120 --> 00:02:31,460
And we'll call the GamePad.GetState, we'll
call

38
00:02:31,460 --> 00:02:37,010
the GamePad.GetState method instead of the
MouseGetState method.

39
00:02:37,010 --> 00:02:40,370
As soon as I open the parenthesis though,
we discover that

40
00:02:40,370 --> 00:02:44,200
we have to provide one argument when we
call the GetState.

41
00:02:44,200 --> 00:02:47,700
And that argument needs to say, well,
which gamepad do you mean?

42
00:02:47,700 --> 00:02:53,080
By default, when you plug a 360 controller
into your laptop or PC of any kind,

43
00:02:53,080 --> 00:02:57,270
it is player index one, but of course if
you're building an xbox

44
00:02:57,270 --> 00:03:02,120
game or if you have multiple 360
controllers plugged in, it is, you know.

45
00:03:03,260 --> 00:03:04,710
There's a broader range.

46
00:03:04,710 --> 00:03:08,010
By the way, gamepad and controller mean
the same thing.

47
00:03:08,010 --> 00:03:12,780
So, within the XNA framework, it's always
gamepad, but most people

48
00:03:12,780 --> 00:03:18,470
call it a 360 controller or a controller.
So I will interchange those

49
00:03:18,470 --> 00:03:22,248
different pieces of terminology, but
they're the same thing.

50
00:03:22,248 --> 00:03:24,780
So we're going to do it for player index
one.

51
00:03:28,780 --> 00:03:34,000
So, one of the things that we have really
talked about, even though we've been using

52
00:03:34,000 --> 00:03:40,220
it a lot, is this syntax that I'm using

53
00:03:40,220 --> 00:03:45,110
to call GetState, and the syntax I used
down here to call GetState on the mouse.

54
00:03:45,110 --> 00:03:48,400
Because we've talked about calling method
by

55
00:03:48,400 --> 00:03:54,750
providing an object name, some lower case
variable,

56
00:03:54,750 --> 00:03:56,310
dot, and the method name.

57
00:03:57,480 --> 00:04:01,670
And so those are what are called instance
methods, because we're calling

58
00:04:01,670 --> 00:04:06,740
the method on a particular instance of the
class on a particular object.

59
00:04:06,740 --> 00:04:10,420
And by and large, that's what you're
going to use all the time.

60
00:04:10,420 --> 00:04:15,590
However, all the time is a little too
strong, because we have discovered

61
00:04:15,590 --> 00:04:19,875
ourselves using this other syntax, even
all the way back on our first

62
00:04:19,875 --> 00:04:23,410
C# program, where we used
console.writeline.

63
00:04:23,410 --> 00:04:26,570
Console, we weren't holding a console
object, we

64
00:04:26,570 --> 00:04:32,100
were providing a class name not an object
name.

65
00:04:32,100 --> 00:04:34,970
And so there are these special kinds of

66
00:04:34,970 --> 00:04:40,090
methods that we call, that are called
static methods.

67
00:04:40,090 --> 00:04:45,820
And we call static methods by putting the
class name followed by

68
00:04:45,820 --> 00:04:49,670
the method, followed by a dot, followed by
the method name rather than a

69
00:04:49,670 --> 00:04:54,700
particular object's name.
So, I just said a whole bunch of words,

70
00:04:54,700 --> 00:05:02,930
you should go do an in-lecture quiz to see
if you figured out what those words mean.

71
00:05:02,930 --> 00:05:06,290
I jumped ahead a little bit, so the
in-lecture quiz

72
00:05:06,290 --> 00:05:11,390
you're about to do is about static
methods, and that's awesome.

73
00:05:11,390 --> 00:05:13,820
So we'll talk about those, and then I'll
have you

74
00:05:13,820 --> 00:05:17,720
do another in-lecture quiz about actually
using a 360 controller.

75
00:05:19,200 --> 00:05:21,440
So, if we look at this slide, I've

76
00:05:21,440 --> 00:05:28,550
extracted from the documentation
information about the GamePad Members.

77
00:05:29,620 --> 00:05:32,610
So the GamePad members are these three
things.

78
00:05:32,610 --> 00:05:37,830
We can call GetCapabilities, we call
GetState and we can call SetVibration.

79
00:05:37,830 --> 00:05:43,190
How do we know if a particular method is
static or not.

80
00:05:43,190 --> 00:05:50,370
We know because on the documentation
there's a big S.

81
00:05:50,370 --> 00:05:54,640
That doesn't stand for super method, that
stands for static.

82
00:05:54,640 --> 00:05:58,280
And so, if we ever see S on a particular
method

83
00:05:58,280 --> 00:06:03,740
in the documentation, that means we call
that method by using

84
00:06:05,020 --> 00:06:08,480
the class name rather than an instance
name.

85
00:06:08,480 --> 00:06:10,160
Let's take a look at another example

86
00:06:10,160 --> 00:06:15,050
where the documentation actually shows us
both kinds.

87
00:06:15,050 --> 00:06:16,740
So let's look at rectangle.

88
00:06:19,380 --> 00:06:21,740
So we've got a whole bunch of rectangle
fields,

89
00:06:21,740 --> 00:06:23,740
so I'm going to navigate around for a
little while.

90
00:06:23,740 --> 00:06:27,560
Rectangle structure, I want rectangle
members, so here we go.

91
00:06:27,560 --> 00:06:29,890
You'll notice, if you look at the public

92
00:06:29,890 --> 00:06:32,830
methods here, and this applies to
properties too.

93
00:06:32,830 --> 00:06:38,750
If we look at the public method, remember
we used intersects with an S

94
00:06:38,750 --> 00:06:43,000
on a particular rectangle object to see if
it intersects with another one.

95
00:06:44,060 --> 00:06:44,550
But we could

96
00:06:44,550 --> 00:06:50,190
use intersect without the S, notice its a
static method cause it has that S.

97
00:06:50,190 --> 00:06:54,626
So we call this one Rectangle.Intersect
and we'd hand it

98
00:06:54,626 --> 00:06:58,460
to two rectangles, and it returns another
rectangle back to us.

99
00:06:58,460 --> 00:07:02,430
So, these static methods that we call by
putting the class name

100
00:07:02,430 --> 00:07:08,390
first, are particularly useful, it's just
a slightly different syntax for calling.

101
00:07:08,390 --> 00:07:09,610
Okay, so backing up

102
00:07:09,610 --> 00:07:12,460
to the in lecture quiz that I went
blasting by, tell me

103
00:07:12,460 --> 00:07:15,869
how you feel about using a 360 controller
as an input device.

104
00:07:21,570 --> 00:07:23,290
Okay.
So let's now

105
00:07:25,560 --> 00:07:26,500
fire up the debugger.

106
00:07:27,500 --> 00:07:30,040
Notice that I do have a 360 controller
plugged in

107
00:07:30,040 --> 00:07:34,220
right now, but let's fire up the debugger,
and I've F5'd.

108
00:07:35,330 --> 00:07:40,360
So I am now going to extract or get the
GamePadState.

109
00:07:40,360 --> 00:07:45,480
And if I press F10, I step over this as it
executes this line of code and

110
00:07:45,480 --> 00:07:50,790
moves onto the next one.
So now I can hover over gamepad and

111
00:07:50,790 --> 00:07:53,860
see that it's connected, and there's a
whole bunch of other

112
00:07:53,860 --> 00:07:59,970
stuff available to me, but this is
connected thing really matters.

113
00:07:59,970 --> 00:08:02,750
And you should go do another in-lecture
quiz to

114
00:08:02,750 --> 00:08:06,685
tell me about how that is connected thing
really matters.

115
00:08:06,685 --> 00:08:16,340
Okay, now that you're back, we're going to
actually

116
00:08:16,340 --> 00:08:19,142
start moving the character around based on
the thumbstick.

117
00:08:19,142 --> 00:08:24,810
Well, unfortunately the thumbstick

118
00:08:24,810 --> 00:08:29,750
we don't get a location like we got for
the mouse.

119
00:08:29,750 --> 00:08:32,520
There is no x and y for the thumbstick.

120
00:08:32,520 --> 00:08:35,440
It's not a pointing device like the mouse
is.

121
00:08:35,440 --> 00:08:40,270
It's just a device that we deflect to get
action in our game.

122
00:08:40,270 --> 00:08:41,170
So we can't use,

123
00:08:41,170 --> 00:08:45,520
there's no such thing as gamepad.x and
gamepad.y or leftthumbstick.x

124
00:08:45,520 --> 00:08:49,708
or anything like that.
So we can't use that to move our

125
00:08:49,708 --> 00:08:54,190
character around.
Instead, what

126
00:08:54,190 --> 00:08:58,990
happens is the thumbstick.
Well, we can look at the documentation and

127
00:08:58,990 --> 00:09:06,240
see what happens.
So, let's look at GamePadState

128
00:09:06,240 --> 00:09:09,734
like so, because that's what comes back
from our GetState method.

129
00:09:10,750 --> 00:09:14,670
We can see, we can access a structure of
thumbsticks.

130
00:09:15,774 --> 00:09:18,250
And those thumbsticks are gamepad
thumbsticks, so

131
00:09:18,250 --> 00:09:20,230
let's take a look at what those are.

132
00:09:24,608 --> 00:09:29,900
So, gamepad thumbsticks, that structure
has a left and a right property,

133
00:09:29,900 --> 00:09:33,330
so we can get at the left thumbstick or
the right thumbstick.

134
00:09:33,330 --> 00:09:38,220
And finally, if we click on the left
property, we get a vector two.

135
00:09:38,220 --> 00:09:40,300
And the vector two says, it's the position
of

136
00:09:40,300 --> 00:09:43,880
the left thumbstick, and we get floating
point values.

137
00:09:43,880 --> 00:09:46,970
So, left and right, negative 1 is all the
way to

138
00:09:46,970 --> 00:09:49,489
the left, positive 1 is all the way to the
right.

139
00:09:50,800 --> 00:09:51,190
Up and down.

140
00:09:51,190 --> 00:09:53,762
So that's the x component of the Vector
two.

141
00:09:53,762 --> 00:09:57,470
Vector two's have an x property and a y
property.

142
00:09:57,470 --> 00:10:04,010
And up and down, negative 1 is all the way
down, positive 1 is all the way up.

143
00:10:04,010 --> 00:10:07,970
So we're going to access some range, some

144
00:10:07,970 --> 00:10:11,110
range of deflection from negative 1 to 1,
for

145
00:10:11,110 --> 00:10:12,950
both x and y on the thumbstick, and we're

146
00:10:12,950 --> 00:10:16,240
going to have to use that somehow to
actually change

147
00:10:16,240 --> 00:10:18,420
the location of the character.

148
00:10:18,420 --> 00:10:22,370
So, think of it as not following the
thumbstick around so

149
00:10:22,370 --> 00:10:28,500
much as using the thumb stick to apply
thrust to the character.

150
00:10:28,500 --> 00:10:30,460
So, we'll use the thumb stick to push

151
00:10:30,460 --> 00:10:32,920
the character around, because we're being
that way.

152
00:10:32,920 --> 00:10:36,490
So we're going to push the character
around instead of

153
00:10:36,490 --> 00:10:38,360
trying to make it follow an x and y.

154
00:10:38,360 --> 00:10:41,240
So let's go back to the code to see

155
00:10:41,240 --> 00:10:42,044
how we can do that.

156
00:10:42,044 --> 00:10:47,400
I'm going to go up to the top of game one,
and you'll see we have a thumbstick

157
00:10:47,400 --> 00:10:52,270
deflection to my entirety declared this
constant, and I've set it to 100.

158
00:10:52,270 --> 00:10:57,400
So if the thumbstick is banged all the way
to the right, for example, on an

159
00:10:57,400 --> 00:11:02,780
particular update, we'll end up moving the
character 100 pixels to the right.

160
00:11:02,780 --> 00:11:04,860
We might find that I can't quite control
that

161
00:11:04,860 --> 00:11:06,660
as much as we'd like but we'll start
there.

162
00:11:07,710 --> 00:11:11,300
Okay.
So what we can do is down here in our

163
00:11:11,300 --> 00:11:18,175
update, we can change the rock, to
drawRectangle.

164
00:11:18,175 --> 00:11:20,220
The drawRectangle's x location, and

165
00:11:23,860 --> 00:11:29,650
we'll add to it remember plus equal.
We'll add

166
00:11:29,650 --> 00:11:35,051
to it the gamepad, left thumbstick, the

167
00:11:35,051 --> 00:11:41,690
gamepad.ThumbSticks.Left.x.
So here's what we have, right?

168
00:11:41,690 --> 00:11:44,430
We have the gamepad, that's the
GamePadState.

169
00:11:44,430 --> 00:11:49,100
The thumbsticks is the sum, thumbsticks
structure which lets us access

170
00:11:49,100 --> 00:11:50,280
the left and thumbsticks.

171
00:11:51,700 --> 00:11:57,520
The left property gives us a vector to
which has x and y property.

172
00:11:58,550 --> 00:12:04,445
And so finally, this whole big thing here
is the deflection on the left

173
00:12:04,445 --> 00:12:10,520
thumbstick in the x direction, which
ranges from negative 1 to 1.

174
00:12:10,520 --> 00:12:14,600
So we'll take that and we'll multiply it
by the thumbstick deflection

175
00:12:14,600 --> 00:12:19,650
amount, that constant I just showed you,
and we'll get a compilation error.

176
00:12:19,650 --> 00:12:23,270
And when we F6, it says we can't compla,
convert float to int.

177
00:12:23,270 --> 00:12:28,070
So this whole thing is a float, and
drawRectangle.x

178
00:12:28,070 --> 00:12:30,390
is an int, so we have to do a typecast,

179
00:12:33,080 --> 00:12:36,640
and we'll do the typecast.
And now I'm going to make another

180
00:12:36,640 --> 00:12:41,230
deliberate mistake.
We'll say great.

181
00:12:41,230 --> 00:12:47,454
We'll do the same down thing for

182
00:12:47,454 --> 00:12:54,680
drawRectangle.Y,

183
00:12:54,680 --> 00:12:58,740
and I run it and I grab my 360 controller.
And

184
00:12:58,740 --> 00:13:04,560
I can drive with the thumbstick very fast
as you can see, but you can't tell.

185
00:13:04,560 --> 00:13:08,300
But as I push the thumbstick up, the
character goes down.

186
00:13:09,400 --> 00:13:12,070
Now, some people, right, some gamers love
that, right.

187
00:13:12,070 --> 00:13:14,080
That's exactly how you like to aim me use
it,

188
00:13:14,080 --> 00:13:16,920
sort of opposite of the way it works and
so on.

189
00:13:16,920 --> 00:13:20,680
But let's actually take care of many
people,

190
00:13:20,680 --> 00:13:24,410
and we'll have to fix this by changing

191
00:13:24,410 --> 00:13:29,720
this to minus equal.
So, remember that the real issue here

192
00:13:29,720 --> 00:13:35,700
is that the thumbstick negative go, is low
and positive is high.

193
00:13:35,700 --> 00:13:40,510
But in our window, y is numbered starting
at zero, the pixels, or

194
00:13:40,510 --> 00:13:43,550
numbered starting at zero at the top and
they increase as we go down.

195
00:13:43,550 --> 00:13:47,420
So we have to reverse it, in this basic
example.

196
00:13:47,420 --> 00:13:49,510
Building a real game, you can actually
find out

197
00:13:49,510 --> 00:13:51,770
from the player profile which they prefer

198
00:13:51,770 --> 00:13:53,640
and set it appropriately, and all those
things.

199
00:13:53,640 --> 00:13:57,370
So, so we can make it much more
customizable for the player.

200
00:13:57,370 --> 00:14:00,040
We just not going to bother to do that
here.

201
00:14:00,040 --> 00:14:04,350
The other thing we will do, is I am
going to change this deflection amount to

202
00:14:04,350 --> 00:14:12,300
be like 20 not 120, 20 instead.
So now, I can move much more reasonably.

203
00:14:12,300 --> 00:14:15,160
And because we actually get a floating
point number

204
00:14:15,160 --> 00:14:20,122
in the left value, I get better control.
I can move slowly, or I can move

205
00:14:20,122 --> 00:14:25,670
quickly, and it just depends on how much
I've deflected it.

206
00:14:25,670 --> 00:14:26,665
So that's all good.

207
00:14:26,665 --> 00:14:27,040
Alright.

208
00:14:27,040 --> 00:14:34,560
That is, and in fact, that is it for the
code that we're writing for today.

209
00:14:36,740 --> 00:14:41,200
So, I should point out that I

210
00:14:41,200 --> 00:14:45,280
just lied to you moments ago, that's not
all it for the

211
00:14:45,280 --> 00:14:50,080
code we're writing for today.
Because, we've skipped one critical thing

212
00:14:50,080 --> 00:14:55,963
down here in using the thumbstick.
Here's the thing, it

213
00:14:55,963 --> 00:15:01,741
is certainly possible that we try to get
GamePadState, but this

214
00:15:01,741 --> 00:15:07,709
particular gamepad is not connected.
So, go do an in-lecture quiz about what

215
00:15:07,709 --> 00:15:12,280
you think happens when that's occasional
come back and fix that last thing.

216
00:15:17,230 --> 00:15:18,040
Okay.

217
00:15:18,040 --> 00:15:23,150
So what we should really do in the code,
is we should only respond to

218
00:15:23,150 --> 00:15:28,188
game pad input, if infact the gamepad is
connected.

219
00:15:28,188 --> 00:15:31,560
And I'm going to get rid of this commented

220
00:15:31,560 --> 00:15:33,870
mouse state code because that was just
temporary stuff.

221
00:15:36,280 --> 00:15:38,640
That was really interesting, sorry about
that.

222
00:15:39,760 --> 00:15:44,840
So it's important if you going to to use
game pad input that you make

223
00:15:44,840 --> 00:15:49,887
sure that in fact the gamepad is connected
before you use any of that input.

224
00:15:49,887 --> 00:15:50,307
Okay.

225
00:15:50,307 --> 00:15:52,855
Because it's invalid, we don't know what
it is.

226
00:15:52,855 --> 00:15:54,480
It's just whatever happens to get return.

227
00:15:55,530 --> 00:16:00,540
So now, I'm no longer lying to you, that
is all the code we're writing for today.

228
00:16:00,540 --> 00:16:01,630
So as a recap,

229
00:16:01,630 --> 00:16:06,320
we learned how to get the GamePadState, we
learned about static method, and

230
00:16:06,320 --> 00:16:10,860
we learned about how to use a thumbstick
to move the character around.

231
00:16:10,860 --> 00:16:15,150
Next lecture, we'll learn how to use a
GamePad button to actually

232
00:16:15,150 --> 00:16:18,080
change the characters just like we used
the left mouse button before.