1
00:00:08,810 --> 00:00:13,470
The good news is, that this module only
has two lectures.

2
00:00:13,470 --> 00:00:17,890
And the even better news is, that we're
going to use some really powerful

3
00:00:17,890 --> 00:00:19,390
ideas that we're going to find to

4
00:00:19,390 --> 00:00:22,320
be particularly useful in our game
development.

5
00:00:22,320 --> 00:00:25,330
To start, here are the Module 9 learning
objectives.

6
00:00:30,790 --> 00:00:35,050
So one of the things that we need to deal
with as we develop games, is,

7
00:00:35,050 --> 00:00:39,900
we need to handle having a whole bunch of
different game objects in the game world.

8
00:00:39,900 --> 00:00:43,550
And we've talked about, from way back in
classes

9
00:00:43,550 --> 00:00:46,570
and objects, we talked about the optic
oriented paradigm.

10
00:00:46,570 --> 00:00:51,280
And how it is a set of interacting objects
in our software.

11
00:00:51,280 --> 00:00:52,920
Well our game world is a set of

12
00:00:52,920 --> 00:00:56,160
interacting entities and we need to do
things

13
00:00:56,160 --> 00:01:00,490
with that set of interacting entities.
We have to update all of them.

14
00:01:00,490 --> 00:01:02,510
We have to draw all of them.

15
00:01:02,510 --> 00:01:04,170
There are lots of things we need to do
with

16
00:01:04,170 --> 00:01:08,380
all of the at least active entities in our
game world.

17
00:01:08,380 --> 00:01:12,270
And having a separate variable that we use
to store each of

18
00:01:12,270 --> 00:01:17,630
them is really problematic, that doesn't
really work for us at all.

19
00:01:17,630 --> 00:01:22,170
We need a better way to store a whole
bunch of game objects, and

20
00:01:22,170 --> 00:01:24,770
use those game objects within our world.

21
00:01:24,770 --> 00:01:27,810
That's what we're going to learn about in
this module.

22
00:01:27,810 --> 00:01:32,540
So to start, you should take an in lecture
quiz about arrays.

23
00:01:36,910 --> 00:01:40,090
So it turns out that arrays are a way

24
00:01:40,090 --> 00:01:42,880
that we can store a whole bunch of
different objects.

25
00:01:42,880 --> 00:01:49,860
And so, it also turns out that arrays in
C# are objects as well.

26
00:01:49,860 --> 00:01:55,670
And we'll see the syntax we use to deal
with those array objects soon, but the

27
00:01:55,670 --> 00:02:01,660
question is, what can we store in arrays?
And,

28
00:02:01,660 --> 00:02:03,980
the answer is, anything.

29
00:02:03,980 --> 00:02:08,300
We can store value types, we can store
reference types, we can store

30
00:02:08,300 --> 00:02:14,010
other arrays in arrays, we can pretty much
store anything we can think of.

31
00:02:14,010 --> 00:02:19,440
At least as a data type in the arrays that
we have available to us in C#.

32
00:02:19,440 --> 00:02:23,280
That comes with a caution.
Darn it, but there's always a but.

33
00:02:25,120 --> 00:02:26,830
The but is, that in a

34
00:02:26,830 --> 00:02:32,480
particular array, we can only store a
single data type.

35
00:02:32,480 --> 00:02:36,680
So each array that we declare and use can
only store one thing.

36
00:02:36,680 --> 00:02:39,190
So we can have an array on ints.
And we could have an array of TeddyBears.

37
00:02:39,190 --> 00:02:44,740
And we could have an array of Texture2D's.
And we could have an array of float.

38
00:02:44,740 --> 00:02:49,650
We can have each array, hold a different
data type.

39
00:02:49,650 --> 00:02:52,120
But once we have an array, it can only

40
00:02:52,120 --> 00:02:56,870
store a single data type.
So what do arrays look like in memory?

41
00:02:56,870 --> 00:02:58,540
Let's take a look.

42
00:02:58,540 --> 00:03:01,550
Conceptually, let's take a look.

43
00:03:01,550 --> 00:03:04,420
Before we do, do an in lecture quiz about

44
00:03:04,420 --> 00:03:06,900
arrays and what they can store, and then
we'll look.

45
00:03:10,345 --> 00:03:14,200
Okay, so sort of conceptually, anyway,
this is

46
00:03:14,200 --> 00:03:17,420
kind of like what arrays look like in
memory.

47
00:03:17,420 --> 00:03:21,000
So think of all these floating point
numbers

48
00:03:21,000 --> 00:03:25,030
as GPAs, or grade point averages that
we're storing.

