1
00:00:00,000 --> 00:00:02,405
(upbeat music)

2
00:00:02,405 --> 00:00:04,488
(typing)

3
00:00:05,380 --> 00:00:07,580
Alright so if you're gonna class loot

4
00:00:07,580 --> 00:00:10,880
as you can see to represent the various items of loot

5
00:00:10,880 --> 00:00:12,650
that the players may find.

6
00:00:12,650 --> 00:00:15,960
And the player class has got an inventory object

7
00:00:15,960 --> 00:00:18,190
that we've added, you can see it on line 7.

8
00:00:18,190 --> 00:00:19,380
That can be used to store a list

9
00:00:19,380 --> 00:00:21,040
of the players' loot.

10
00:00:21,040 --> 00:00:22,790
Now notice we haven't specified

11
00:00:22,790 --> 00:00:25,550
how many items we can put into the inventory.

12
00:00:25,550 --> 00:00:29,120
The array list class manages all the complexity for us,

13
00:00:29,120 --> 00:00:30,840
we'll just add or delete loot items

14
00:00:30,840 --> 00:00:33,830
and the list will take care of everything else for us.

15
00:00:33,830 --> 00:00:35,040
Now in the last video we saw

16
00:00:35,040 --> 00:00:37,870
that the array list class has an add function

17
00:00:37,870 --> 00:00:39,630
so we can use that in our main function

18
00:00:39,630 --> 00:00:42,170
and add loot to the list that way.

19
00:00:42,170 --> 00:00:43,150
Now there's a couple of reasons

20
00:00:43,150 --> 00:00:44,360
why we shouldn't do that

21
00:00:44,360 --> 00:00:46,610
but let's start by seeing that working.

22
00:00:46,610 --> 00:00:49,430
So I'm gonna go back to the main function

23
00:00:50,530 --> 00:00:51,740
and after the last line,

24
00:00:51,740 --> 00:00:53,350
after the Tim dot show,

25
00:00:53,350 --> 00:00:54,500
I'm gonna create a new bit of loot

26
00:00:54,500 --> 00:00:55,540
and give it to myself.

27
00:00:55,540 --> 00:00:56,570
So let's just try that.

28
00:00:56,570 --> 00:01:00,280
So I'm gonna type val red potion

29
00:01:00,280 --> 00:01:03,090
is equal to loot parentheses

30
00:01:04,390 --> 00:01:08,090
red potion double quotes comma

31
00:01:08,090 --> 00:01:09,420
then loot type

32
00:01:10,570 --> 00:01:15,570
dot potion comma and we'll assign a value of $7.50.

33
00:01:17,410 --> 00:01:18,670
Then in the next line we're gonna type

34
00:01:18,670 --> 00:01:23,450
Tim dot inventory dot add

35
00:01:23,450 --> 00:01:25,503
parentheses red potion.

36
00:01:26,590 --> 00:01:29,460
So we created a new loot type; the red potion

37
00:01:29,460 --> 00:01:32,800
and then we add the object to Tim's inventory list.

38
00:01:32,800 --> 00:01:35,710
I run the programme to make sure that it actually works.

39
00:01:35,710 --> 00:01:37,960
(clicking)

40
00:01:40,610 --> 00:01:42,070
But of course we're not gonna see anything different

41
00:01:42,070 --> 00:01:44,250
because we haven't got any output yet.

42
00:01:44,250 --> 00:01:46,070
So what we need to test this properly

43
00:01:46,070 --> 00:01:48,810
is some way to display a player's inventory.

44
00:01:48,810 --> 00:01:52,840
Now we can do that by adding a show inventory function

45
00:01:52,840 --> 00:01:53,810
to the player class.

46
00:01:53,810 --> 00:01:55,090
So let's have a look at doing that.

47
00:01:55,090 --> 00:01:57,060
Back to the player class

48
00:01:57,060 --> 00:02:00,060
and below the show function lets add another one.

