Hello, guys.
It's time to talk about saving system.
We basically have almost all the functionality in the game.
We only need to save coins we have so we can use them and shop on the next run.
So we need to make saving system.
We need to make a loading system and we need to make a shop functional.
All right.
And then we can jump to sounds and polish and with the post processing and we are done.
Yeah.
So let's begin how saving system going to work.
First I want to make folder that I'm going to call UI and I'll take these two scripts, put it in the
UI and then I'm going to drag this UI to the scripts folder.
So let's go to game manager and talk about saving system.
We're going to use player preps.
It's very possible that you've seen this before and you use this before, but if you didn't, don't
worry.
It's pretty simple to understand.
I'm going to explain it right now and it will take only a couple of minutes.
I'm going to create private void function let I'm going to just call save for now.
I'm not going to do anything else here.
So player reps this would be going to use.
Player press allows you to save info on the device and upload info from the device as well.
You can save only three type of variables.
You can save integer so you type set int write, then you give it a name.
For example, we save in coins, we have total amount of coins, so I'm going to type total amount of
coins.
Name should be very understandable, super clear and understandable and it should be same every time.
After a comma you can pass a value.
For example, we want to save amount of coins.
We have coins.
Wait a second.
Coins.
And to get this information, we need to use player proofs again, but a bit differently.
Let's go and create, start function.
And in the start I'm going to do player preps.
Get int with a name.
And actually I don't really like this long name because we're not going to have lots of variables to
save.
We can just type here coins and it's going to be fine.
So coins it.
Now as I said in the start, we say get int coins and that's it.
You would get the amount of coins you have.
But if you just write a line of code like that, it's going to give you nothing.
So we can do debug.
For example, we can see it in the console.
Now we need to call this save function from somewhere somehow and I offer you to do that in the player
when he dies.
Let's go to our day care routine and over here before we restart level, let's do game manager that
instance Dot.
Save.
I guess it's not public.
Well, let's make it public.
Ock now in the player dot save.
All right, now I'm going to collect some coins, and then I'm going to die.
To have eight coins.
I'm dying.
Game is restarted and in the console we can see number eight, which is cool, which is what we need.
So we save the info about eight coins.
Now if I'll exit my play mode and I'll enter play mode again, I'm sure they're going to be number eight.
Eight.
Very good, right?
Now you can clear all of the information you saved before by going to edit.
Clear all player briefs.
Let's do this now.
If I'll start my game, it's going to be zero.
Yeah.
Now, sometimes you need to put some information in case the value is zero, in case there is nothing
saved with this name.
What you can do is to put comma inside and give it a default value.
For example, I'll put just five.
Let's go back.
I'm going to clean it all.
And you can see number is five.
Now, I'm going to go fill up the volume with something.
I'm going to collect some coins.
And I had six coins and died, and now I have number six.
Pretty simple, right?
Now, I told you, you can save only three types of variables.
It could be integer, it can be a player.
Press set float.
And it can be.
String player.
Press dot.
Set String.
With this method, you cannot do anything else.
Integer, float and string.
But it doesn't mean you cannot save info that you need.
For example, you want to save position or you want to save color that you purchased.
Just for example.
All right.
Let's imagine we want to save color we purchased.
And let's imagine this color is white.
Just to not make it complicated for us.
Now, how you would save a color, basically you need to do color are for red.
Then you pass it a value.
It's going to be one.
Then player, press set, float color green, then you pass it a value, then player press set float.
Color blue.
You posit the value, and I'm going to use one just to make white color.
And as of alpha, we're not going to call it.
We're going to use default value.
Then in the start, let's do this.
We're going to get access to the player component.
S.R.
So let's do a Sprite renderer.
S r equals to player don't get component of sprite renderer and then we get the color.
How are we going to get it?
Very simple color.
New color equals to new color.
You see we need red, green, blue and alpha for red.
I'm going to do layer preps, get float.
Oh.
Color are, then when you do a green one, let's do this Layer preps, get float color g, then player
press get float color B and for the alpha weight it should be a parentheses here.
Yeah.
And for the alpha we can use default value, as I said before, because we don't want this color to
be transparent.
We always want it to be visible, but we have no point of saving.
Just a number one.
We could put a number one here or we could do this player press just in case we want to save Alpha,
but not always.
We can do get float color A and just in case there is no saved value, we're going to pass one just
like that.
Then parentheses and close it.
And I did it like this.
So it's easier to read.
But actually it can be just one line of code, very long line.
All right.
So this is the saved color we have now.
Let's go back.
I'm going to change color of the player to something different, for example.
Blue like this.
Look, Red, green, blue and alpha is different.
I'm going to go and die.
Now.
Color was saved.
But it didn't work.
Wait a second.
Oh, yeah.
We did not assign it.
We just got the color as color equals to new color.
But now, because value isn't saved yet, we passed 000.
So we have black color of the player.
But if I'll go and die, my info are going to be saved and I'm going to save it to a white color.
Just let me find a drop.
Whereas the trop one boy.
Hear this.
Why did I die like that?
I should have extra life.
Oh, I guess I know.
Okay, we'll fix that in the Polish stage.
Let's focus on color.
Yeah.
As you can see, I got white color.
Exactly the color we saved before.
Cool.
Now we have the functionality, and now we have a bit of understanding on how it works.
Let's modify these functions just a bit to save info on color.
Let's save info on coins and score as well.
Okay, let's do this.
Yeah, let's organize this code a bit.
So what we're going to do here is to I can delete debug, let's delete debug.
We need the sprite renderer of the player.
We need a new color of course, and we need to set color to a new one.
And we're going to change the name of this function from start to load load color.
Now let's make a safe color function, which is going to be also easy to do public void, save color.
And we need to pass values here.
We need to pass float our float G and float B, and inside I can basically.
Take gold from here.
Cut it out, paste it in.
And instead of values, I'm going to pass our.
G.
B.
Now pay attention here.
We save in only one color, and I want to use it for saying color for player.
So if you want to save color for a platform, you would need to create another function with another
variables of r.g.
B, you would maybe have to modify a name by just a bit to something like platform color, for example.
Yeah, but I want to use it only for player.
For now it's enough.
So we save color through this.
We load color through this.
Now we need to save amount of coins.
And before we save amount of coins, we need to get the amount of coins saved already.
How are we going to do this?
We're going to do int saved coins equals to layer press.
Get int of coins.
That's enough.
Now let's save those coins we're going to do.
Press set int.
Yeah, we're going to type here.
Saved coins, the value we had before.
Plus coins we collected in this game.
Right?
These coins.
So.
Plus coins.
And that's it.
Now, next time you upload, you're going to have summary of those saved coins before plus new coins,
and you have a new amount of total coins.
Let's rename this to save info.
Yeah, this is better.
Now let's save info.
First we need float of the last score.
Last score equals to player press.
Get float last score.
First we need to save info on the score.
How we want to calculate the score is a different question.
What I want to do is just to multiply coins by distance run.
So it's going to be float score equals two.
Distance multiplied by coins.
Weight added me spell that distance multiplied by coins.
Then we're going to save it.
Player press said.
Float off last score to score.
And then we also want to save high score.
What we're going to do is a simple check.
If player preps get float high score is less than score means new score.
Then then player preps set lot of high score to a new one.
New value score.
Very simple.
You got What are we doing here?
We're checking if high score is less than new score.
And if new score is bigger, then we set high score to a new score.
Very simple stuff.
Much, much, much easier than what we did before with generation of platforms and parallax background
and let's climb.
Yeah, now we save in the info when we need to upload the info.
Of course we need to do it at the beginning of the game.
I want to upload color and if you want to upload the color you can upload the color.
You can just go to awake and type here load color.
Personally for my version of this game.
I will not save color of the player that he purchased.
I will not load the color I want player to burn the money.
I want him to collect coins.
I want him to purchase color for a new run.
When he dies, he loses his color, his mistake, his problem.
In this way we have circulation of the money.
Yeah, but if you want, you can just keep it here, load color, but I'll comment it out.
Now, regarding the score info, let's go to UI main.
You.
I mean, over here.
And we need to get access to two variables serialize field, private text mesh, pro gooey, which is
last score.
Text one more.
Highest court text and one more private text match pro cui coin's text.
We could upload last score and high score just once and the start.
But regarding the coins, we need to do it regularly.
We need to do it again and again.
For example, if you uploaded the game, you have some amount of coins, you went to the shop, you
spend coins, then you're going back to menu.
You need to see new amount of coins.
We could do it in different places.
I offer to do it in the switch UI.
Let's just do this.
Coins text the text equals to layer press, get int of coins, get int of coins and we need to do two
string if you remember because this is a number to string with format of one comma.
And in the start.
Last score text.
That text equals two.
And what was that?
Let's go and check that.
Last score.
Let's just copy this, paste it in and change this from set to get and remove this.
And same for the high score.
High score.
This would be a high score.
Now we need to fix this mistake here.
And that is simple.
Just do it to string with the format.
And same for the next one.
Now we have the functionality.
We only need to decide when we save in the game, Right.
And I think we need to do this whenever you do restart of the game.
So let's change this call by just a bit.
I'm going to put curly brackets here and here.
Delete the return sign.
Press Enter, enter, enter.
And before we upload scene, we're going to do save info.
So now, no matter when, no matter how, no matter why we restarted the level, we're going to save
info.
Now we can go to player and delete this.
Save info from here.
Let's go in.
Double check that zone.
Yeah, we do restart level here and if you go inside, it's going to save info as well.
Very cool.
Very simple, very easy.
Regarding saving color and change the amount of money in the bank when you're buying something, We're
going to do it in the next video when we finalize shop functionality.
Yeah, let's go and do that.
And now wait, let's double check it first.
You see, we need to assign values.
And I already thought of one mistake.
I did.
First, let's go, Canvas.
General info.
I want to change this to info bar.
Info bar.
Now canvas last score, best score coins.
And I guess now it's going to be empty.
If there is nothing, which is not cool.
Yeah, Totally empty.
Well, maybe it's not that bad.
I guess it's going to be your first run.
But then when you hit something and you die.
It's going to have a number.
We only need to do additional text here.
You see, we have a number, but no text.
Let's go back to code.
I mean, we're going to add some text here, quotation marks, last score, a couple of spaces plus
and over here, the same.
Highest score and name going to be longer.
I don't really like it.
I'll just type high score.
And plus, this is better.
Okay, cool.
I like it.
Let's go to the next video.