1
00:00:00,149 --> 00:00:03,482
(playful ambient music)

2
00:00:05,620 --> 00:00:08,140
Okay, so moving on with the Kotlin tutorial,

3
00:00:08,140 --> 00:00:09,640
I'm gonna go into a bit more detail now

4
00:00:09,640 --> 00:00:12,690
with classes and look at them more in depth.

5
00:00:12,690 --> 00:00:14,370
Now, an important to thing understand,

6
00:00:14,370 --> 00:00:16,070
and it took me a while to get the hang of this

7
00:00:16,070 --> 00:00:18,970
when I first started object-oriented programming,

8
00:00:18,970 --> 00:00:22,420
is that a class is like a cookie cutter, or a template,

9
00:00:22,420 --> 00:00:24,790
and from that you can create an unlimited number

10
00:00:24,790 --> 00:00:27,500
of player objects or player instances.

11
00:00:27,500 --> 00:00:28,700
Now at the end of the last video,

12
00:00:28,700 --> 00:00:32,580
we created four instances from our player class.

13
00:00:32,580 --> 00:00:34,830
So what I'm gonna do is delete the

14
00:00:34,830 --> 00:00:36,920
commented out code firstly, these four printlns,

15
00:00:36,920 --> 00:00:38,703
because we don't need those anymore.

16
00:00:39,720 --> 00:00:41,470
And then just run the programme again

17
00:00:41,470 --> 00:00:42,670
even though it's still showing on the screen,

18
00:00:42,670 --> 00:00:45,020
just to remind ourselves what it was doing.

19
00:00:45,020 --> 00:00:46,620
And you can see it's outputting the four

20
00:00:46,620 --> 00:00:50,270
four player instances there and their values.

21
00:00:50,270 --> 00:00:53,250
So we're getting four totally separate objects displayed,

22
00:00:53,250 --> 00:00:56,600
but they were all created from the same template class,

23
00:00:56,600 --> 00:00:59,000
our player class, in this case.

24
00:00:59,000 --> 00:01:00,650
Now they all have the same properties,

25
00:01:00,650 --> 00:01:03,440
they have a name, and lives, level,

26
00:01:03,440 --> 00:01:06,830
and score properties, but each instance or object

27
00:01:06,830 --> 00:01:09,630
has different values for those properties.

28
00:01:09,630 --> 00:01:11,760
Now they also all have a show method,

29
00:01:11,760 --> 00:01:14,240
or a show function, they mean the same thing.

30
00:01:14,240 --> 00:01:17,550
Kotlin tends to use the term function, by the way,

31
00:01:17,550 --> 00:01:20,840
but the object-oriented term is method.

32
00:01:20,840 --> 00:01:22,610
So you hear me use both words to refer

33
00:01:22,610 --> 00:01:25,350
to a function as part of a class.

34
00:01:25,350 --> 00:01:29,620
Now if you look at the show method, in the player class,

35
00:01:29,620 --> 00:01:31,580
it just prints out the values of the properties

36
00:01:31,580 --> 00:01:35,610
as you can see from lines 7 through 14.

37
00:01:35,610 --> 00:01:37,500
Now each instance of the player class has

38
00:01:37,500 --> 00:01:39,360
different values for the properties,

39
00:01:39,360 --> 00:01:42,340
which is why calling tim.show on line three

40
00:01:42,340 --> 00:01:44,600
of our main method, here,

41
00:01:44,600 --> 00:01:49,583
prints different values to the louise.show on line six.

42
00:01:49,583 --> 00:01:51,180
Okay, so let's extend our player class now

43
00:01:51,180 --> 00:01:53,190
a little bit further.

44
00:01:53,190 --> 00:01:55,850
So we look at our player class again,

45
00:01:55,850 --> 00:01:59,140
so we've got name, lives, level, and score.

46
00:01:59,140 --> 00:02:02,330
And we've used a string and three int types.

47
00:02:02,330 --> 00:02:03,710
But you're not restricted to using

48
00:02:03,710 --> 00:02:05,630
just those types of variables,

49
00:02:05,630 --> 00:02:08,060
we could use an object as a type and a class.