49
00:03:25,030 --> 00:03:27,810
So we have a variable called GPAs.

50
00:03:29,060 --> 00:03:34,560
And then, if we're storing ten of them, as
in this example, then they're numbered.

51
00:03:34,560 --> 00:03:35,730
They're in memory

52
00:03:35,730 --> 00:03:40,970
boxes, as we're used to, but they're
numbered from 0 to 9.

53
00:03:40,970 --> 00:03:46,280
And so, let's hit some terminology about
how

54
00:03:46,280 --> 00:03:49,790
we talk about arrays and the things in
arrays.

55
00:03:49,790 --> 00:03:56,500
So, do an in-lecture quiz about what each
value that's stored in array is called.

56
00:03:57,770 --> 00:04:01,020
And because these in-lecture quizzes are
often about joking

57
00:04:01,020 --> 00:04:06,520
around and being, I was going to say
funny, being funny like I think funny is.

58
00:04:07,930 --> 00:04:10,120
it's possible that you don't actually know
the

59
00:04:10,120 --> 00:04:11,450
right answer when you're done with this
one.

60
00:04:11,450 --> 00:04:13,990
So the right answer is element.

61
00:04:13,990 --> 00:04:18,870
So each thing we store, we talk about an
array of elements.

62
00:04:18,870 --> 00:04:21,910
So the thing at location zero is an array
element, and

63
00:04:21,910 --> 00:04:25,220
the thing at location one is an array
element and so on.

64
00:04:25,220 --> 00:04:26,180
So that the values

65
00:04:26,180 --> 00:04:30,190
that we're storing in the array are called
elements, another end lecture quiz.

66
00:04:34,180 --> 00:04:40,190
The number we use to talk about the
location in an array is called the index.

67
00:04:40,190 --> 00:04:43,080
So, zero is an index, and eight is an
index.

68
00:04:43,080 --> 00:04:45,760
They're indexes into the array.

69
00:04:45,760 --> 00:04:49,770
Now, hopefully, you recall that you've
heard this terminology before.

70
00:04:49,770 --> 00:04:54,630
We talked about indexes before, fairly
recently, Module 7.

71
00:04:54,630 --> 00:05:00,360
Because we talked about accessing a
particular character in a string

72
00:05:00,360 --> 00:05:07,000
by using the index of that character.
We were actually ahead of the game

73
00:05:07,000 --> 00:05:12,030
we are actually accessing a particular
item in an array by using an index.

74
00:05:12,030 --> 00:05:17,340
So that's why that terminology index was
used there, and we will use it here too.

75
00:05:17,340 --> 00:05:25,570
So the index is a location in the array.
So, let's go to the code we had before.

76
00:05:25,570 --> 00:05:28,320
This was mouse input processing, right?

77
00:05:28,320 --> 00:05:31,020
So, I press F5 to run the code, it

78
00:05:31,020 --> 00:05:35,610
follows me around, and I click to change
the character.

79
00:05:35,610 --> 00:05:39,540
And, I only had four characters in this
particular game.

80
00:05:39,540 --> 00:05:42,310
Oh, it is a game, I was interacting with
it.

81
00:05:42,310 --> 00:05:48,150
in the game, but it could have been that I
had hundreds of characters or something.

82
00:05:49,240 --> 00:05:49,950
We will see.

83
00:05:49,950 --> 00:05:51,210
Let's convert this

84
00:05:51,210 --> 00:05:54,330
to use an array instead of the four

85
00:05:54,330 --> 00:05:59,450
separate variables for the different
character texture 2Ds.

86
00:05:59,450 --> 00:06:03,005
So I'm going to start here and instead of
having these

87
00:06:03,005 --> 00:06:06,880
four variables, I'm going to declare a
single variable that's an array.

88
00:06:06,880 --> 00:06:12,600
And the way I say it's an array, first I
put the data type of the elements.

89
00:06:12,600 --> 00:06:15,970
This array is going to store texture to
these.

90
00:06:15,970 --> 00:06:16,400
So that's

91
00:06:16,400 --> 00:06:17,240
what I put first.

92
00:06:18,990 --> 00:06:20,420
But then to tell the compiler I'm

93
00:06:20,420 --> 00:06:23,700
declaring an array variable, I put square
brackets.

94
00:06:24,860 --> 00:06:26,540
Now, I put a variable name, I'm going to

95
00:06:26,540 --> 00:06:29,230
call these characters maybe, I'm going to
call these characters

96
00:06:33,650 --> 00:06:39,350
and remember because arrays are objects.
I'm going to need to create a new array.

97
00:06:40,630 --> 00:06:46,110
And I want to create a new array of
Texture2Ds, but between the square