49
00:02:00,060 --> 00:02:02,973
Fun space show inventory,

50
00:02:04,150 --> 00:02:08,060
parentheses left and right curly braces

51
00:02:08,060 --> 00:02:11,860
and within there we're going to type print lend parentheses

52
00:02:12,750 --> 00:02:16,750
double quote and dollar name single quote S

53
00:02:16,750 --> 00:02:18,673
so it would be like Tim's inventory.

54
00:02:20,519 --> 00:02:24,730
Then the next line println inventory dot get

55
00:02:26,607 --> 00:02:29,560
and then in the parentheses we're gonna put zero.

56
00:02:29,560 --> 00:02:30,880
Then on the next line we're gonna type

57
00:02:30,880 --> 00:02:32,950
println double quotes

58
00:02:32,950 --> 00:02:35,570
and just put a whole series of equal signs

59
00:02:35,570 --> 00:02:36,810
just so we can break up the screen,

60
00:02:36,810 --> 00:02:38,230
I put a little bit there.

61
00:02:38,230 --> 00:02:41,070
Now we saw that get function in the documentation,

62
00:02:41,070 --> 00:02:44,350
in the previous video so it returns the items

63
00:02:44,350 --> 00:02:46,850
at the index position we specify.

64
00:02:46,850 --> 00:02:48,340
Now remember that things are indexed

65
00:02:48,340 --> 00:02:50,012
starting from zero in Kotlin

66
00:02:50,012 --> 00:02:52,550
and in fact that's true of most programming languages.

67
00:02:52,550 --> 00:02:55,490
Which is why we're using zero here on line 22.

68
00:02:55,490 --> 00:02:58,760
So basically get and zero in parentheses

69
00:02:58,760 --> 00:03:01,460
gives us the first item in the list.

70
00:03:01,460 --> 00:03:04,770
But what if there's more than one item in the list?

71
00:03:04,770 --> 00:03:06,750
That's why we're using a list after all.

72
00:03:06,750 --> 00:03:07,843
Now there are two things

73
00:03:07,843 --> 00:03:09,720
I need to cover at the same time here

74
00:03:09,720 --> 00:03:11,160
which isn't possible.

75
00:03:11,160 --> 00:03:12,550
So we're gonna come back to dealing with more

76
00:03:12,550 --> 00:03:15,680
than one item after we've got this bit working.

77
00:03:15,680 --> 00:03:18,430
First though, let's use our show inventory method in main

78
00:03:18,430 --> 00:03:21,520
to make sure something has been added to the list.

79
00:03:21,520 --> 00:03:22,850
I'll go back to main

80
00:03:22,850 --> 00:03:25,750
and below the Tim dot inventory dot add line,

81
00:03:25,750 --> 00:03:28,933
I'm gonna type Tim dot show inventory.

82
00:03:30,290 --> 00:03:31,750
So we're gonna call that function,

83
00:03:31,750 --> 00:03:34,250
so we'll just run that and we'll see what happens.

84
00:03:35,920 --> 00:03:37,330
And again if you get in that situation

85
00:03:37,330 --> 00:03:39,410
where for whatever reason no output is coming out

86
00:03:39,410 --> 00:03:42,973
which is happening for me, you need to go up and do a,

87
00:03:44,040 --> 00:03:47,740
build clean build make module

88
00:03:47,740 --> 00:03:49,280
and hopefully you won't have to do this

89
00:03:49,280 --> 00:03:50,710
but in case it doesn't work for you,

90
00:03:50,710 --> 00:03:52,310
do that then we'll run it again.

91
00:03:53,740 --> 00:03:56,330
We can see that we've got some output now.

92
00:03:56,330 --> 00:03:58,410
So we've got something printed out

93
00:03:58,410 --> 00:04:00,170
but it doesn't look like our red potion

94
00:04:00,170 --> 00:04:03,160
but we can tell that it's got something to do with loot.

95
00:04:03,160 --> 00:04:05,610
So clearly that code we added

