1
00:00:00,025 --> 00:00:06,787
This is our second and final

2
00:00:06,787 --> 00:00:12,380
lecture in module nine.

3
00:00:12,380 --> 00:00:16,010
As a reminder here that our learning
objective through the module.

4
00:00:19,800 --> 00:00:25,582
So last time, we talked about arrays as a
way to store multiple

5
00:00:25,582 --> 00:00:31,720
game entities, if you will, elements that
we could then process in a reasonable way.

6
00:00:31,720 --> 00:00:34,280
And it saved on how many variables we
needed.

7
00:00:34,280 --> 00:00:37,670
And we saw how it could save us some code
and so on.

8
00:00:37,670 --> 00:00:40,770
Arrays are a structure that tends to be

9
00:00:40,770 --> 00:00:44,900
available in pretty much every modern
programming language.

10
00:00:44,900 --> 00:00:50,210
Though in most of them, as in c sharp, we
need to know how many elements of array

11
00:00:50,210 --> 00:00:55,920
we need when we create the array object.
In some cases, that works just fine.

12
00:00:55,920 --> 00:00:59,270
But in cases where the size of, excuse me,
the

13
00:00:59,270 --> 00:01:03,470
number of the elements we need to store
can change dynamically.

14
00:01:03,470 --> 00:01:06,370
For example, in a game where we're
actually

15
00:01:06,370 --> 00:01:09,580
spawning stuff, and killing stuff off, and
so on.

16
00:01:09,580 --> 00:01:10,320
That's a

17
00:01:10,320 --> 00:01:13,840
problematic limitation So this time we're
going to look

18
00:01:13,840 --> 00:01:18,610
at something called collection classes
that don't have that limitation.

19
00:01:18,610 --> 00:01:22,790
They can dynamically grow and shrink as we
need them to as the game runs.

20
00:01:25,220 --> 00:01:28,390
You should immediately take an in-lecture
quiz to tell

21
00:01:28,390 --> 00:01:31,190
me what you know about what collection
classes can hold.

22
00:01:34,980 --> 00:01:37,790
And this slide should look really familiar
to you because I

23
00:01:37,790 --> 00:01:42,580
basically came in and replaced the word
arrays with collection classes.

24
00:01:42,580 --> 00:01:48,220
So, just as with arrays, we can store
anything.

25
00:01:49,410 --> 00:01:50,700
But.

26
00:01:50,700 --> 00:01:52,220
Same as with arrays.

27
00:01:52,220 --> 00:01:57,110
A particular collection, which is an
object just like arrays are.

28
00:01:57,110 --> 00:02:00,670
A particular collection can only store

29
00:02:00,670 --> 00:02:04,330
a single data type, and we can have lots
of different collections that store

30
00:02:04,330 --> 00:02:10,050
lots of different stuff, but we can't mix
data types in a particular collection.

31
00:02:10,050 --> 00:02:14,380
So, the next question is where can we find
these things?

32
00:02:14,380 --> 00:02:17,870
And for that, we should open up the IDE
and look at Help.

33
00:02:21,340 --> 00:02:25,290
So, here is the project that we used last
time with arrays.

34
00:02:25,290 --> 00:02:27,330
This was our final product from last time.

35
00:02:27,330 --> 00:02:31,110
And I'm going to convert it to use a
particular collection class.

36
00:02:31,110 --> 00:02:34,019
But, before I do, I want to go to Help.

37
00:02:35,210 --> 00:02:39,240
And show you where you can find all the
collection classes.

38
00:02:39,240 --> 00:02:49,080
So, this is a name state, a name space,
system dot collections

39
00:02:49,080 --> 00:02:50,490
dot generic.

40
00:02:50,490 --> 00:02:53,180
And we'll talk about generic again
momentarily.

41
00:02:54,200 --> 00:02:57,470
But I'm going to click on the very first
search result.

42
00:02:57,470 --> 00:02:59,500
And you can see here that there are a

43
00:02:59,500 --> 00:03:03,540
whole bunch of different collection
classes we can use.

