1 00:00:04,759 --> 00:00:06,589 In this video, we're going to go a bit 2 00:00:06,589 --> 00:00:08,510 deeper, and look at setting up an 3 00:00:08,510 --> 00:00:11,240 inventory within our Player class, and that's 4 00:00:11,240 --> 00:00:13,040 going to show you the concept of storing 5 00:00:13,040 --> 00:00:15,980 multiple items within a class. Now just 6 00:00:15,980 --> 00:00:17,599 to recap, we've seen how we can store 7 00:00:17,599 --> 00:00:19,820 objects in our classes, where we added a 8 00:00:19,820 --> 00:00:22,640 Weapon object to the Player class. At the 9 00:00:22,640 --> 00:00:24,169 moment, we're just storing a single 10 00:00:24,169 --> 00:00:26,869 weapon. We can change it by replacing it 11 00:00:26,869 --> 00:00:27,800 with another weapon. 12 00:00:27,800 --> 00:00:30,399 So let's actually review how that works. 13 00:00:30,399 --> 00:00:32,780 Now I've left the code that was 14 00:00:32,780 --> 00:00:34,399 run at the end of the last video, and 15 00:00:34,399 --> 00:00:36,260 towards the end you can see that it's 16 00:00:36,260 --> 00:00:38,350 showing - if we scroll up a little bit - 17 00:00:38,350 --> 00:00:40,640 firstly this bit here, showing tim at 18 00:00:40,640 --> 00:00:43,340 level: one, lives: three, weapon: Sword and 19 00:00:43,340 --> 00:00:46,700 damage: 10. Now Tim then gives the sword 20 00:00:46,700 --> 00:00:48,110 to Louise, and we can see this 21 00:00:48,110 --> 00:00:50,390 next bit of output here - the middle part - or 22 00:00:50,390 --> 00:00:51,800 you could say that Louise takes the 23 00:00:51,800 --> 00:00:53,210 sword from Tim, depending how you want to 24 00:00:53,210 --> 00:00:54,980 look at it. And we can also see the 25 00:00:54,980 --> 00:00:56,840 details for Louise, which is now 26 00:00:56,840 --> 00:00:59,180 outputting a sword. Then Tim picks up the 27 00:00:59,180 --> 00:01:00,680 spear - if we can scroll down and have a 28 00:01:00,680 --> 00:01:02,390 look - and you can see that final output. 29 00:01:02,390 --> 00:01:04,489 That's the final call to tim's.show 30 00:01:04,489 --> 00:01:06,500 function, prints the Spear with 14 31 00:01:06,500 --> 00:01:07,520 points of damage. 32 00:01:07,520 --> 00:01:10,069 So objects can contain references to 33 00:01:10,069 --> 00:01:12,499 other objects. So here, a Player object 34 00:01:12,499 --> 00:01:15,350 called tim has a field that contains a 35 00:01:15,350 --> 00:01:17,450 reference to a Weapon object that's a 36 00:01:17,450 --> 00:01:20,030 spear. So that's great for modelling the 37 00:01:20,030 --> 00:01:22,490 real world - or a fantasy world, in the 38 00:01:22,490 --> 00:01:24,499 case of an adventure game. Now you could 39 00:01:24,499 --> 00:01:26,600 have a House object, for example, that 40 00:01:26,600 --> 00:01:28,639 contains Room objects, that themselves 41 00:01:28,639 --> 00:01:31,789 have Window, Door and Wall objects. Now if 42 00:01:31,789 --> 00:01:33,409 you gave those Window, Door and Wall 43 00:01:33,409 --> 00:01:35,929 objects some properties, representing the 44 00:01:35,929 --> 00:01:37,939 materials they're made of - things 45 00:01:37,939 --> 00:01:40,700 like the thermal conductivity of the 46 00:01:40,700 --> 00:01:42,829 material - then you could model the heat 47 00:01:42,829 --> 00:01:45,049 loss of different houses. So that could 48 00:01:45,049 --> 00:01:47,170 let you design energy-efficient houses. 49 00:01:47,170 --> 00:01:49,310 Now that's great where the player has 50 00:01:49,310 --> 00:01:51,770 got one, and only one weapon, but what 51 00:01:51,770 --> 00:01:54,170 about a house? So house hasn't only got 52 00:01:54,170 --> 00:01:57,709 one wall - that's just a wall! So we 53 00:01:57,709 --> 00:01:59,450 often want to store multiple references 54 00:01:59,450 --> 00:02:01,369 to things. So we're going to give our 55 00:02:01,369 --> 00:02:03,499 players an inventory, to track things that 56 00:02:03,499 --> 00:02:05,599 they can pick up in the game; typess of 57 00:02:05,599 --> 00:02:07,759 things like potions or rings or armour, 58 00:02:07,759 --> 00:02:10,280 where there could be many items. Now 59 00:02:10,280 --> 00:02:12,110 obviously, we don't want to have to 60 00:02:12,110 --> 00:02:14,060 create one variable for each type of 61 00:02:14,060 --> 00:02:15,530 item, because we don't know in advance 62 00:02:15,530 --> 00:02:18,200 how many items the player will collect. 63 00:02:18,200 --> 00:02:20,239 So we wouldn't want to try and guess and 64 00:02:20,239 --> 00:02:21,770 create a new property for every single 65 00:02:21,770 --> 00:02:24,170 item that the player may pick up. In 66 00:02:24,170 --> 00:02:25,580 other words, we wouldn't want to do 67 00:02:25,580 --> 00:02:27,580 something like this - if we go back to the 68 00:02:27,580 --> 00:02:30,980 Player class. So we wouldn't want to do 69 00:02:30,980 --> 00:02:34,610 something along these lines; var weapon2, 70 00:02:34,610 --> 00:02:40,760 var weapon3, var weapon4 and so on. 71 00:02:40,760 --> 00:02:43,430 That could be severely limiting, as well 72 00:02:43,430 --> 00:02:45,530 as wasting memory, if they had fewer 73 00:02:45,530 --> 00:02:47,410 weapons than we allocate properties for. 74 00:02:47,410 --> 00:02:50,530 So I'll just delete that. 75 00:02:50,530 --> 00:02:53,150 What we really want, is some way of 76 00:02:53,150 --> 00:02:55,760 storing a list, that we can add things to 77 00:02:55,760 --> 00:02:58,160 or remove things from, without having to 78 00:02:58,160 --> 00:03:00,650 worry about how many items there are. Now 79 00:03:00,650 --> 00:03:02,299 we can actually do that using something 80 00:03:02,299 --> 00:03:03,440 called an ArrayList. 81 00:03:03,440 --> 00:03:06,319 So let's actually set that up. So I've 82 00:03:06,319 --> 00:03:08,209 deleted those extra weapons that I just 83 00:03:08,209 --> 00:03:10,370 typed in, so the first thing we're going 84 00:03:10,370 --> 00:03:12,640 to do, though, is create another class. 85 00:03:12,640 --> 00:03:15,170 Now this class will hold the details 86 00:03:15,170 --> 00:03:17,720 for the inventory items that we'll be 87 00:03:17,720 --> 00:03:20,569 storing in the list. Now InventoryItem 88 00:03:20,569 --> 00:03:22,670 is a good name for these things, but the 89 00:03:22,670 --> 00:03:24,739 use of the word item can get 90 00:03:24,739 --> 00:03:27,109 confusing, and that's because there are items 91 00:03:27,109 --> 00:03:29,569 in lists. Now anything the player picks 92 00:03:29,569 --> 00:03:30,920 up in the course of the game will be 93 00:03:30,920 --> 00:03:33,260 known as Loot. So that's a good name for 94 00:03:33,260 --> 00:03:35,120 this class, so let's go with that. So I'm 95 00:03:35,120 --> 00:03:37,400 going to right-click on the Java folder; 96 00:03:37,400 --> 00:03:40,280 New, select Kotlin/File Class. Select 97 00:03:40,280 --> 00:03:42,049 Class under the drop-down, although we 98 00:03:42,049 --> 00:03:43,489 could have left it as we wanted to, 99 00:03:43,489 --> 00:03:45,560 without changing that, and then just type 100 00:03:45,560 --> 00:03:48,920 Loot. Obviously, we've done that a few 101 00:03:48,920 --> 00:03:50,540 times now, so you should be 102 00:03:50,540 --> 00:03:52,639 fairly familiar with that process. Al 103 00:03:52,639 --> 00:03:54,370 right, so we've called the class Loot. Now 104 00:03:54,370 --> 00:03:56,600 this class is going to have a few 105 00:03:56,600 --> 00:03:59,150 properties - one of them will be the name. 106 00:03:59,150 --> 00:04:01,430 Each piece of loot will have a name so 107 00:04:01,430 --> 00:04:03,530 we know what it is, but let's also have a 108 00:04:03,530 --> 00:04:06,980 type. Now, the type will be used to store 109 00:04:06,980 --> 00:04:09,049 what kind of loot we've got, whether it's 110 00:04:09,049 --> 00:04:11,569 a potion or a ring or, perhaps, it's some 111 00:04:11,569 --> 00:04:13,639 armor. So we want to be able to break it 112 00:04:13,639 --> 00:04:15,709 down, so we know what kind of thing each 113 00:04:15,709 --> 00:04:18,048 and every bit of loot we've got, is. So 114 00:04:18,048 --> 00:04:19,370 what we're going to do is use something 115 00:04:19,370 --> 00:04:22,250 that we call an enum. Now an enum is a 116 00:04:22,250 --> 00:04:24,380 way of storing multiple choices for 117 00:04:24,380 --> 00:04:26,510 something. So if you think of an int, a 118 00:04:26,510 --> 00:04:28,400 property that we declare to be an int 119 00:04:28,400 --> 00:04:30,200 can have an infinite number of values. 120 00:04:30,200 --> 00:04:32,360 But if we've only got a few 121 00:04:32,360 --> 00:04:34,340 values that a property can have, then that's 122 00:04:34,340 --> 00:04:36,919 where an enum's really useful. So we're 123 00:04:36,919 --> 00:04:40,219 going to call our enum LootType. Let's 124 00:04:40,219 --> 00:04:41,449 add some code. I'm going to type this 125 00:04:41,449 --> 00:04:43,490 above the class definition. We're going 126 00:04:43,490 --> 00:04:49,849 to type enum class LootType, noting the 127 00:04:49,849 --> 00:04:51,620 capitalization of the word Loot and Type, 128 00:04:51,620 --> 00:04:54,889 then curly braces. Then within there, 129 00:04:54,889 --> 00:04:56,719 we're going to type POTION, all in 130 00:04:56,719 --> 00:05:01,490 uppercase comma RING in uppercase comma, 131 00:05:01,490 --> 00:05:05,659 then ARMOR. I'm just going to go down and put a space 132 00:05:05,659 --> 00:05:07,580 after the ending definition for the enum 133 00:05:07,580 --> 00:05:10,370 and before the class. Now the convention 134 00:05:10,370 --> 00:05:12,379 is to use all capitals for the values in 135 00:05:12,379 --> 00:05:14,900 an enum, and that way they stand out when 136 00:05:14,900 --> 00:05:16,279 you read the code and you don't spend 137 00:05:16,279 --> 00:05:18,020 time wondering where the variable called 138 00:05:18,020 --> 00:05:20,360 POTION is declared. One advantage of 139 00:05:20,360 --> 00:05:21,800 using an enum, is that you can only 140 00:05:21,800 --> 00:05:23,930 assign one of those defined values to a 141 00:05:23,930 --> 00:05:26,389 LootType field. Any loot that players 142 00:05:26,389 --> 00:05:28,129 pick up, in other words, has to either be 143 00:05:28,129 --> 00:05:31,580 a POTION, a RING or ARMOR. So that's the 144 00:05:31,580 --> 00:05:33,409 three types of loot that players may 145 00:05:33,409 --> 00:05:35,330 pick up while playing the game. Now 146 00:05:35,330 --> 00:05:36,800 obviously, you could add more, but I'm gonna 147 00:05:36,800 --> 00:05:38,889 stick with those three types for this example. 148 00:05:38,889 --> 00:05:41,120 Alright, so let's give our Loot objects 149 00:05:41,120 --> 00:05:43,400 some properties. Let's go back to the class line 150 00:05:43,400 --> 00:05:45,169 definition. We're going to add some 151 00:05:45,169 --> 00:05:48,229 parentheses, and we're going to start by 152 00:05:48,229 --> 00:05:54,289 typing val space name colon and a String colon, 153 00:05:54,289 --> 00:05:59,389 val type colon - we're calling that LootType, 154 00:05:59,389 --> 00:06:02,779 noticing I've typed it LootType. Then comma 155 00:06:02,779 --> 00:06:08,589 val value colon, Double with a capital D. 156 00:06:08,589 --> 00:06:11,000 So each piece of loot will have a name 157 00:06:11,000 --> 00:06:13,699 and a type. Now the type will be one 158 00:06:13,699 --> 00:06:16,339 of our LootTypes: POTION, RING or ARMOR. 159 00:06:16,339 --> 00:06:18,529 Now we also store a value for the loot, 160 00:06:18,529 --> 00:06:20,779 so we know what each piece is worth, and 161 00:06:20,779 --> 00:06:23,120 I'm using a Double for the value, so 162 00:06:23,120 --> 00:06:24,620 that we can work in dollars and cents, 163 00:06:24,620 --> 00:06:26,569 say. That lets us have values like 164 00:06:26,569 --> 00:06:28,939 twelve dollars fifty, for example. So 165 00:06:28,939 --> 00:06:32,150 that's our basic Loot class. Now each 166 00:06:32,150 --> 00:06:33,889 particular item that's going to be in a 167 00:06:33,889 --> 00:06:35,870 player's inventory is going to have a 168 00:06:35,870 --> 00:06:38,810 name, a type and a value assigned to it. 169 00:06:38,810 --> 00:06:40,669 So now we want to go back to the Player 170 00:06:40,669 --> 00:06:43,279 class and store our loot, somehow. So 171 00:06:43,279 --> 00:06:45,300 let's go back to the Player class. 172 00:06:45,300 --> 00:06:47,970 Now we can come down under the weapon 173 00:06:47,970 --> 00:06:50,490 definition. Now what we can't do is just 174 00:06:50,490 --> 00:06:54,290 do something like this; var loot Loot. 175 00:06:54,290 --> 00:06:57,360 The problem with that is, it's only going 176 00:06:57,360 --> 00:06:59,880 to store a single piece of loot. What we 177 00:06:59,880 --> 00:07:02,130 want is a list of some sort, and Java 178 00:07:02,130 --> 00:07:04,050 provides a few different types of Lists 179 00:07:04,050 --> 00:07:06,270 that we can use. And I know I said Java, 180 00:07:06,270 --> 00:07:07,770 rather than Kotlin there, but I'm going 181 00:07:07,770 --> 00:07:09,660 to come back to that. So here, we're going 182 00:07:09,660 --> 00:07:11,880 to use something called an ArrayList. So 183 00:07:11,880 --> 00:07:13,490 I'm going to come back and delete that, 184 00:07:13,490 --> 00:07:16,980 and make the var a Val, and we're going to call 185 00:07:16,980 --> 00:07:22,770 it inventory equals ArrayList and then 186 00:07:22,770 --> 00:07:25,350 add our diamond, and then put Loot in the 187 00:07:25,350 --> 00:07:27,630 middle of that. Then we need to add left 188 00:07:27,630 --> 00:07:29,280 and right curly braces on the end of that. 189 00:07:29,280 --> 00:07:31,020 Alright, so you can see there, that the 190 00:07:31,020 --> 00:07:32,730 declaration I've typed in looks slightly 191 00:07:32,730 --> 00:07:35,460 different to the other fields. Now the 192 00:07:35,460 --> 00:07:37,500 reason for that is because our inventory 193 00:07:37,500 --> 00:07:40,230 ArrayList will contain other objects, we 194 00:07:40,230 --> 00:07:42,510 need to tell Kotlin what kind of objects 195 00:07:42,510 --> 00:07:45,420 it'll contain. But we don't have to do 196 00:07:45,420 --> 00:07:46,830 that, in fact. The Java collections 197 00:07:46,830 --> 00:07:50,010 classes will quite happily let you 198 00:07:50,010 --> 00:07:51,930 store just about anything in them. The 199 00:07:51,930 --> 00:07:53,220 problem with that, though, is we don't 200 00:07:53,220 --> 00:07:54,420 want to allow the program to store 201 00:07:54,420 --> 00:07:56,280 Players or Weapons in this list. If we 202 00:07:56,280 --> 00:07:58,230 did that, we'd have problems when we came 203 00:07:58,230 --> 00:08:00,240 to calculate the total value of our loot. 204 00:08:00,240 --> 00:08:02,340 Now if there was a Player stored in the 205 00:08:02,340 --> 00:08:03,750 list, for example, then the program would 206 00:08:03,750 --> 00:08:05,640 crash when we tried to access the Player's 207 00:08:05,640 --> 00:08:07,860 value field. Now we're not allowing 208 00:08:07,860 --> 00:08:09,870 slavery in this game, so Players don't 209 00:08:09,870 --> 00:08:12,600 have a monetary value. By specifying Loot 210 00:08:12,600 --> 00:08:14,460 inside the angle brackets here, as you 211 00:08:14,460 --> 00:08:16,500 can say that I've done on line 7, we're 212 00:08:16,500 --> 00:08:18,630 restricting the ArrayList to only hold 213 00:08:18,630 --> 00:08:21,390 objects of type Loot, and we can't then 214 00:08:21,390 --> 00:08:24,360 store other objects in the list. Now the 215 00:08:24,360 --> 00:08:27,090 ArrayList comes from Java, or comes with 216 00:08:27,090 --> 00:08:29,220 Java, and it's in the Java.util 217 00:08:29,220 --> 00:08:31,650 package, and it takes care of storing 218 00:08:31,650 --> 00:08:33,600 things in a list for us. And that's why 219 00:08:33,600 --> 00:08:35,820 I referred to Java classes earlier. And 220 00:08:35,820 --> 00:08:37,350 we can see that by holding down the Ctrl 221 00:08:37,350 --> 00:08:39,299 key and clicking on ArrayList. I'm 222 00:08:39,299 --> 00:08:40,710 going to hold down Command because I'm 223 00:08:40,710 --> 00:08:43,620 on a Mac. Now Kotlin's using a 224 00:08:43,620 --> 00:08:45,330 typealias there, as you can see on the 225 00:08:45,330 --> 00:08:47,490 screen, so that we can refer to Array 226 00:08:47,490 --> 00:08:49,260 List instead of the full Java name of 227 00:08:49,260 --> 00:08:52,140 java.util.ArrayList. Now that 228 00:08:52,140 --> 00:08:54,510 saves typing, but we're still using the 229 00:08:54,510 --> 00:08:57,960 Java ArrayList class in our code. So 230 00:08:57,960 --> 00:08:58,740 you'll hear me 231 00:08:58,740 --> 00:09:00,779 to Java classes throughout the course, 232 00:09:00,779 --> 00:09:03,330 because Kotlin uses a lot of the Java 233 00:09:03,330 --> 00:09:05,640 classes, and actually, most of the Android 234 00:09:05,640 --> 00:09:08,100 framework's still written in Java. Now if 235 00:09:08,100 --> 00:09:09,480 you want more information on the Java 236 00:09:09,480 --> 00:09:11,370 ArrayList class, and the things you can 237 00:09:11,370 --> 00:09:12,810 do with it, there's some documentation 238 00:09:12,810 --> 00:09:15,029 that I'll briefly show you now. So I'm 239 00:09:15,029 --> 00:09:17,910 just going to load a browser - let's go and 240 00:09:17,910 --> 00:09:22,770 have a quick look. So there's our 241 00:09:22,770 --> 00:09:24,570 ArrayList. And if we scroll down and 242 00:09:24,570 --> 00:09:25,830 have a look at some of the methods, in 243 00:09:25,830 --> 00:09:27,810 the Method Summary here - they're on the 244 00:09:27,810 --> 00:09:30,180 screen - there's a method to add new items 245 00:09:30,180 --> 00:09:32,220 to the list, as you can see up there. And 246 00:09:32,220 --> 00:09:33,779 in fact, there's two add methods - the 247 00:09:33,779 --> 00:09:35,430 second one lets us add items at a 248 00:09:35,430 --> 00:09:38,160 specific position in the list. We've also 249 00:09:38,160 --> 00:09:41,070 got a clear function there, and a bit 250 00:09:41,070 --> 00:09:42,480 further down, if you scroll down, we've 251 00:09:42,480 --> 00:09:44,880 also got a remove as well. And 252 00:09:44,880 --> 00:09:47,220 there's also a get function as well, so 253 00:09:47,220 --> 00:09:49,170 that we can use that to access the 254 00:09:49,170 --> 00:09:51,330 individual items in the list, but we'll 255 00:09:51,330 --> 00:09:54,029 use that, probably, in the next video. So 256 00:09:54,029 --> 00:09:56,550 the ArrayList class provides a load of 257 00:09:56,550 --> 00:09:58,770 methods for manipulating the contents of 258 00:09:58,770 --> 00:10:00,600 the list. And we're going to be looking at a 259 00:10:00,600 --> 00:10:02,850 few of those methods next, so that we can 260 00:10:02,850 --> 00:10:05,220 get a feel for how to use them. Now 261 00:10:05,220 --> 00:10:06,839 whenever you're using the Java classes, 262 00:10:06,839 --> 00:10:09,390 or the android framework classes, that 263 00:10:09,390 --> 00:10:10,410 we're going to look at throughout the 264 00:10:10,410 --> 00:10:12,540 course, check out the documentation to 265 00:10:12,540 --> 00:10:14,670 see what you can do with them. Alright. so 266 00:10:14,670 --> 00:10:16,770 let's so go back to our code, and what 267 00:10:16,770 --> 00:10:18,720 I'll do is, I'll end the video here. In 268 00:10:18,720 --> 00:10:20,850 the next one, we'll continue on, and we'll 269 00:10:20,850 --> 00:10:22,860 start making use of this ArrayList. So 270 00:10:22,860 --> 00:10:25,910 I'll see you in the next video.