50
00:02:08,060 --> 00:02:10,990
In fact, in Kotlin, string and int are classes,

51
00:02:10,990 --> 00:02:13,650
so we're already using objects.

52
00:02:13,650 --> 00:02:16,550
Now, I'm gonna make more sense if I create an example,

53
00:02:16,550 --> 00:02:19,040
so I'm gonna create this new class called Weapon

54
00:02:19,040 --> 00:02:20,820
to show what I mean there.

55
00:02:20,820 --> 00:02:23,310
So as before, when creating a new class,

56
00:02:23,310 --> 00:02:25,680
you click on, or right click on the Java folder,

57
00:02:25,680 --> 00:02:29,883
or Java directory, New, and select Kotlin File/Class.

58
00:02:30,850 --> 00:02:32,350
And I'll call this one Weapon.

59
00:02:34,252 --> 00:02:35,580
And I'm gonna leave this set to File this time

60
00:02:35,580 --> 00:02:36,750
just to show you what you'd have to do

61
00:02:36,750 --> 00:02:39,023
if you didn't select the class.

62
00:02:40,698 --> 00:02:42,220
And you can see that it's just created a comment,

63
00:02:42,220 --> 00:02:45,080
but it hasn't actually created a class declaration,

64
00:02:45,080 --> 00:02:46,620
so I can actually just type that in manually.

65
00:02:46,620 --> 00:02:49,690
So, class, Weapon...

66
00:02:49,690 --> 00:02:52,190
In this case we're gonna give it some properties

67
00:02:52,190 --> 00:02:53,800
in the primary constructor.

68
00:02:53,800 --> 00:02:55,580
Remember that we define them in parentheses

69
00:02:55,580 --> 00:02:57,080
immediately after the class name,

70
00:02:57,080 --> 00:02:59,100
so we put parentheses there.

71
00:02:59,100 --> 00:03:01,760
So the properties that we want for weapon,

72
00:03:01,760 --> 00:03:05,593
we're gonna start with, val name, colon, String,

73
00:03:06,720 --> 00:03:11,720
comma, then var damageInflicted, colon, Int,

74
00:03:13,320 --> 00:03:16,360
closing parentheses and I'm gonna open the code block,

75
00:03:16,360 --> 00:03:18,200
or create a block of code

76
00:03:18,200 --> 00:03:20,320
using the left and right curly braces.

77
00:03:20,320 --> 00:03:23,120
So a weapon's gonna have a name, like axe or sword,

78
00:03:23,120 --> 00:03:24,940
and we'll store that in the string field,

79
00:03:24,940 --> 00:03:26,750
then we have an int to show how much damage

80
00:03:26,750 --> 00:03:29,170
this particular weapon does.

81
00:03:29,170 --> 00:03:31,700
Now I haven't used any default values there,

82
00:03:31,700 --> 00:03:34,080
so we'll have to specify both values

83
00:03:34,080 --> 00:03:36,140
when we create our weapon objects.

84
00:03:36,140 --> 00:03:39,980
Not that I've made damage inflicted a Var property.

85
00:03:39,980 --> 00:03:41,340
Now it probably wouldn't change

86
00:03:41,340 --> 00:03:43,360
in a normal course of a game like this,

87
00:03:43,360 --> 00:03:46,190
but I want to use it to demonstrate something else soon.

88
00:03:46,190 --> 00:03:48,350
So imagine that there's some magic spell in the game

89
00:03:48,350 --> 00:03:50,960
that can make weapons more deadly or something.

90
00:03:50,960 --> 00:03:53,010
Okay, so moving on, and we've now got

91
00:03:53,010 --> 00:03:55,240
a very simple class that can be used

92
00:03:55,240 --> 00:03:58,230
to represent a weapon in a game.

93
00:03:58,230 --> 00:04:01,010
So let's use this class in our player class,

94
00:04:01,010 --> 00:04:03,180
we'll let the player have weapons.

95
00:04:03,180 --> 00:04:05,090
So we need a property in the player class

96
00:04:05,090 --> 00:04:07,330
to store the weapon in, and it's usual