96
00:04:05,610 --> 00:04:08,100
to the player down here

97
00:04:08,100 --> 00:04:10,490
for the show inventory is actually working,

98
00:04:10,490 --> 00:04:11,680
it's just not giving us the results

99
00:04:11,680 --> 00:04:13,010
that we're expecting.

100
00:04:13,010 --> 00:04:13,980
So we need to clear this up

101
00:04:13,980 --> 00:04:15,130
and get some decent output

102
00:04:15,130 --> 00:04:17,279
before dealing with more than one item.

103
00:04:17,279 --> 00:04:18,410
So what's actually going on here?

104
00:04:18,410 --> 00:04:19,950
Why are we getting that loot at,

105
00:04:19,950 --> 00:04:23,010
and that strange number on the end of it?

106
00:04:23,010 --> 00:04:24,600
What's happening is Kotlin doesn't really

107
00:04:24,600 --> 00:04:27,670
know how we like our loot objects to be displayed.

108
00:04:27,670 --> 00:04:28,800
Now it's obvious to us

109
00:04:28,800 --> 00:04:31,060
that we wanna see the name at the very least.

110
00:04:31,060 --> 00:04:34,170
We may also want the type and the value displayed

111
00:04:34,170 --> 00:04:36,160
but we haven't told Kotlin that.

112
00:04:36,160 --> 00:04:37,493
So Kotlin is just using a default function

113
00:04:37,493 --> 00:04:38,787
that it knows about

114
00:04:38,787 --> 00:04:41,760
and that it uses whenever we wanna print objects.

115
00:04:41,760 --> 00:04:43,690
Now unless we tell it otherwise,

116
00:04:43,690 --> 00:04:45,660
the function's gonna print the class name

117
00:04:45,660 --> 00:04:47,500
in that strange hexadecimal number

118
00:04:47,500 --> 00:04:48,810
that you can see after the at sign.

119
00:04:48,810 --> 00:04:51,710
So at and in this case that text and a small number there.

120
00:04:52,550 --> 00:04:54,450
Now that strange number is called a hash code

121
00:04:54,450 --> 00:04:57,400
by the way, but don't worry about it at moment.

122
00:04:57,400 --> 00:04:58,650
Now it'll do the same actually

123
00:04:58,650 --> 00:05:00,684
for our player objects as well.

124
00:05:00,684 --> 00:05:05,430
And just to show that println (tim)

125
00:05:05,430 --> 00:05:08,563
in parentheses, on that.

126
00:05:12,030 --> 00:05:13,750
And I'm just going to do what I've done before,

127
00:05:13,750 --> 00:05:16,373
clear clean build,

128
00:05:17,550 --> 00:05:20,393
sorry make and you should find that within work for me.

129
00:05:22,030 --> 00:05:23,710
You can see there the player object output,

130
00:05:23,710 --> 00:05:25,960
the code that I just printed out from line 38

131
00:05:25,960 --> 00:05:28,320
is doing the same thing with a different number,

132
00:05:28,320 --> 00:05:29,153
as you can see.

133
00:05:30,160 --> 00:05:32,390
Now I said that Kotlin uses a default function

134
00:05:32,390 --> 00:05:33,800
when printing out objects.

135
00:05:33,800 --> 00:05:36,170
That function's called toString.

136
00:05:36,170 --> 00:05:37,900
Now the toString method

137
00:05:37,900 --> 00:05:41,450
or function produces a string representation of an object

138
00:05:41,450 --> 00:05:43,890
and the default representation is the class name

139
00:05:43,890 --> 00:05:47,120
followed by this unique number and an at sign.

140
00:05:47,120 --> 00:05:49,740
Now that's not a very useful representation

141
00:05:49,740 --> 00:05:52,090
but we can override the toString function

142
00:05:52,090 --> 00:05:55,400
to get it to produce something a lot more interesting.

143
00:05:55,400 --> 00:05:57,630
So let's actually do that for our player class.