98
00:06:46,110 --> 00:06:52,250
brackets on this side, I need to say
exactly how many elements I want.

99
00:06:52,250 --> 00:06:54,165
And in this particular case, I want

100
00:06:54,165 --> 00:07:00,160
four elements because I'm replacing four
variables, semicolon.

101
00:07:00,160 --> 00:07:04,600
And that declares the array variable and
creates

102
00:07:04,600 --> 00:07:08,170
the array object.
Now, if I were to put the debugger right

103
00:07:08,170 --> 00:07:15,610
here, a break point, and get rid of that
old break point I have hanging around.

104
00:07:20,070 --> 00:07:22,680
Let's stop.
Let's try,

105
00:07:22,680 --> 00:07:22,680
[LAUGH],

106
00:07:22,680 --> 00:07:26,840
that again.
We'll put the break-point here after all.

107
00:07:28,140 --> 00:07:30,480
If I F5, and now I look at this characters

108
00:07:30,480 --> 00:07:33,050
array I've hovered over, now I know it's
hard to see.

109
00:07:33,050 --> 00:07:36,820
You can also, by the way, click around

110
00:07:36,820 --> 00:07:40,340
in here to expand what's in the
character's array.

111
00:07:40,340 --> 00:07:42,180
Maybe that's easier to see.

112
00:07:42,180 --> 00:07:45,950
But you can see each of these four
elements in the array is null right now.

113
00:07:45,950 --> 00:07:47,740
That's the default value for a reference

114
00:07:47,740 --> 00:07:50,210
type, is it starts at null.

115
00:07:50,210 --> 00:07:53,310
And so, even though I created the ray,
array

116
00:07:53,310 --> 00:07:55,770
object, I didn't create all those
characters in it.

117
00:07:56,800 --> 00:08:01,350
I'll F5 and get out of here.
Get rid of that break point.

118
00:08:01,350 --> 00:08:05,380
Now, I'm going to delete these four
variables, I don't need them anymore.

119
00:08:05,380 --> 00:08:09,720
I have this array instead, of course, that
means, I don't compile anymore.

120
00:08:11,570 --> 00:08:12,940
Because down here,

121
00:08:12,940 --> 00:08:15,810
where I was trying to load those, right.

122
00:08:15,810 --> 00:08:19,710
I used to be loading into these four
separate variables.

123
00:08:19,710 --> 00:08:24,940
Instead, I'm going to index into the array
using

124
00:08:24,940 --> 00:08:29,720
the same notations we use to index to get
a particular character from a string.

125
00:08:30,960 --> 00:08:34,610
And so I put the variable name square,

126
00:08:34,610 --> 00:08:38,040
open square bracket, the index I want to
access and

127
00:08:38,040 --> 00:08:39,310
the closed square bracket.

128
00:08:42,180 --> 00:08:46,370
And of course I need the actual variable
name characters,

129
00:08:48,750 --> 00:08:52,900
so that now I can compile.
I'm still getting compilation errors and

130
00:08:52,900 --> 00:08:57,680
we'll fix those soon.
But before we do, you should go do an in

131
00:08:57,680 --> 00:09:02,390
lecture quiz about accessing individual
elements of the array.

132
00:09:04,250 --> 00:09:09,162
Now that you're back, let's finish that
conversion to using

133
00:09:09,162 --> 00:09:14,220
the array.
This is our next problem, and again,

134
00:09:14,220 --> 00:09:18,320
we'll just access characters zero instead.

135
00:09:18,320 --> 00:09:22,750
And so far, you might not see, be seeing a
whole bunch of benefit, right?

136
00:09:22,750 --> 00:09:27,390
I mean, we did, in fact, reduce those four
variables to one.

137
00:09:27,390 --> 00:09:29,950
And the rest of this, we haven't sort of

138
00:09:29,950 --> 00:09:33,540
done any reduction in code, or anything
like that.

139
00:09:33,540 --> 00:09:40,070
Here's where we see a real win is here,
when we're changing to a random character.

140
00:09:40,070 --> 00:09:44,130
So, we used to have all this code, to

141
00:09:44,130 --> 00:09:48,160
actually figure out what the next
character was going to be.

142
00:09:48,160 --> 00:09:49,410
And I'm going to comment that out,

143
00:09:51,430 --> 00:09:54,680
and I'm going to say that the current
character.

144
00:09:57,690 --> 00:10:02,950
Get set equal to some element of the
character's array.

145
00:10:06,010 --> 00:10:11,230
And all I have to say is which index.
But which index can be this

146
00:10:11,230 --> 00:10:16,268
random number that I generated between
zero and three