44
00:03:03,540 --> 00:03:09,090
And as you go through your programming
career, you will discover

45
00:03:09,090 --> 00:03:13,460
that some of these are more effective in
particular situations than others.

46
00:03:13,460 --> 00:03:14,580
But for us,

47
00:03:14,580 --> 00:03:15,870
for right now.

48
00:03:15,870 --> 00:03:20,810
We're going to go and use the list
collection class, this one right here.

49
00:03:20,810 --> 00:03:23,510
But when you're going to look for
collection classes, you

50
00:03:23,510 --> 00:03:26,980
should go to the systems dot collections
dot generic namespace.

51
00:03:26,980 --> 00:03:26,980
[INAUDIBLE]

52
00:03:26,980 --> 00:03:26,980
.

53
00:03:26,980 --> 00:03:32,310
In the interest of full disclosure I
should tell you can also go to the systems

54
00:03:32,310 --> 00:03:36,200
dot collections names space, but the
generic collections

55
00:03:36,200 --> 00:03:38,730
tend to be the ones that are most useful.

56
00:03:38,730 --> 00:03:41,420
And, and better programnming practice in
general.

57
00:03:42,450 --> 00:03:44,460
So here we are.

58
00:03:44,460 --> 00:03:52,150
And what I want to do is I want to convert
this from a array, from an

59
00:03:52,150 --> 00:03:55,680
array into a list instead.

60
00:03:55,680 --> 00:03:58,740
So what I'm going to do is I'll just
comment this up.

61
00:04:01,060 --> 00:04:07,550
The first thing we need is a data type,
and the data type is list as we saw.

62
00:04:07,550 --> 00:04:08,130
However,

63
00:04:10,330 --> 00:04:15,050
it's actually a generic so we have to say
a list of what, just

64
00:04:15,050 --> 00:04:19,070
as we had to say an array of what when we
were declaring an array.

65
00:04:19,070 --> 00:04:21,110
Here we have to say a list of what?

66
00:04:21,110 --> 00:04:26,080
And let's make it Texture2D, because
that's what we want to store.

67
00:04:26,080 --> 00:04:31,060
Now, you should be, hopefully, thinking
you've seen something like this before.

68
00:04:31,060 --> 00:04:35,850
When we use the Content Manager Load
method, I said this

69
00:04:35,850 --> 00:04:38,800
is an instance of a generic, and we had
to, between the

70
00:04:38,800 --> 00:04:42,370
less than and greater than signs, we had
to put the data type.

71
00:04:42,370 --> 00:04:44,340
Of the content that we need it to load.

72
00:04:45,360 --> 00:04:50,920
And that, the load method is actually a,
what's called a generic method.

73
00:04:50,920 --> 00:04:56,620
Whereas this list class, is a generic
class, but the idea is the same.

74
00:04:56,620 --> 00:04:59,780
We have to put the data type that we want
to store.

75
00:04:59,780 --> 00:05:00,880
In between the less than and

76
00:05:00,880 --> 00:05:01,450
greater than.

77
00:05:01,450 --> 00:05:04,210
So you've seen this before, when we loaded
content.

78
00:05:04,210 --> 00:05:06,290
This is in a slightly different context.

79
00:05:06,290 --> 00:05:10,800
It's basically saying the data type of the
elements we're going to store in the list.

80
00:05:10,800 --> 00:05:12,400
So I can still call it characters, and

81
00:05:15,600 --> 00:05:16,830
it's still an object,

82
00:05:19,000 --> 00:05:21,760
and in fact it's a list of texture 2Ds

83
00:05:24,650 --> 00:05:29,630
So I have now created this new list, the
only other thing I need

84
00:05:29,630 --> 00:05:34,070
to change in this code is to convert from
arrays to list, is right

85
00:05:34,070 --> 00:05:40,380
here where I was setting these particular,
where I was loading the elements

86
00:05:40,380 --> 00:05:45,970
of the array, because with the list class,
we don't actually do it this way.

87
00:05:45,970 --> 00:05:48,260
We add things to the list.