97
00:04:07,330 --> 00:04:09,540
to declare properties before methods,

98
00:04:09,540 --> 00:04:11,590
so I'm gonna go back to the player class,

99
00:04:11,590 --> 00:04:14,370
we're gonna add this at the start of the class.

100
00:04:14,370 --> 00:04:18,010
So, just below the class definition on line five,

101
00:04:18,010 --> 00:04:19,980
I'm gonna come over here, tab over,

102
00:04:19,980 --> 00:04:22,303
and type var, weapon,

103
00:04:23,140 --> 00:04:25,713
colon, then Weapon, with a capital W,

104
00:04:27,162 --> 00:04:29,260
and I'm gonna press enter.

105
00:04:29,260 --> 00:04:31,410
Now you see that sort of thing done a lot

106
00:04:31,410 --> 00:04:32,690
in Kotlin programming.

107
00:04:32,690 --> 00:04:34,910
The class name normally gives a good indication

108
00:04:34,910 --> 00:04:38,560
of what it represents so Weapon, with a capital W,

109
00:04:38,560 --> 00:04:41,780
tells us that the class represents a weapon.

110
00:04:41,780 --> 00:04:43,900
So that's the type of the property,

111
00:04:43,900 --> 00:04:46,760
and we call it weapon with a lower case w.

112
00:04:46,760 --> 00:04:48,480
Now we could've called it something else,

113
00:04:48,480 --> 00:04:50,370
such as player's weapon or something,

114
00:04:50,370 --> 00:04:53,600
but weapon's a good name for the object.

115
00:04:53,600 --> 00:04:56,610
Because variables in Kotlin are case-sensitive,

116
00:04:56,610 --> 00:04:58,340
Weapon with a capital W

117
00:04:58,340 --> 00:05:00,270
and weapon with a lower case w

118
00:05:00,270 --> 00:05:02,560
are referring to different things.

119
00:05:02,560 --> 00:05:04,830
So here, Weapon, with a capital W,

120
00:05:04,830 --> 00:05:07,120
is the name of the class,

121
00:05:07,120 --> 00:05:09,300
and weapon, with a lower case w,

122
00:05:09,300 --> 00:05:11,710
is an object or instance of that type,

123
00:05:11,710 --> 00:05:14,010
or it will be once we create the weapon.

124
00:05:14,010 --> 00:05:15,690
So you'll see that sort of thing done a lot

125
00:05:15,690 --> 00:05:17,820
when we use Android classes.

126
00:05:17,820 --> 00:05:19,638
And you'll see things like, I'm just gonna type

127
00:05:19,638 --> 00:05:22,920
some code in temporarily, val recyclerViewAdapter,

128
00:05:25,510 --> 00:05:28,033
colon, RecyclerViewAdapter.

129
00:05:30,270 --> 00:05:31,540
That's an example of code

130
00:05:31,540 --> 00:05:33,650
that you'll actually see in Android.

131
00:05:33,650 --> 00:05:37,110
It's very common to use a class name for the variable name,

132
00:05:37,110 --> 00:05:39,760
but with a lower case initial letter.

133
00:05:39,760 --> 00:05:42,340
All right, so I'm gonna delete that line.

134
00:05:42,340 --> 00:05:45,200
Now let's give the player a default weapon,

135
00:05:45,200 --> 00:05:48,270
when the player objects or instances are created.

136
00:05:48,270 --> 00:05:49,830
Now, at the moment we've got an error

137
00:05:49,830 --> 00:05:51,900
in the declaration of our weapon,

138
00:05:51,900 --> 00:05:53,750
and that's because Kotlin wants everything

139
00:05:53,750 --> 00:05:56,570
to be given a value when it's declared.

140
00:05:56,570 --> 00:05:58,750
Now, you'll see ways to avoid doing that,

141
00:05:58,750 --> 00:06:01,670
and of deferring the initial value until later,

142
00:06:01,670 --> 00:06:03,910
but for now we're gonna give everything a value.

143
00:06:03,910 --> 00:06:05,970
So when our players first start playing,