144
00:05:57,630 --> 00:06:00,010
We go back to our player class.

145
00:06:00,010 --> 00:06:01,930
We've already got this show function

146
00:06:01,930 --> 00:06:03,100
but I'm just gonna copy the code

147
00:06:03,100 --> 00:06:04,000
that we've used in show.

148
00:06:04,000 --> 00:06:05,950
So I'm gonna take a copy of everything,

149
00:06:08,010 --> 00:06:09,870
all that code in show

150
00:06:09,870 --> 00:06:11,060
and in between the show

151
00:06:11,060 --> 00:06:12,530
and the show inventory,

152
00:06:12,530 --> 00:06:14,700
I'm going to type override

153
00:06:16,920 --> 00:06:18,630
space fun space toString

154
00:06:19,986 --> 00:06:21,360
and notice when I've done that

155
00:06:21,360 --> 00:06:24,130
Kotlin's automatically coming up with some code for us.

156
00:06:24,130 --> 00:06:25,130
I press enter there.

157
00:06:26,426 --> 00:06:27,530
And it's actually added the rest

158
00:06:27,530 --> 00:06:29,690
of the function definition for us.

159
00:06:29,690 --> 00:06:31,250
But we're gonna change this a little bit

160
00:06:31,250 --> 00:06:33,570
and we're going to delete that line

161
00:06:33,570 --> 00:06:34,710
and paste in our code

162
00:06:35,550 --> 00:06:38,120
which again is the same code from the show function

163
00:06:38,120 --> 00:06:40,040
but I'm gonna change the first line

164
00:06:40,040 --> 00:06:42,930
where it's got printlin we're going to do a return there.

165
00:06:42,930 --> 00:06:44,130
We're gonna type return,

166
00:06:46,410 --> 00:06:50,650
get rid of the parentheses and go to the end

167
00:06:50,650 --> 00:06:52,760
and get rid of the parentheses for that as well.

168
00:06:52,760 --> 00:06:56,010
So we've now overwritten the toString function.

169
00:06:56,010 --> 00:06:59,280
Now the toString function returns a string

170
00:06:59,280 --> 00:07:01,190
so I've just got it to return the same string

171
00:07:01,190 --> 00:07:03,390
that our show method was printing.

172
00:07:03,390 --> 00:07:05,490
Now when we run this programme again,

173
00:07:05,490 --> 00:07:06,323
back to main,

174
00:07:10,120 --> 00:07:11,690
and I'm just gonna get into the habit

175
00:07:11,690 --> 00:07:12,610
now of actually running,

176
00:07:12,610 --> 00:07:16,284
doing this process of cleaning and making the module.

177
00:07:16,284 --> 00:07:18,534
(clicking)

178
00:07:21,600 --> 00:07:22,540
You can see now that I've done that.

179
00:07:22,540 --> 00:07:24,000
This time though,

180
00:07:24,000 --> 00:07:25,510
we get a much better representation

181
00:07:25,510 --> 00:07:28,070
of Tim printed out at the end of the output.

182
00:07:28,070 --> 00:07:29,730
So let's go over what we've done here.

183
00:07:29,730 --> 00:07:34,730
So in main we used println on line 38 to print out Tim.

184
00:07:35,210 --> 00:07:37,410
So Kotlin's looking at Tim's class,

185
00:07:37,410 --> 00:07:39,400
the player class,

186
00:07:39,400 --> 00:07:41,500
to see if it's got a toString function

187
00:07:41,500 --> 00:07:44,420
that can be used to display the Tim object.

188
00:07:44,420 --> 00:07:46,230
Now when we ran the programme last time

189
00:07:46,230 --> 00:07:48,260
there wasn't a toString function in player

190
00:07:48,260 --> 00:07:49,370
because we just added that.

191
00:07:49,370 --> 00:07:52,060
So Kotlin used the default toString function

192
00:07:52,060 --> 00:07:53,591
that every object's got.

