In this video, we're going to go a bit
deeper, and look at setting up an
inventory within our Player class, and that's
going to show you the concept of storing
multiple items within a class. Now just
to recap, we've seen how we can store
objects in our classes, where we added a
Weapon object to the Player class. At the
moment, we're just storing a single
weapon. We can change it by replacing it
with another weapon.
So let's actually review how that works.
Now I've left the code that was
run at the end of the last video, and
towards the end you can see that it's
showing - if we scroll up a little bit -
firstly this bit here, showing tim at
level: one, lives: three, weapon: Sword and
damage: 10. Now Tim then gives the sword
to Louise, and we can see this
next bit of output here - the middle part - or
you could say that Louise takes the
sword from Tim, depending how you want to
look at it. And we can also see the
details for Louise, which is now
outputting a sword. Then Tim picks up the
spear - if we can scroll down and have a
look - and you can see that final output.
That's the final call to tim's.show
function, prints the Spear with 14
points of damage.
So objects can contain references to
other objects. So here, a Player object
called tim has a field that contains a
reference to a Weapon object that's a
spear. So that's great for modelling the
real world - or a fantasy world, in the
case of an adventure game. Now you could
have a House object, for example, that
contains Room objects, that themselves
have Window, Door and Wall objects. Now if
you gave those Window, Door and Wall
objects some properties, representing the
materials they're made of - things
like the thermal conductivity of the
material - then you could model the heat
loss of different houses. So that could
let you design energy-efficient houses.
Now that's great where the player has
got one, and only one weapon, but what
about a house? So house hasn't only got
one wall - that's just a wall! So we
often want to store multiple references
to things. So we're going to give our
players an inventory, to track things that
they can pick up in the game; typess of
things like potions or rings or armour,
where there could be many items. Now
obviously, we don't want to have to
create one variable for each type of
item, because we don't know in advance
how many items the player will collect.
So we wouldn't want to try and guess and
create a new property for every single
item that the player may pick up. In
other words, we wouldn't want to do
something like this - if we go back to the
Player class. So we wouldn't want to do
something along these lines; var weapon2,
var weapon3, var weapon4 and so on.
That could be severely limiting, as well
as wasting memory, if they had fewer
weapons than we allocate properties for.
So I'll just delete that.
What we really want, is some way of
storing a list, that we can add things to
or remove things from, without having to
worry about how many items there are. Now
we can actually do that using something
called an ArrayList.
So let's actually set that up. So I've
deleted those extra weapons that I just
typed in, so the first thing we're going
to do, though, is create another class.
Now this class will hold the details
for the inventory items that we'll be
storing in the list. Now InventoryItem
is a good name for these things, but the
use of the word item can get
confusing, and that's because there are items
in lists. Now anything the player picks
up in the course of the game will be
known as Loot. So that's a good name for
this class, so let's go with that. So I'm
going to right-click on the Java folder;
New, select Kotlin/File Class. Select
Class under the drop-down, although we
could have left it as we wanted to,
without changing that, and then just type
Loot. Obviously, we've done that a few
times now, so you should be
fairly familiar with that process. Al
right, so we've called the class Loot. Now
this class is going to have a few
properties - one of them will be the name.
Each piece of loot will have a name so
we know what it is, but let's also have a
type. Now, the type will be used to store
what kind of loot we've got, whether it's
a potion or a ring or, perhaps, it's some
armor. So we want to be able to break it
down, so we know what kind of thing each
and every bit of loot we've got, is. So
what we're going to do is use something
that we call an enum. Now an enum is a
way of storing multiple choices for
something. So if you think of an int, a
property that we declare to be an int
can have an infinite number of values.
But if we've only got a few
values that a property can have, then that's
where an enum's really useful. So we're
going to call our enum LootType. Let's
add some code. I'm going to type this
above the class definition. We're going
to type enum class LootType, noting the
capitalization of the word Loot and Type,
then curly braces. Then within there,
we're going to type POTION, all in
uppercase comma RING in uppercase comma,
then ARMOR. I'm just going to go down and put a space
after the ending definition for the enum
and before the class. Now the convention
is to use all capitals for the values in
an enum, and that way they stand out when
you read the code and you don't spend
time wondering where the variable called
POTION is declared. One advantage of
using an enum, is that you can only
assign one of those defined values to a
LootType field. Any loot that players
pick up, in other words, has to either be
a POTION, a RING or ARMOR. So that's the
three types of loot that players may
pick up while playing the game. Now
obviously, you could add more, but I'm gonna
stick with those three types for this example.
Alright, so let's give our Loot objects
some properties. Let's go back to the class line
definition. We're going to add some
parentheses, and we're going to start by
typing val space name colon and a String colon,
val type colon - we're calling that LootType,
noticing I've typed it LootType. Then comma
val value colon, Double with a capital D.
So each piece of loot will have a name
and a type. Now the type will be one
of our LootTypes: POTION, RING or ARMOR.
Now we also store a value for the loot,
so we know what each piece is worth, and
I'm using a Double for the value, so
that we can work in dollars and cents,
say. That lets us have values like
twelve dollars fifty, for example. So
that's our basic Loot class. Now each
particular item that's going to be in a
player's inventory is going to have a
name, a type and a value assigned to it.
So now we want to go back to the Player
class and store our loot, somehow. So
let's go back to the Player class.
Now we can come down under the weapon
definition. Now what we can't do is just
do something like this; var loot Loot.
The problem with that is, it's only going
to store a single piece of loot. What we
want is a list of some sort, and Java
provides a few different types of Lists
that we can use. And I know I said Java,
rather than Kotlin there, but I'm going
to come back to that. So here, we're going
to use something called an ArrayList. So
I'm going to come back and delete that,
and make the var a Val, and we're going to call
it inventory equals ArrayList and then
add our diamond, and then put Loot in the
middle of that. Then we need to add left
and right curly braces on the end of that.
Alright, so you can see there, that the
declaration I've typed in looks slightly
different to the other fields. Now the
reason for that is because our inventory
ArrayList will contain other objects, we
need to tell Kotlin what kind of objects
it'll contain. But we don't have to do
that, in fact. The Java collections
classes will quite happily let you
store just about anything in them. The
problem with that, though, is we don't
want to allow the program to store
Players or Weapons in this list. If we
did that, we'd have problems when we came
to calculate the total value of our loot.
Now if there was a Player stored in the
list, for example, then the program would
crash when we tried to access the Player's
value field. Now we're not allowing
slavery in this game, so Players don't
have a monetary value. By specifying Loot
inside the angle brackets here, as you
can say that I've done on line 7, we're
restricting the ArrayList to only hold
objects of type Loot, and we can't then
store other objects in the list. Now the
ArrayList comes from Java, or comes with
Java, and it's in the Java.util
package, and it takes care of storing
things in a list for us. And that's why
I referred to Java classes earlier. And
we can see that by holding down the Ctrl
key and clicking on ArrayList. I'm
going to hold down Command because I'm
on a Mac. Now Kotlin's using a
typealias there, as you can see on the
screen, so that we can refer to Array
List instead of the full Java name of
java.util.ArrayList. Now that
saves typing, but we're still using the
Java ArrayList class in our code. So
you'll hear me
to Java classes throughout the course,
because Kotlin uses a lot of the Java
classes, and actually, most of the Android
framework's still written in Java. Now if
you want more information on the Java
ArrayList class, and the things you can
do with it, there's some documentation
that I'll briefly show you now. So I'm
just going to load a browser - let's go and
have a quick look. So there's our
ArrayList. And if we scroll down and
have a look at some of the methods, in
the Method Summary here - they're on the
screen - there's a method to add new items
to the list, as you can see up there. And
in fact, there's two add methods - the
second one lets us add items at a
specific position in the list. We've also
got a clear function there, and a bit
further down, if you scroll down, we've
also got a remove as well. And
there's also a get function as well, so
that we can use that to access the
individual items in the list, but we'll
use that, probably, in the next video. So
the ArrayList class provides a load of
methods for manipulating the contents of
the list. And we're going to be looking at a
few of those methods next, so that we can
get a feel for how to use them. Now
whenever you're using the Java classes,
or the android framework classes, that
we're going to look at throughout the
course, check out the documentation to
see what you can do with them. Alright. so
let's so go back to our code, and what
I'll do is, I'll end the video here. In
the next one, we'll continue on, and we'll
start making use of this ArrayList. So
I'll see you in the next video.