(upbeat music)
(typing)
Alright so if you're gonna class loot
as you can see to represent the various items of loot
that the players may find.
And the player class has got an inventory object
that we've added, you can see it on line 7.
That can be used to store a list
of the players' loot.
Now notice we haven't specified
how many items we can put into the inventory.
The array list class manages all the complexity for us,
we'll just add or delete loot items
and the list will take care of everything else for us.
Now in the last video we saw
that the array list class has an add function
so we can use that in our main function
and add loot to the list that way.
Now there's a couple of reasons
why we shouldn't do that
but let's start by seeing that working.
So I'm gonna go back to the main function
and after the last line,
after the Tim dot show,
I'm gonna create a new bit of loot
and give it to myself.
So let's just try that.
So I'm gonna type val red potion
is equal to loot parentheses
red potion double quotes comma
then loot type
dot potion comma and we'll assign a value of $7.50.
Then in the next line we're gonna type
Tim dot inventory dot add
parentheses red potion.
So we created a new loot type; the red potion
and then we add the object to Tim's inventory list.
I run the programme to make sure that it actually works.
(clicking)
But of course we're not gonna see anything different
because we haven't got any output yet.
So what we need to test this properly
is some way to display a player's inventory.
Now we can do that by adding a show inventory function
to the player class.
So let's have a look at doing that.
Back to the player class
and below the show function lets add another one.
Fun space show inventory,
parentheses left and right curly braces
and within there we're going to type print lend parentheses
double quote and dollar name single quote S
so it would be like Tim's inventory.
Then the next line println inventory dot get
and then in the parentheses we're gonna put zero.
Then on the next line we're gonna type
println double quotes
and just put a whole series of equal signs
just so we can break up the screen,
I put a little bit there.
Now we saw that get function in the documentation,
in the previous video so it returns the items
at the index position we specify.
Now remember that things are indexed
starting from zero in Kotlin
and in fact that's true of most programming languages.
Which is why we're using zero here on line 22.
So basically get and zero in parentheses
gives us the first item in the list.
But what if there's more than one item in the list?
That's why we're using a list after all.
Now there are two things
I need to cover at the same time here
which isn't possible.
So we're gonna come back to dealing with more
than one item after we've got this bit working.
First though, let's use our show inventory method in main
to make sure something has been added to the list.
I'll go back to main
and below the Tim dot inventory dot add line,
I'm gonna type Tim dot show inventory.
So we're gonna call that function,
so we'll just run that and we'll see what happens.
And again if you get in that situation
where for whatever reason no output is coming out
which is happening for me, you need to go up and do a,
build clean build make module
and hopefully you won't have to do this
but in case it doesn't work for you,
do that then we'll run it again.
We can see that we've got some output now.
So we've got something printed out
but it doesn't look like our red potion
but we can tell that it's got something to do with loot.
So clearly that code we added
to the player down here
for the show inventory is actually working,
it's just not giving us the results
that we're expecting.
So we need to clear this up
and get some decent output
before dealing with more than one item.
So what's actually going on here?
Why are we getting that loot at,
and that strange number on the end of it?
What's happening is Kotlin doesn't really
know how we like our loot objects to be displayed.
Now it's obvious to us
that we wanna see the name at the very least.
We may also want the type and the value displayed
but we haven't told Kotlin that.
So Kotlin is just using a default function
that it knows about
and that it uses whenever we wanna print objects.
Now unless we tell it otherwise,
the function's gonna print the class name
in that strange hexadecimal number
that you can see after the at sign.
So at and in this case that text and a small number there.
Now that strange number is called a hash code
by the way, but don't worry about it at moment.
Now it'll do the same actually
for our player objects as well.
And just to show that println (tim)
in parentheses, on that.
And I'm just going to do what I've done before,
clear clean build,
sorry make and you should find that within work for me.
You can see there the player object output,
the code that I just printed out from line 38
is doing the same thing with a different number,
as you can see.
Now I said that Kotlin uses a default function
when printing out objects.
That function's called toString.
Now the toString method
or function produces a string representation of an object
and the default representation is the class name
followed by this unique number and an at sign.
Now that's not a very useful representation
but we can override the toString function
to get it to produce something a lot more interesting.
So let's actually do that for our player class.
We go back to our player class.
We've already got this show function
but I'm just gonna copy the code
that we've used in show.
So I'm gonna take a copy of everything,
all that code in show
and in between the show
and the show inventory,
I'm going to type override
space fun space toString
and notice when I've done that
Kotlin's automatically coming up with some code for us.
I press enter there.
And it's actually added the rest
of the function definition for us.
But we're gonna change this a little bit
and we're going to delete that line
and paste in our code
which again is the same code from the show function
but I'm gonna change the first line
where it's got printlin we're going to do a return there.
We're gonna type return,
get rid of the parentheses and go to the end
and get rid of the parentheses for that as well.
So we've now overwritten the toString function.
Now the toString function returns a string
so I've just got it to return the same string
that our show method was printing.
Now when we run this programme again,
back to main,
and I'm just gonna get into the habit
now of actually running,
doing this process of cleaning and making the module.
(clicking)
You can see now that I've done that.
This time though,
we get a much better representation
of Tim printed out at the end of the output.
So let's go over what we've done here.
So in main we used println on line 38 to print out Tim.
So Kotlin's looking at Tim's class,
the player class,
to see if it's got a toString function
that can be used to display the Tim object.
Now when we ran the programme last time
there wasn't a toString function in player
because we just added that.
So Kotlin used the default toString function
that every object's got.
Now that wasn't pretty,
that's the one that gave us the hexadecimal number
so what we've done here in player is
added our own version of the toString function.
Now the key word override there in line 20
that tells Kotlin that this function is replacing
one of the default functions
that classes get automatically.
Now to throw in a bit of jargon,
classes inherit the toString function
from their base class.
Now don't worry too much about
where that default toString function came from
because we're gonna be looking at
inheritance in a future video.
Now if we wanna replace the default toString function
with our own implementation of it,
we override the function with our own version,
using that override keyword as you can see there.
So that's another bit of object oriented programming jargon.
When you overrode a function or method,
to use the object oriented programming term for it,
when you override a method,
you write your own version of the method
that does things differently.
So our toString function returns a string
that's a bit more interesting
than just the class name and that hash code.
Now when overwriting a method,
the method signature must be the same
as the method that's being override.
Now method signature means the name
of the method toString here,
any parameters that it declares inside the parentheses
and the type of value that it returns.
Now the signature for the show functions quite simple.
You can see up here on line 9.
It's called show and there's no parameters,
defined inside of the parentheses.
So it also doesn't return a value,
it just prints values out,
it doesn't return anything to the calling code.
But our toString function doesn't do any printing,
it just sends a string back to whatever calls it.
We've got the call of string here
on the declaration to indicate
that the type value it turns,
that it returns is a string.
So back in main when we println(tim) on line 38,
Kotlin's automatically calling the toString function
in a player class to get the string that should be printed.
Now we can be explicit about that
if we want and specify toStrings.
We can go back to there and put Tim dot toString,
which does the same thing,
but just makes it a bit more explicit.
That's not necessary because Kotlin calls it for us
if we don't specify it.
And if we just run the programme again,
(clicking)
you can see we're getting the very same output.
So the println function needs a string
to print and Tim isn't a string on 38,
it's a player because of that Kotlin knows it has to call
toString so that it's got a string to print
and that's what it's doing here.
Okay, so now that we've got a better toString function
for our player objects,
we don't need show to do the same thing.
So I'm gonna change to just print out
whether the player's are alive or not.
Let's go ahead and do that.
So back to player and delete that code
and within there we're gonna put,
if parentheses lives,
is greater than zero
curly braces we're gonna do println,
parentheses double quotes
dollar name, is alive
we're gonna close down the code block
and then else and another code block there
and it's gonna be println,
double quotes dollar name is dead.
So there's our revised show method.
So lets just clean, make the module
and we're going back up and try running the code.
(clicking)
You can see the output now is a lot tidier
than what it was before.
So all our players as you can see are alive
and we can get the information
on the players by just printing them out.
Alright so now you can produce more interesting output
for our loot objects by overriding the toString function
in the loot class and that sounds like a good challenge
so we'll do that in the next video.