144
00:06:05,970 --> 00:06:07,280
they won't have found any weapons,

145
00:06:07,280 --> 00:06:09,520
so we're just gonna give them a fist to start with.

146
00:06:09,520 --> 00:06:12,490
So we've typed var weapon, colon, Weapon

147
00:06:12,490 --> 00:06:13,900
then we're gonna put after that an assignment,

148
00:06:13,900 --> 00:06:16,770
so, equals, Weapon with a capital W,

149
00:06:16,770 --> 00:06:20,520
parentheses, double quotes, fist,

150
00:06:20,520 --> 00:06:24,430
comma, one, and ending parentheses.

151
00:06:24,430 --> 00:06:27,120
So now, each time we create a new player,

152
00:06:27,120 --> 00:06:30,140
the code will automatically create a new weapon object,

153
00:06:30,140 --> 00:06:32,250
which in this case will be a fist.

154
00:06:32,250 --> 00:06:33,400
Now, they're gonna be finding more

155
00:06:33,400 --> 00:06:35,240
weapons as they play the game,

156
00:06:35,240 --> 00:06:37,970
so that's why I've made weapon a var there.

157
00:06:37,970 --> 00:06:40,720
Now we want to assign different weapons to it ultimately.

158
00:06:40,720 --> 00:06:42,970
Hopefully, you're starting to see when to use val

159
00:06:42,970 --> 00:06:45,180
and when to use var.

160
00:06:45,180 --> 00:06:47,120
Now don't worry if it's still unclear,

161
00:06:47,120 --> 00:06:49,820
just remember you can always change a declaration

162
00:06:49,820 --> 00:06:51,540
if you decide that you should've used var,

163
00:06:51,540 --> 00:06:53,550
for example, instead of val.

164
00:06:53,550 --> 00:06:55,770
So let's actually check if this all works

165
00:06:55,770 --> 00:06:58,210
by printing out the name of Ace's weapon

166
00:06:58,210 --> 00:07:00,900
and the damage it can do in our main function.

167
00:07:00,900 --> 00:07:02,910
So we're gonna come over here to Main,

168
00:07:02,910 --> 00:07:05,634
and we're going to, after this

169
00:07:05,634 --> 00:07:08,923
one2watch.show, we're going to put println,

170
00:07:10,160 --> 00:07:12,553
parentheses, one2watch.weapon.name,

171
00:07:18,820 --> 00:07:20,370
then on the next line, println,

172
00:07:22,070 --> 00:07:26,480
one2watch.weapon.damageInflicted.

173
00:07:26,480 --> 00:07:27,823
First run the programme,

174
00:07:31,066 --> 00:07:32,220
and we can see down at the bottom here,

175
00:07:32,220 --> 00:07:34,430
we've got fist and 1 printed.

176
00:07:34,430 --> 00:07:37,360
So notice how we're still using dot notation

177
00:07:37,360 --> 00:07:39,980
to access the properties of the two classes.

178
00:07:39,980 --> 00:07:42,930
So one2watch.weapon gives us a reference

179
00:07:42,930 --> 00:07:45,880
to Ace's weapon object and we can access

180
00:07:45,880 --> 00:07:48,720
the properties of the weapon by using more dots.

181
00:07:48,720 --> 00:07:50,970
So, .name gives the name of the weapon

182
00:07:50,970 --> 00:07:53,480
and .damageInflicted gives the amount

183
00:07:53,480 --> 00:07:55,390
of damage that weapon does.

184
00:07:55,390 --> 00:07:57,320
Now the name here is a string,

185
00:07:57,320 --> 00:08:00,000
and I mentioned that string is also a class.

186
00:08:00,000 --> 00:08:01,800
So we can expect the string class to

187
00:08:01,800 --> 00:08:04,140
have some functions of it's own,

188
00:08:04,140 --> 00:08:05,860
and it turns out that it does.

189
00:08:05,860 --> 00:08:07,100
So I'm not gonna list them all here,

190
00:08:07,100 --> 00:08:10,550
but a use for one converts the string to uppercase.

191
00:08:10,550 --> 00:08:12,360
So we can come over here to weapon.name,

