Hello guys.
Welcome to the video.
So in this one I'm going to explain how the saving system is going to work.
And because this course is mostly for beginners and game is very simple itself, we don't really need
a big saving system.
So we can just use a Playerprefs.
Now let me explain.
How do they work?
What is it?
I'm going to make an empty object.
And I'm going to make a script.
Uh, player refs example.
All right, let me open the script.
Over here I'm going to make a public int, let's say uh fruits.
I'm going to do public uh, float seconds.
And I'm going to do public string level, um, name.
Now these three data types that can be saved with help of Playerprefs to save value, let's do public
void save um value.
All right.
And I'm going to use context menu so I can click it in the play mode save value.
So if you want to save the value you can type Playerprefs set.
And here you can choose what you want to save float integer or string.
For example I want to save the integer of fruits collected.
I need to give it a name so it's easier to find later on.
And I can just type fruits collected.
And then after a comma, we're going to need to give it a value so it can save it with the name of fruits
collected.
Let's say I want to save five.
Then let's make a method public void load value.
And is going to be context menu load value.
And over here I'm going to do Playerprefs dot.
And here we need to type get int because now we getting the value you need to give it a same name.
So it will work.
And that's just it for now.
So this is how you get in the value.
And we can not only get the value but we can say that fruits are equals to the value we've got.
All right.
This will save it.
This will load it.
Let's go to unity.
To the play mode.
And we can see I have zero fruits here.
Now I'm going to click save value.
I saved the value.
Nothing happened.
I just saved it.
Now let's go back.
Let's go to play mode.
Now this is a new instance of a game.
But here I can click load value and I'll have fruits five and go further instead of setting five.
Here you can use the variable fruits.
Uh, let's try again.
I'm going to give it 15.
Let's go to play mode.
Now I have 15 I can load value is going to become five.
I can set it to something new, let's say 35.
I can save it.
And then when I go out of a play mode, this will be reset it.
Then I can go to play mode again.
And I can load the value.
All right.
So now we know that there is also a way to set default value into playerprefs.
For example let's try to work with seconds.
For example, we want to save how many seconds it took the player to finish the level we could do Playerprefs
set float of let's do level one.
Time.
And we're going to give it a value of, uh, time.
The time we have so time in the game.
Then when we get in the value.
We're going to do seconds equals to playerprefs.
Get float of level one time name should be the same.
And over here when you load in the value, it may happen that you did not save any information before.
And in that case you can set default value.
So in case we cannot find the value that we saved before, we're going to load default value.
For example let's try 999 here.
This will be the biggest time that can be on the best time on the level.
Let's go back to unity.
Now I go to play mode.
And seconds is zero.
I'm going to load value and it's 9.99 because I've loaded the default value.
Now let me try to save the value.
And it will save time.
The time.
And now when we go to play mode again.
We can load value and it will be 7.8.
So that's the time I saved when I called this method.
Very simple one.
Now we're not going to practice for the string variable because it basically works the same.
You set the value and you get the value.
You just need to type set, string or get.
Uh, stream.
Now I want to show you a simple trick.
I want to show you how we can save other values with the help of float, int, and string.
So for example, we want to save information about level if it's unlocked or not we can simply.
Make.
Uh playerprefs set int of uh, let's say.
Level one.
Unlocked and if level is unlocked, we can simply save it as one.
If it's not unlocked, we can simply save it as zero.
And then we're going to do here Playerprefs get end of level one unlocked.
Default value will be zero, so if we did not save anything before it is zero.
It means it is false.
It is not unlocked.
Then I'm going to do something like bull level unlocked equals to playerprefs.
Actually, we can just get it here and do display.
A pref is equals to one.
And then if level unlocked then debug log level is unlocked.
All right.
Simple as that.
Now let's go ahead and try this.
I'm going to go to play mode.
Gonna try to load value and we don't see any message because level was not unlocked.
Now I'm going to save value and then I'm going to load value again.
And we can see level is unlocked because we saved one into integer level one unlocked.
So this is how we're going to save progress with levels in the next video.
As of now I just wanted you to see simple examples on Playerprefs.
Now let's go to the next one and we're just going to kick the hell out of this playerprefs system.
Okay, I'll see you there.