147
00:10:16,268 --> 00:10:21,040
inclusive.
So this single line of

148
00:10:21,040 --> 00:10:26,320
code does what all of this used to do

149
00:10:26,320 --> 00:10:31,510
before.
And when I run the code again, I still am

150
00:10:31,510 --> 00:10:34,470
randomly getting different characters,
with some

151
00:10:34,470 --> 00:10:37,230
duplicates, as we've discussed, and so on.

152
00:10:37,230 --> 00:10:43,510
So there's, there's a big benefit from
using the arrays, I'll close out of here.

153
00:10:43,510 --> 00:10:48,590
There's a big benefit from using these
arrays, because it

154
00:10:48,590 --> 00:10:52,450
let's us reduce the number of variables we
have to have.

155
00:10:52,450 --> 00:10:54,950
And in some cases it significantly reduces
the

156
00:10:54,950 --> 00:10:57,120
amount of code we need to have as well.

157
00:10:58,690 --> 00:11:01,220
We haven't even talked about it, but that
array

158
00:11:01,220 --> 00:11:05,020
that we used here, was a one dimensional
array.

159
00:11:05,020 --> 00:11:07,540
But arrays don't have to be one
dimensional, they

160
00:11:07,540 --> 00:11:10,430
can have two dimensionals, for a game
board, for example.

161
00:11:10,430 --> 00:11:13,490
If you were building a game of checkers, a
two dimensional array would

162
00:11:13,490 --> 00:11:19,280
be a great way to save each space, what's
in it, and so on.

163
00:11:19,280 --> 00:11:22,020
So, arrays don't have to be one
dimensional.

164
00:11:22,020 --> 00:11:24,170
They can have numerous dimensions.

165
00:11:24,170 --> 00:11:27,550
And you should go do an in lecture quiz to
tell me what you understand about that.

166
00:11:31,730 --> 00:11:34,620
Before I let you go, I thought you might
like to see

167
00:11:34,620 --> 00:11:39,860
a little clip of how this array discussion
goes in my regular classroom.

168
00:11:41,040 --> 00:11:45,110
So, at this point, I can tell I feel the
energy pouring off of you guys.

169
00:11:45,110 --> 00:11:51,560
You guys are like, holy crap, what a tool.
Now, I don't mean, okay wait a minute.

170
00:11:51,560 --> 00:11:51,560
[LAUGH].

171
00:11:51,560 --> 00:11:51,560
>>

172
00:11:51,560 --> 00:11:52,060
[LAUGH].

173
00:11:54,550 --> 00:11:57,420
No, no, don't look at me and think, what a
tool.

174
00:11:57,420 --> 00:12:03,700
Think about arrays and say, what a tool,
because now, now I can

175
00:12:03,700 --> 00:12:10,240
build games and if I'm going to have 400
teddy bears, I don't need 400 variables.

176
00:12:10,240 --> 00:12:14,000
Called teddy bear zero, teddy bear one,
teddy bear two, teddy bear three.

177
00:12:14,000 --> 00:12:19,620
I can just build an array that will hold
400 teddy bears, and I just

178
00:12:19,620 --> 00:12:21,700
have to declare a variable once.

179
00:12:21,700 --> 00:12:24,990
And then you just, yay, hazzah, and
there's much rejoicing.

180
00:12:24,990 --> 00:12:30,840
Okay, so we learned how to solve a core
problem here.

181
00:12:30,840 --> 00:12:38,510
We learned how to store and use multiple
different things all same data type,

182
00:12:38,510 --> 00:12:45,080
multiple different game entities so that
we could efficiently use and process them.

183
00:12:45,080 --> 00:12:50,590
Next time, we're going to discover and
even more robust way that we can

184
00:12:50,590 --> 00:12:56,530
store these multiple elements to overcome
one of the core limitations of arrays.

185
00:12:56,530 --> 00:13:00,450
Say right now, the core limitation of the
array is when we created the

186
00:13:00,450 --> 00:13:04,650
array object, we had to say how many
elements there were going to be.

187
00:13:04,650 --> 00:13:06,100
And so if we have a game where we

188
00:13:06,100 --> 00:13:10,420
spawn elements or kill elements, game
entities, excuse me,

189
00:13:10,420 --> 00:13:14,030
then arrays are harder to work with.

190
00:13:14,030 --> 00:13:17,580
So next time, we're going to learn about
something called collection classes.

191
00:13:17,580 --> 00:13:20,960
That we can use to, basically use many of
the same

192
00:13:20,960 --> 00:13:24,060
ideas that we get with arrays, but in a
more robust way.