192
00:08:12,360 --> 00:08:14,810
and at the end of that we can add a .toUppercase.

193
00:08:16,980 --> 00:08:18,890
Select that and press enter.

194
00:08:18,890 --> 00:08:21,797
Now we're using one2watch.weapon to access the weapon,

195
00:08:21,797 --> 00:08:24,340
and using .name to get the weapon's name,

196
00:08:24,340 --> 00:08:28,440
and finally, calling the names toUpperCase function

197
00:08:28,440 --> 00:08:31,200
to convert the name string to capitals.

198
00:08:31,200 --> 00:08:32,460
So you can chain these property

199
00:08:32,460 --> 00:08:35,270
and method accesses as far as you wanna go.

200
00:08:35,270 --> 00:08:36,309
So let's actually just run that to

201
00:08:36,309 --> 00:08:37,860
confirm that it actually works.

202
00:08:39,575 --> 00:08:41,900
Let's see, we've got fist now showing in upper case.

203
00:08:41,900 --> 00:08:43,409
And this chaining is something that you'll

204
00:08:43,409 --> 00:08:45,990
see a lot of in Android programming.

205
00:08:45,990 --> 00:08:48,980
In fact, in Kotlin programming in general, as well.

206
00:08:48,980 --> 00:08:51,210
Many of the built in Java classes have been

207
00:08:51,210 --> 00:08:53,810
designed to be used in this way.

208
00:08:53,810 --> 00:08:54,980
Now, I'm not gonna go into any more

209
00:08:54,980 --> 00:08:58,390
complexity at this stage, but suffice to say,

210
00:08:58,390 --> 00:09:00,600
when we ran the programme as you saw on the screen,

211
00:09:00,600 --> 00:09:03,200
we've got fist now printing out in capitals

212
00:09:03,200 --> 00:09:05,430
So before I finish, we're gonna have a look

213
00:09:05,430 --> 00:09:07,370
at a couple of ways that we can give our players

214
00:09:07,370 --> 00:09:09,080
some more impressive weapons.

215
00:09:09,080 --> 00:09:12,660
Now, the first way is to create a new weapon object,

216
00:09:12,660 --> 00:09:15,590
then assign it to the player's weapon property.

217
00:09:15,590 --> 00:09:17,410
Let's actually have a go at doing that.

218
00:09:17,410 --> 00:09:19,360
So we're gonna come down here.

219
00:09:19,360 --> 00:09:23,490
After the one2watch.weapon.damageInflicted line,

220
00:09:23,490 --> 00:09:25,740
I'm gonna type val, axe

221
00:09:26,860 --> 00:09:30,520
equals, Weapon in capital W, parentheses,

222
00:09:30,520 --> 00:09:35,071
and axe in double quotes, comma, and then 12.

223
00:09:35,071 --> 00:09:39,960
Then we can type gr8.weapon, equals axe,

224
00:09:39,960 --> 00:09:42,690
then we can type println,

225
00:09:42,690 --> 00:09:46,073
gr8.weapon.name,

226
00:09:48,250 --> 00:09:51,403
fix that typo, and on the next line println,

227
00:09:53,186 --> 00:09:55,053
then axe.name.

228
00:09:57,440 --> 00:09:59,697
So we've created a new instance of weapon,

229
00:09:59,697 --> 00:10:02,600
and we assign it to the variable axe.

230
00:10:02,600 --> 00:10:06,210
Then we assign axe to gr8's weapon property.

231
00:10:06,210 --> 00:10:08,910
So gr8 now has an axe as her weapon,

232
00:10:08,910 --> 00:10:10,210
which we can see when we print it out,

233
00:10:10,210 --> 00:10:12,710
so let's just run that to make sure that it works.

234
00:10:15,653 --> 00:10:17,620
And you can see axe is showing there.

235
00:10:17,620 --> 00:10:20,550
Now axe also exists within our main function,

236
00:10:20,550 --> 00:10:22,340
and so we're printing out the value

237
00:10:22,340 --> 00:10:26,610
of the name property from axe on line 18, as well.