193
00:07:53,591 --> 00:07:55,060
Now that wasn't pretty,

194
00:07:55,060 --> 00:07:57,230
that's the one that gave us the hexadecimal number

195
00:07:57,230 --> 00:07:59,290
so what we've done here in player is

196
00:07:59,290 --> 00:08:02,240
added our own version of the toString function.

197
00:08:02,240 --> 00:08:04,940
Now the key word override there in line 20

198
00:08:06,150 --> 00:08:09,160
that tells Kotlin that this function is replacing

199
00:08:09,160 --> 00:08:10,380
one of the default functions

200
00:08:10,380 --> 00:08:12,810
that classes get automatically.

201
00:08:12,810 --> 00:08:14,240
Now to throw in a bit of jargon,

202
00:08:14,240 --> 00:08:16,970
classes inherit the toString function

203
00:08:16,970 --> 00:08:18,820
from their base class.

204
00:08:18,820 --> 00:08:19,930
Now don't worry too much about

205
00:08:19,930 --> 00:08:22,580
where that default toString function came from

206
00:08:22,580 --> 00:08:23,430
because we're gonna be looking at

207
00:08:23,430 --> 00:08:25,133
inheritance in a future video.

208
00:08:26,360 --> 00:08:28,640
Now if we wanna replace the default toString function

209
00:08:28,640 --> 00:08:30,560
with our own implementation of it,

210
00:08:30,560 --> 00:08:32,980
we override the function with our own version,

211
00:08:32,980 --> 00:08:36,100
using that override keyword as you can see there.

212
00:08:36,100 --> 00:08:39,730
So that's another bit of object oriented programming jargon.

213
00:08:39,730 --> 00:08:42,390
When you overrode a function or method,

214
00:08:42,390 --> 00:08:45,430
to use the object oriented programming term for it,

215
00:08:45,430 --> 00:08:47,320
when you override a method,

216
00:08:47,320 --> 00:08:49,140
you write your own version of the method

217
00:08:49,140 --> 00:08:50,870
that does things differently.

218
00:08:50,870 --> 00:08:52,930
So our toString function returns a string

219
00:08:52,930 --> 00:08:54,500
that's a bit more interesting

220
00:08:54,500 --> 00:08:57,090
than just the class name and that hash code.

221
00:08:57,090 --> 00:08:58,830
Now when overwriting a method,

222
00:08:58,830 --> 00:09:00,590
the method signature must be the same

223
00:09:00,590 --> 00:09:02,980
as the method that's being override.

224
00:09:02,980 --> 00:09:05,140
Now method signature means the name

225
00:09:05,140 --> 00:09:07,099
of the method toString here,

226
00:09:07,099 --> 00:09:10,550
any parameters that it declares inside the parentheses

227
00:09:10,550 --> 00:09:13,210
and the type of value that it returns.

228
00:09:13,210 --> 00:09:16,380
Now the signature for the show functions quite simple.

229
00:09:16,380 --> 00:09:18,320
You can see up here on line 9.

230
00:09:18,320 --> 00:09:20,150
It's called show and there's no parameters,

231
00:09:20,150 --> 00:09:22,580
defined inside of the parentheses.

232
00:09:22,580 --> 00:09:23,990
So it also doesn't return a value,

233
00:09:23,990 --> 00:09:25,960
it just prints values out,

234
00:09:25,960 --> 00:09:28,600
it doesn't return anything to the calling code.

235
00:09:28,600 --> 00:09:31,160
But our toString function doesn't do any printing,

236
00:09:31,160 --> 00:09:34,030
it just sends a string back to whatever calls it.

237
00:09:34,030 --> 00:09:37,130
We've got the call of string here

238
00:09:37,130 --> 00:09:39,070
on the declaration to indicate

239
00:09:39,070 --> 00:09:40,620
that the type value it turns,

240
00:09:40,620 --> 00:09:42,650
that it returns is a string.

