(upbeat music)
When we started looking at classes,
I mentioned that there's less need to overload
functions in Kotlin.
And that's because Kotlin allows default values
to be specified for function arguments.
Now, I didn't say very much about overloading at the time,
so let's look at what that is in a bit more detail.
Now, when you overload a function,
that just means creating another copy of it
with different parameters.
So that could be very useful here
because we could use a different version of drop loot
and give it the name of the loot.
So, we could come down here,
at the moment we've got on line 51,
the definition for blue potion,
but in terms of drop loot, we could do something like,
tim.droploot
and actually enter the name.
So, invisibility potion.
Now, we've got an error at the moment
because we're passing a string to the drop loot function
and it expects a loot object.
Now, if we could pass a string to drop loot,
then we could drop the invisibility potion,
even though we don't have a reference to it.
So what we need to do here,
is create a version of drop loot
that accepts the string argument,
then get it to remove the corresponding item from the list.
So let's go to our player class,
we're gonna add this after the existing drop loot method.
Alright, so we go to start, and drop loot,
parentheses
name,
colon string,
colon, boolean.
Then we create the function with
the left and right curly braces.
And we'll just do println,
$name
will be dropped.
For now, we're just gonna do return
true.
So when overloading a function,
we create another function with the same name
but we give it different parameters.
Now, the compiler checks the parameters,
and calls the version of the overloaded function
that has matching parameters.
So, we've got two versions of drop loot now,
as you can see.
So this first one, on line 32,
takes an argument of top loot
because we've defined the parameter to be loot.
Now, the second drop loot, on line 41,
takes a string argument.
Now, it doesn't do anything useful yet,
we're focusing on the overloading mechanism at the moment.
We'll actually add more code to the second version
in a minute.
So go back to main now.
Notice that the error disappeared.
And the first time we call our drop loot,
this code here on line 45,
actually this is probably a better one here,
so let's do this one on line 52.
So, we're calling drop loot there and we're
and we're giving it a loot instance, blue potion,
and by doing that, the first drop loot function's called.
But down here, when we call it with a string argument,
invisibility potion, in line 58,
then that's the second drop loot function
that's gonna be called instead,
because we're past the argument of top string.
So let's actually try running this and see what happens.
And you can see that the other codes executed,
then right down the bottom,
invisibility potion will be dropped.
So that's working in our second version of drop loots,
obviously being called at that point.
So that's function overloading,
you create another version of the function
with different parameters
and Kotlin calls the one that matches the argument types.
Now, there's less need to overload functions in Kotlin
than there is in Java,
because again we can use default values
for the parameters instead.
But it's still necessary to overload functions sometimes
and this is one example.
Alright so we're gonna finish this video up
by making our overloaded drop loot function work.
Now, the Kotlin collection classes have a removed
IF function,
that we can use to remove an item from the list
if a certain condition's true.
So let's actually try that.
We'll go back to player,
then player class,
and our second drop loot function,
we're gonna do return, we're gonna delete true
and instead we're gonna return
inventory.removeIf
and we're gonna delete the parentheses
and we're gonna add a code block instead.
And within there we're gonna put
it.name == name
Two equals signs there.
So that checks each item in the list
and removes it if the name is the same
as the name we passed into drop loot.
Now, it here, where the it.
well, that refers to the item
that's currently being checked.
So if the name of the current item
matches invisibility potion in our example,
then the item gets removed from the list.
Remove if then moves on to the next item on the list
and repeats the check.
Now, if any items are removed, it returns true,
otherwise it returns false.
Which is what we want the function to do,
so we can return whatever remove if returns.
So let's see it working.
What I'm gonna do is print out Tim's inventory
at the end of main,
so we can check that the invisibility potion
has been removed.
So we'll go back to our main,
making sure that I've actually spelled it the same here,
it's being added as invisibility potion
and what is being removed is invisibility potion.
And I'll type tim.showInventory
and let's see how that goes if we run it.
And you can see Tim's inventory down there and here
and invisibility potion,
which was there on the previous call,
has now been removed.
And we've only got the chest armour
and the ring of protection left.
So that's working fine.
Now, just in case you're wondering,
we don't have to use the return values
when calling a function.
So, we could have done the same thing as we did before,
for the blue potion.
So we could have done something like this:
if
and surround that in parentheses,
add a code block and then put tim.showInventory,
and else, your println,
"you don't have an invisibility potion."
So if you do that instead,
and I'll delete that call to show inventory method.
So let's run that.
And you can see we get the same result in this case,
with only the two items left.
Alright, so I think we got to the end of this video.
Now, that's probably quite a bit to digest,
so go back and watch the last few videos a few more times
if you need to,
and go through step by step.
Now, it'll make more sense
the more times that you go through it.
And if you watch this and you aren't following along,
I do suggest that you go back and actually type this in
as I'm typing it in.
What I found when I was learning to programme
was that typing the code in,
rather than just reading it,
really helped to consolidate things.
Back then of course,
there wasn't really much in the way of video tutorials.
What we did have was a whole load of computers released
at around the same time.
Such as Sinclair ZX 81, Commodore PET,
BBC computer made by Acorn,
and a whole lot of other machines.
And there was also monthly magazines
and we learned a lot from reading the manuals
but also from typing in the code
that was published in those magazines.
Now obviously you'll make errors when typing the code in
and debugging the programmes to find the errors
and make them work, was also a great way to learn.
So, typing it in seems to solidify the information
and make it a lot easier to understand.
So I certainly recommend that you don't just
watch these videos,
but you also act and be typing this in
as the video progresses.
And it addition, also try modifying the code
to try different things.
Start off simple things, such as,
perhaps adding an extra field to one of the classes,
then by making other improvements.
You'll almost certainly get errors
but sorting out the errors is a great way
to increase your understanding of how it all works.
You can't do any damage.
The worse that will happen is you get an error message.
Now that wasn't always the case, by the way.
It was possible to speed up the screen
on an early computer called the Commodore PET,
by writing certain values to specific memory locations,
and if you got that wrong,
the display would be permanently damaged.
But that's no longer the case
and you literally can't do that these days
with modern computers.
Basically you can't do any damage to your computer
with Kotlin, so have fun and try things out.
Alright, so I've hoped you enjoyed this
and I will see you in the next video.