238
00:10:26,610 --> 00:10:29,260
So we've got two references to the same object,

239
00:10:29,260 --> 00:10:31,920
one reference is by using the axe variable

240
00:10:31,920 --> 00:10:34,880
and the other's by using the gr8.weapon.

241
00:10:34,880 --> 00:10:36,470
So it's important here to understand that they

242
00:10:36,470 --> 00:10:40,190
both refer to exactly the same object, the axe.

243
00:10:40,190 --> 00:10:42,110
Now, if that seems complicated,

244
00:10:42,110 --> 00:10:45,030
keep in mind that we do the same thing in normal speech.

245
00:10:45,030 --> 00:10:49,190
So someone might refer to my son's wife's father-in-law,

246
00:10:49,190 --> 00:10:51,610
or they may refer to me, Tim Buchalka,

247
00:10:51,610 --> 00:10:54,100
so it's the same person, unless my son has

248
00:10:54,100 --> 00:10:56,200
another wife that we're not aware of.

249
00:10:56,200 --> 00:10:58,790
Now, we can check that they're the same object

250
00:10:58,790 --> 00:11:01,700
by casting that spell I mentioned earlier on the axe.

251
00:11:01,700 --> 00:11:05,850
And we'll increase the damage inflicted by the axe to 24.

252
00:11:05,850 --> 00:11:07,860
So let's actually add the code,

253
00:11:07,860 --> 00:11:12,603
so we'll type axe.damageInflicted, equals 24,

254
00:11:14,570 --> 00:11:18,027
println, axe.damageInflicted.

255
00:11:18,951 --> 00:11:20,343
Let's run that.

256
00:11:23,010 --> 00:11:26,400
So no surprises there, we get 24 printed.

257
00:11:26,400 --> 00:11:27,880
But before I run the programme again

258
00:11:27,880 --> 00:11:29,660
after making this next change,

259
00:11:29,660 --> 00:11:31,910
have a think about what should be printed.

260
00:11:31,910 --> 00:11:34,340
So I'm gonna type in the code first,

261
00:11:34,340 --> 00:11:37,993
so println, gr8.weapon.damageInflicted.

262
00:11:41,560 --> 00:11:42,910
So have a think about what you think

263
00:11:42,910 --> 00:11:46,027
that should print out, the code on line 22.

264
00:11:46,027 --> 00:11:47,477
All right, so let's run that.

265
00:11:52,330 --> 00:11:56,190
And you can see it also shows the value of 24.

266
00:11:56,190 --> 00:11:58,370
So remember that these are two different ways

267
00:11:58,370 --> 00:12:02,010
to refer to the object, the same axe in effect.

268
00:12:02,010 --> 00:12:04,840
So when we change the damage inflicted by axe,

269
00:12:04,840 --> 00:12:09,840
on line 20 to 24, gr8's weapon now does 24 points of damage.

270
00:12:10,890 --> 00:12:12,350
So if you did work out that the programme

271
00:12:12,350 --> 00:12:14,940
should print 24 again, well done.

272
00:12:14,940 --> 00:12:16,310
And if you're not sure why, then it's worth

273
00:12:16,310 --> 00:12:18,830
watching these last couple of videos again.

274
00:12:18,830 --> 00:12:20,630
All right, so I'm gonna finish with the other way

275
00:12:20,630 --> 00:12:23,140
of assigning a weapon to one of the players.

276
00:12:23,140 --> 00:12:24,750
We'll give me a sword.

277
00:12:24,750 --> 00:12:26,250
So we're gonna come down here,

278
00:12:27,903 --> 00:12:30,000
we're gonna type tim.weapon

279
00:12:31,060 --> 00:12:35,690
is equal to Weapon, parentheses,

280
00:12:35,690 --> 00:12:39,473
double quotes, Sword, 10,

281
00:12:41,130 --> 00:12:45,853
and you're gonna println, tim.weapon.name.

282
00:12:47,770 --> 00:12:50,020
So this time we're creating a new weapon,

283
00:12:50,020 --> 00:12:53,480
and assigning it directly to my weapon property.

284
00:12:53,480 --> 00:12:55,980
Now we can see refer to it as tim.weapon,