241
00:09:42,650 --> 00:09:45,050
So back in main when we println(tim) on line 38,

242
00:09:47,100 --> 00:09:51,500
Kotlin's automatically calling the toString function

243
00:09:51,500 --> 00:09:54,760
in a player class to get the string that should be printed.

244
00:09:54,760 --> 00:09:56,230
Now we can be explicit about that

245
00:09:56,230 --> 00:09:57,640
if we want and specify toStrings.

246
00:09:57,640 --> 00:10:02,313
We can go back to there and put Tim dot toString,

247
00:10:04,780 --> 00:10:05,627
which does the same thing,

248
00:10:05,627 --> 00:10:08,040
but just makes it a bit more explicit.

249
00:10:08,040 --> 00:10:10,630
That's not necessary because Kotlin calls it for us

250
00:10:10,630 --> 00:10:12,080
if we don't specify it.

251
00:10:12,080 --> 00:10:14,534
And if we just run the programme again,

252
00:10:14,534 --> 00:10:16,784
(clicking)

253
00:10:20,980 --> 00:10:23,040
you can see we're getting the very same output.

254
00:10:23,040 --> 00:10:24,900
So the println function needs a string

255
00:10:24,900 --> 00:10:29,130
to print and Tim isn't a string on 38,

256
00:10:29,130 --> 00:10:32,140
it's a player because of that Kotlin knows it has to call

257
00:10:32,140 --> 00:10:34,730
toString so that it's got a string to print

258
00:10:34,730 --> 00:10:36,300
and that's what it's doing here.

259
00:10:36,300 --> 00:10:38,540
Okay, so now that we've got a better toString function

260
00:10:38,540 --> 00:10:39,950
for our player objects,

261
00:10:39,950 --> 00:10:42,460
we don't need show to do the same thing.

262
00:10:42,460 --> 00:10:44,700
So I'm gonna change to just print out

263
00:10:44,700 --> 00:10:46,950
whether the player's are alive or not.

264
00:10:46,950 --> 00:10:47,890
Let's go ahead and do that.

265
00:10:47,890 --> 00:10:51,337
So back to player and delete that code

266
00:10:51,337 --> 00:10:54,840
and within there we're gonna put,

267
00:10:54,840 --> 00:10:59,383
if parentheses lives,

268
00:11:00,370 --> 00:11:01,430
is greater than zero

269
00:11:03,880 --> 00:11:05,690
curly braces we're gonna do println,

270
00:11:06,830 --> 00:11:09,030
parentheses double quotes

271
00:11:10,470 --> 00:11:13,910
dollar name, is alive

272
00:11:15,380 --> 00:11:16,507
we're gonna close down the code block

273
00:11:16,507 --> 00:11:19,653
and then else and another code block there

274
00:11:19,653 --> 00:11:20,486
and it's gonna be println,

275
00:11:22,540 --> 00:11:25,713
double quotes dollar name is dead.

276
00:11:27,420 --> 00:11:29,590
So there's our revised show method.

277
00:11:29,590 --> 00:11:33,250
So lets just clean, make the module

278
00:11:33,250 --> 00:11:35,856
and we're going back up and try running the code.

279
00:11:35,856 --> 00:11:38,106
(clicking)

280
00:11:40,630 --> 00:11:41,740
You can see the output now is a lot tidier

281
00:11:41,740 --> 00:11:43,310
than what it was before.

282
00:11:43,310 --> 00:11:45,570
So all our players as you can see are alive

283
00:11:45,570 --> 00:11:46,530
and we can get the information

284
00:11:46,530 --> 00:11:49,020
on the players by just printing them out.

285
00:11:49,020 --> 00:11:51,220
Alright so now you can produce more interesting output

286
00:11:51,220 --> 00:11:54,630
for our loot objects by overriding the toString function

287
00:11:54,630 --> 00:11:57,750
in the loot class and that sounds like a good challenge

288
00:11:57,750 --> 00:11:59,653
so we'll do that in the next video.