88
00:05:48,260 --> 00:05:50,290
So instead of saying characters

89
00:05:50,290 --> 00:05:55,128
zero equal content load that Texture2D,
I'm going to say

90
00:05:55,128 --> 00:06:01,920
characters.Add, like that.

91
00:06:01,920 --> 00:06:06,150
And what happens is it will load this
Texture2D,

92
00:06:06,150 --> 00:06:09,080
the content manager still loads this
content as it did

93
00:06:09,080 --> 00:06:12,420
before and it will add this to the begin,
sorry,

94
00:06:12,420 --> 00:06:14,190
it will add it to the end of the list.

95
00:06:14,190 --> 00:06:15,440
And since this is an empty

96
00:06:15,440 --> 00:06:22,700
list, it will add it at the zero location.
And so, to add each of these to the list,

97
00:06:25,320 --> 00:06:29,148
we just change the syntax.

98
00:06:29,148 --> 00:06:35,790
Like this.

99
00:06:35,790 --> 00:06:38,526
And this will work to add character zero
at

100
00:06:38,526 --> 00:06:42,160
location zero in the list, character one
at location one

101
00:06:42,160 --> 00:06:44,299
in the list, character two at location two
in the

102
00:06:44,299 --> 00:06:46,710
list, and character three at location
three in the list.

103
00:06:49,440 --> 00:06:50,270
And when I run this.

104
00:06:52,290 --> 00:06:56,880
It works exactly the way it did before,
except now we're using

105
00:06:56,880 --> 00:07:01,380
a collection that we dynamically grouped
as we went along.

106
00:07:03,010 --> 00:07:08,080
So, that's it for the conversion of the
code to a collection.

107
00:07:09,420 --> 00:07:15,990
What that means Is I have a couple of
in-lecture quizzes for you.

108
00:07:15,990 --> 00:07:18,210
So, do this first one

109
00:07:18,210 --> 00:07:22,100
that really gets at the key difference
between collection classes and arrays.

110
00:07:25,980 --> 00:07:28,350
And, now do this one which addresses
something

111
00:07:28,350 --> 00:07:30,760
that I haven't even spoken about at all
today.

112
00:07:33,830 --> 00:07:36,730
So the answer to that is, we access
elements of

113
00:07:36,730 --> 00:07:39,860
the list just like we accessed elements of
the array.

114
00:07:39,860 --> 00:07:44,010
We use the sqaure bracket notation to get
at stuff in the list.

115
00:07:44,010 --> 00:07:50,620
Now, it might be hard to see from this
example why collection classes are useful.

116
00:07:50,620 --> 00:07:50,900
Right?

117
00:07:50,900 --> 00:07:56,820
Cause Sure I changed some syntax here, but
we really didn't see any huge benefit.

118
00:07:58,230 --> 00:08:00,020
Next module,

119
00:08:00,020 --> 00:08:02,310
we will see why this is really useful.

120
00:08:02,310 --> 00:08:07,790
We need one more piece of understanding
about how c sharp works before we can

121
00:08:07,790 --> 00:08:10,320
see why collection classes are so much
more

122
00:08:10,320 --> 00:08:14,300
powerful then arrays for, for games in
general.

123
00:08:14,300 --> 00:08:19,320
So we'll have to wait patiently.
But we did see that collection classes can

124
00:08:19,320 --> 00:08:25,150
also be used to store multiple elements
for ease of processing, just as

125
00:08:25,150 --> 00:08:27,020
we used arrays for.

126
00:08:27,020 --> 00:08:29,940
We learned that collection classes can
grow and

127
00:08:29,940 --> 00:08:33,840
shrink dynamically, which is something
that arrays can't do.

128
00:08:33,840 --> 00:08:35,850
And then next time, we'll start on the

129
00:08:35,850 --> 00:08:39,110
next module where we'll learn the final
control

130
00:08:39,110 --> 00:08:41,940
structure we need to be able to solve,

131
00:08:41,940 --> 00:08:44,630
sort of, almost any problem you can
imagine.