285
00:12:55,980 --> 00:12:58,280
but we don't have a direct reference to it

286
00:12:58,280 --> 00:12:59,810
in our main function.

287
00:12:59,810 --> 00:13:00,923
So to run the programme,

288
00:13:04,759 --> 00:13:07,490
we can see it correctly prints out sword.

289
00:13:07,490 --> 00:13:09,490
So which way should you use?

290
00:13:09,490 --> 00:13:11,710
Well, the answer is that it depends.

291
00:13:11,710 --> 00:13:13,300
We're gonna be seeing one reason for using

292
00:13:13,300 --> 00:13:15,980
the first method in a later video.

293
00:13:15,980 --> 00:13:17,830
The important thing to consider is

294
00:13:17,830 --> 00:13:21,140
where the weapon, in this case, belongs.

295
00:13:21,140 --> 00:13:22,560
You obviously won't just be dealing

296
00:13:22,560 --> 00:13:24,240
with weapon objects in your code,

297
00:13:24,240 --> 00:13:26,250
but the principle's the same.

298
00:13:26,250 --> 00:13:29,440
In other words, does the axe belong to the player gr8,

299
00:13:29,440 --> 00:13:31,700
or is it part of the game.

300
00:13:31,700 --> 00:13:33,530
Another way to look at that is to ask,

301
00:13:33,530 --> 00:13:38,110
can the axe exist in our programme without the player, gr8?

302
00:13:38,110 --> 00:13:40,100
So I think there's like the names Tim, Louise,

303
00:13:40,100 --> 00:13:42,970
one2watch, and so on, don't have any meaning

304
00:13:42,970 --> 00:13:45,560
without the player that they're attached to.

305
00:13:45,560 --> 00:13:47,380
The axe and the sword, on the other hand,

306
00:13:47,380 --> 00:13:49,244
are part of the overall game.

307
00:13:49,244 --> 00:13:51,800
I could be forced to drop my sword at some some point,

308
00:13:51,800 --> 00:13:54,840
in which case another player might pick it up.

309
00:13:54,840 --> 00:13:56,300
So, if I drop the sword,

310
00:13:56,300 --> 00:13:58,800
let's say I pick up a spear instead.

311
00:13:58,800 --> 00:14:02,430
So, adding that to the code, tim.weapon,

312
00:14:02,430 --> 00:14:05,570
is equal to Weapon, parentheses,

313
00:14:05,570 --> 00:14:10,570
spear, comma 14, and println again, tim.weapon.name.

314
00:14:14,223 --> 00:14:15,806
So if you run this,

315
00:14:18,647 --> 00:14:19,800
so I've now got a spear,

316
00:14:19,800 --> 00:14:22,060
and there's no trace of the sword anywhere.

317
00:14:22,060 --> 00:14:24,170
So the reference to tim.weapon

318
00:14:24,170 --> 00:14:26,190
no longer refers to the sword,

319
00:14:26,190 --> 00:14:28,660
and we can't assign that to another player.

320
00:14:28,660 --> 00:14:30,280
Now, don't worry too much about this.

321
00:14:30,280 --> 00:14:32,830
It's one of the decisions we have to make

322
00:14:32,830 --> 00:14:34,530
when creating our code.

323
00:14:34,530 --> 00:14:36,160
At this stage I just want you to be aware

324
00:14:36,160 --> 00:14:38,120
of the difference and that you can have more

325
00:14:38,120 --> 00:14:40,260
than one reference to the same object.

326
00:14:40,260 --> 00:14:42,400
We'll come back to this in a later video

327
00:14:42,400 --> 00:14:43,970
when we provide a way for our players

328
00:14:43,970 --> 00:14:45,890
to drop things that they've picked up.

329
00:14:45,890 --> 00:14:47,087
So I'm gonna stop the video here,

330
00:14:47,087 --> 00:14:49,050
and in the next one, we're gonna have a challenge

331
00:14:49,050 --> 00:14:51,070
to let you practise some of these techniques.

332
00:14:51,070 --> 00:14:52,520
So see you in the next video.

