In this lecture, we're implementing our game sessions so that we can keep track of how many lives the
player has.
But and when we drop into some spikes, drops down to one.
If we go all the way down to zero lives, then it will reset us back to the very first level.
Let's look at what we're trying to achieve here with our goal.
I want to have the player have a certain number of lives, probably three for me.
I want to have the scene restart when the player dies so that you go back to the start of the scene
or start the level and try to do it all over again.
And when all the players lives are lost, I want the game to be over and reset all the way back to the
first screen, or if we've got a main menu or the very first in our build index, most likely.
So that's our goal.
There's a problem.
However, after death, reloading the scene re instantiating the player and all other objects.
So it loads everything again.
And any progress we have with our player or our scene is going to be lost.
So we don't want to have that if the player has a certain score, if we've picked up a certain number
of coins, if the player has a certain number of lives, then we want to retain that.
We don't want to have all that loss when we restart the scene.
So what I'm going to create is a game session object which will be responsible for all these things.
It'll be game session.
CSS is our script and it will be responsible for our lives and our dying, for our score, for level
progress.
So what level we're on, as well as restarting the game when we have run out of lives and anything else
that we want to persist throughout our game session.
So let's scootch back over into Unity.
I'm going to create myself a new game object.
I'll do this within level one and then prefab it and pop it into the other levels as we go.
So right click Create Empty.
I will call this game session, game session and I'll reset that back to the zeros.
Now in my scripts folder, I'm going to right click create a C sharp script.
I will call also game session and clicking on game session.
I will drag game session onto game session.
How many times can we say game session and what we're here for?
A bit of fun.
I'm going to click on my Prefabs folder and drag the game session down into the Prefabs folder.
So we've started that particular process or double click on our game session script.
Open that up.
I'm going to get rid of the bits and pieces.
We don't need a bit of tidy up here.
Now the first I'm going to do, instead of using Start, I'm going to use a wake.
So if you remember our unity order of execution, awake is the very first thing that is going to happen
when we push, play, push our play button.
So within a week, what I want to do is find out how many of the game sessions we currently have.
What I'm doing here, a little bit of a spoiler.
I'm going to implement a singleton pattern.
And the singleton pattern is a way for us to say we only want to have one particular thing in a single
a single thing.
And when we click on play, we'll have this one game session object.
We only want to have one because if we then reset the level or restart the level, we don't want to
have a new one appear.
We only want to have one of these.
So right at the very start, we're going to find out how many game sessions we have.
I'll store this in an integer, which I'll call NUM as a number of game sessions, right equals.
And this will be find objects of type with an s.
I sometimes get this messed up, so I'm going to make a really big deal out of the SFU so you don't
mess up as well.
Objects of type.
This is going to create an array because there's many things.
There's possibly more than one thing find objective type finds the first thing, but objects of type
finds all the things I'm going to be looking for game session and that goes in our angle brackets and
then parentheses.
Dot we want to know how many game sessions we have because this is an array.
Can you have a think of how we would go about finding how many items are in the array?
We use length that will tell us how many things are in the array.
Now we need a little if statement in here if our number of game sessions is greater than one, so we've
got more than one.
What do we want to do?
Well, we destroy whatever appears.
We destroy the game, object, semicolon.
And so a wake is going to be called whenever this class wakes up or comes alive or is brought to life.
The first time that happens is when we push the play button.
This game session with its script on it will blink, come to life.
It also happens when we die and we restart this scene so that we freshly go back to the start of it.
When we start that, it's also going to awaken a new script.
So at that stage or a new class at that stage, if something already exists, if there's already one
in there, so this new one makes the second one, then we destroy this new one.
However otherwise or else is how we write otherwise.
Then don't destroy on load.
And once again it's the game object.
So that's our way of saying each time you load a scene, don't destroy it, have it, stick around,
have it stay there.
There's another couple of things I'm going to do in here.
I'm going to remove update.
I don't think we're going to need that next stop.
I'm going to create a public method that I can call from somewhere else.
Call from the player script, I think is where we'll call it.
So public void process, player death is what I'll call this death.
And within here I'm going to add two methods, one of which I'll do with you now, and the other I'll
set as a challenge for you.
Start by adding an if statement, if the player lives and we need to create a variable for this.
So if player lives is greater than one.
So in other words, you still have enough to keep going.
Let's jump up and create this now.
So I will create a serialized field.
It will be of type int.
We don't want to have a fraction of a life.
We'll call this player lives.
Player lives.
It looks like the player leaves now its player lives will initialize that at three, give the player
three lives.
So back down into here.
If player lives, what have I done wrong?
Play lives.
Well, I've had an extra se down there.
I'm in the habit of the find objects of type.
I'm find everything's of type players of lives.
So okay, just player lives greater than one as per the variable we've just made.
Then we want to do something.
Well, we'll call this for now.
Take life.
So take a life of the player.
It's a method.
We'll create that in a moment, else or otherwise.
We want to do what?
Well, I'll make this as a method as well.
There's only a couple of lines, but it's nice.
It reads more nicely.
If we've got separate methods for these, we'll call this reset game session.
So basically, if you're at the point where you've lost all your lives, we want to reset that.
So I want to say the single turn, just reset, start a new session.
So let's create reset game session together.
Then I'll get you to do take life as a challenge so I can click on game session control period or control
full stop.
Generate this method that'll generate down below.
Just clean it up a little bit void.
And then we don't want to throw an exception.
We want to put some content in here.
So when we reset the game session, we want to first of all, access scene manager and because it's
not showing up, it means we haven't used that.
Once again, I can hit control period and then add the using unity scene management that will plop that
up at the top.
Yes.
Thank you.
Scene management.
Good.
Now we should know scene manager something we want to do.
What?
We want to load the very first scene.
So I'm going to hard code this in load scene zero assuming that the very first thing we come across
is going to be the main menu or the first level for now, it's just the first level.
Later on, we might need to come back and change this, but I think this is okay for the moment.
Just saying go back to the first level or first scene we have in our entire build at this point in time.
And then when we do that, it's really, really important that we destroy this thing, destroy this
game object.
So we're destroying this instance of the game session so that if someone's playing this and they push
play again, we don't have all this stuff hanging around.
We get a totally fresh new instance of the game session.
That can be the first one that when other ones come along and says, Oh, there's already one here,
but at some point we need to reset it back to having none of them.
That's what we're doing here.
When we destroy the game object, that's most of what we need in here.
The last thing I'm going to give to you is a challenge.
Take life, so finish processing the player.
Death reduced the number of lives when the player has some sort of death incident.
And then to load our current scene that we're currently on when that happens, we've done this a couple
of times in this section, so there's some code you might want to go look at and grab, but there's
your challenge, plus video.
See you back in when you're done.
So take life.
I'm going to control period to generate that as a method, clean it up a little bit, get rid of our
exception.
First thing I want to do in here is player lives.
Now I'll write it the long way then the short way equals player lives minus one.
Now the quicker way we can write this, it's exact same thing is to say player lives minus minus.
That will decrease whatever the current value is by one.
Now we need to load where we currently are and we've done this before.
I'm going to use it because our build index is an integer.
Current scene index equals scene manager dot get active scene.
What's the active scene we're looking for?
Well, we need to know from the build index what its number is and then we're going to load that.
So scene manager, dot load scene and we're loading the current scene index semicolon.
Now the last step we need to do because we created a public method process player death that goes through
all of this kind of stuff on the game session.
We need to find a place to call that, so I need to open up my player movement script.
It's a little bit more than player movement, but I'm okay.
We could rename it to player, we could break it out into a number of different scripts.
But I'm okay for now because there's no confusion.
There really is only one thing in here when we move in a way that causes us to die.
Well, we want to be reloading certain things.
Let's have a look at where we've got die in here.
This is a big class now, isn't it?
Die if this happens.
Bump, bump, bump down at the very bottom.
We're going to look for our game session.
So find object of type.
Object of type.
In this instance, we want to find the one that exists for an object of type.
We're looking for game session parentheses, full stop.
And we're going to call which method?
Hopefully the one we just created.
Process player, death, parentheses, semicolon and we'll say we'll see how this works.
Probably need to first of all, go and throw this into all of our other levels.
So I'll just do that quickly.
Jump into levels.
Level two, save level one.
Yes, level two.
We need to grab our prefabs and put in game session into the level.
Save that up over to our levels.
Fine level three prefabs game session save.
So I'm going to go back to level one just because it's nice and simple and we can go through this process
quite quickly.
If you click on play, you'll see it don't destroy on load.
If we expand the don't destroy on load, you can see the game session in here.
Player lives run, run, run, drop.
It's down to two, down to one, down to zero.
And then it should reset.
Once again, we get a new game session and it's back up to three.
I just noticed one bug that I just found just now.
I click on play if I shoot bump.
It takes us to the next scene.
Okay, let's fix that bug really quickly now.
I don't want to take up too much time in this particular lecture.
If we click on the play off, just going to tag the player as player and then jump over into one of
my scripts.
Which one?
This is going to be the level exit.
Open that up.
What do we need to do in here?
Little micro challenge for you.
Pause the video and see if you can have it.
So only the player starts the code routine.
Well, it should be reasonably straightforward if other tag equals equals parentheses player, make
sure we spell it exactly right.
Wrap our curly braces around the stock code routine.
Pop that in there.
Get our formatting figured out.
We'll save that.
Hopefully, that's going to work.
Just do a little bit of a test on this.
I totally didn't realize we had that bug until I saw it accidentally just now.
I was trying to click on one thing and I shot Let's see if I shoot now at the level exit editor I'm
shooting.
Nothing's happening.
If I run down and run through it, I'm the player.
Yes.
And the player is recognize game session is still telling us we've got three player lives.
Let me just run into the spikes to make sure I haven't broken anything with that.
And that's all working.
Cool.
There we go.
We fixed the bug as a bit of a bonus.
That's very exciting.
That's what game development is all about.
Making bugs and fixing bugs.
So great stuff.
I'll see you in the next lecture.