WEBVTT

00:00.000 --> 00:00.420
Hello guys.

00:00.420 --> 00:01.320
Welcome to the video.

00:01.320 --> 00:04.800
So in this one I'm going to explain how the saving system is going to work.

00:04.800 --> 00:09.720
And because this course is mostly for beginners and game is very simple itself, we don't really need

00:09.720 --> 00:10.740
a big saving system.

00:10.740 --> 00:12.930
So we can just use a Playerprefs.

00:12.930 --> 00:13.830
Now let me explain.

00:13.830 --> 00:14.790
How do they work?

00:14.790 --> 00:15.480
What is it?

00:15.480 --> 00:17.370
I'm going to make an empty object.

00:18.290 --> 00:19.910
And I'm going to make a script.

00:20.240 --> 00:23.360
Uh, player refs example.

00:25.310 --> 00:27.380
All right, let me open the script.

00:28.630 --> 00:36.370
Over here I'm going to make a public int, let's say uh fruits.

00:36.550 --> 00:40.480
I'm going to do public uh, float seconds.

00:40.480 --> 00:44.410
And I'm going to do public string level, um, name.

00:44.410 --> 00:50.680
Now these three data types that can be saved with help of Playerprefs to save value, let's do public

00:50.680 --> 00:54.220
void save um value.

00:54.250 --> 00:54.760
All right.

00:54.760 --> 01:00.130
And I'm going to use context menu so I can click it in the play mode save value.

01:00.130 --> 01:04.210
So if you want to save the value you can type Playerprefs set.

01:04.210 --> 01:08.800
And here you can choose what you want to save float integer or string.

01:08.800 --> 01:14.230
For example I want to save the integer of fruits collected.

01:14.650 --> 01:17.710
I need to give it a name so it's easier to find later on.

01:17.710 --> 01:20.920
And I can just type fruits collected.

01:20.920 --> 01:25.600
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

01:25.600 --> 01:26.170
collected.

01:26.170 --> 01:28.360
Let's say I want to save five.

01:28.960 --> 01:34.060
Then let's make a method public void load value.

01:35.090 --> 01:39.380
And is going to be context menu load value.

01:39.380 --> 01:42.080
And over here I'm going to do Playerprefs dot.

01:42.080 --> 01:48.530
And here we need to type get int because now we getting the value you need to give it a same name.

01:48.830 --> 01:50.060
So it will work.

01:50.060 --> 01:51.830
And that's just it for now.

01:51.830 --> 01:53.480
So this is how you get in the value.

01:53.480 --> 01:58.310
And we can not only get the value but we can say that fruits are equals to the value we've got.

01:58.310 --> 01:59.090
All right.

01:59.510 --> 02:00.500
This will save it.

02:00.500 --> 02:01.850
This will load it.

02:01.850 --> 02:04.130
Let's go to unity.

02:05.350 --> 02:06.760
To the play mode.

02:07.880 --> 02:10.400
And we can see I have zero fruits here.

02:10.430 --> 02:13.340
Now I'm going to click save value.

02:13.370 --> 02:15.050
I saved the value.

02:15.050 --> 02:15.770
Nothing happened.

02:15.800 --> 02:16.700
I just saved it.

02:16.730 --> 02:18.230
Now let's go back.

02:18.530 --> 02:19.910
Let's go to play mode.

02:19.940 --> 02:21.980
Now this is a new instance of a game.

02:21.980 --> 02:31.040
But here I can click load value and I'll have fruits five and go further instead of setting five.

02:31.040 --> 02:33.530
Here you can use the variable fruits.

02:34.620 --> 02:36.030
Uh, let's try again.

02:36.060 --> 02:37.530
I'm going to give it 15.

02:37.530 --> 02:38.880
Let's go to play mode.

02:39.740 --> 02:44.150
Now I have 15 I can load value is going to become five.

02:44.150 --> 02:47.180
I can set it to something new, let's say 35.

02:47.180 --> 02:48.560
I can save it.

02:48.560 --> 02:52.100
And then when I go out of a play mode, this will be reset it.

02:52.100 --> 02:53.870
Then I can go to play mode again.

02:55.760 --> 02:57.860
And I can load the value.

02:57.920 --> 02:58.580
All right.

02:58.580 --> 03:04.220
So now we know that there is also a way to set default value into playerprefs.

03:04.220 --> 03:06.800
For example let's try to work with seconds.

03:06.800 --> 03:13.130
For example, we want to save how many seconds it took the player to finish the level we could do Playerprefs

03:13.130 --> 03:17.000
set float of let's do level one.

03:18.250 --> 03:19.000
Time.

03:19.670 --> 03:23.120
And we're going to give it a value of, uh, time.

03:23.120 --> 03:25.700
The time we have so time in the game.

03:26.290 --> 03:28.060
Then when we get in the value.

03:28.960 --> 03:32.950
We're going to do seconds equals to playerprefs.

03:32.980 --> 03:37.630
Get float of level one time name should be the same.

03:37.630 --> 03:42.580
And over here when you load in the value, it may happen that you did not save any information before.

03:42.580 --> 03:45.220
And in that case you can set default value.

03:45.220 --> 03:49.300
So in case we cannot find the value that we saved before, we're going to load default value.

03:49.300 --> 03:51.850
For example let's try 999 here.

03:51.850 --> 03:56.050
This will be the biggest time that can be on the best time on the level.

03:57.280 --> 03:58.810
Let's go back to unity.

04:01.500 --> 04:03.120
Now I go to play mode.

04:04.040 --> 04:06.200
And seconds is zero.

04:06.230 --> 04:11.960
I'm going to load value and it's 9.99 because I've loaded the default value.

04:11.990 --> 04:14.660
Now let me try to save the value.

04:14.930 --> 04:16.010
And it will save time.

04:16.010 --> 04:16.940
The time.

04:17.240 --> 04:19.280
And now when we go to play mode again.

04:20.440 --> 04:23.890
We can load value and it will be 7.8.

04:23.890 --> 04:28.660
So that's the time I saved when I called this method.

04:28.660 --> 04:29.710
Very simple one.

04:29.710 --> 04:33.790
Now we're not going to practice for the string variable because it basically works the same.

04:33.790 --> 04:35.470
You set the value and you get the value.

04:35.470 --> 04:39.160
You just need to type set, string or get.

04:40.120 --> 04:40.960
Uh, stream.

04:43.210 --> 04:45.010
Now I want to show you a simple trick.

04:45.010 --> 04:50.500
I want to show you how we can save other values with the help of float, int, and string.

04:50.500 --> 04:57.280
So for example, we want to save information about level if it's unlocked or not we can simply.

04:57.960 --> 04:58.980
Make.

04:59.190 --> 05:03.930
Uh playerprefs set int of uh, let's say.

05:04.610 --> 05:06.140
Level one.

05:06.830 --> 05:11.360
Unlocked and if level is unlocked, we can simply save it as one.

05:12.100 --> 05:15.220
If it's not unlocked, we can simply save it as zero.

05:15.670 --> 05:23.410
And then we're going to do here Playerprefs get end of level one unlocked.

05:23.590 --> 05:29.290
Default value will be zero, so if we did not save anything before it is zero.

05:29.290 --> 05:30.550
It means it is false.

05:30.550 --> 05:32.050
It is not unlocked.

05:32.820 --> 05:38.370
Then I'm going to do something like bull level unlocked equals to playerprefs.

05:38.400 --> 05:42.660
Actually, we can just get it here and do display.

05:42.660 --> 05:44.400
A pref is equals to one.

05:44.820 --> 05:50.280
And then if level unlocked then debug log level is unlocked.

05:51.590 --> 05:52.220
All right.

05:52.220 --> 05:53.180
Simple as that.

05:53.210 --> 05:55.130
Now let's go ahead and try this.

05:59.770 --> 06:01.330
I'm going to go to play mode.

06:03.800 --> 06:09.920
Gonna try to load value and we don't see any message because level was not unlocked.

06:09.950 --> 06:13.610
Now I'm going to save value and then I'm going to load value again.

06:13.610 --> 06:20.180
And we can see level is unlocked because we saved one into integer level one unlocked.

06:20.180 --> 06:23.450
So this is how we're going to save progress with levels in the next video.

06:23.450 --> 06:26.840
As of now I just wanted you to see simple examples on Playerprefs.

06:26.840 --> 06:30.770
Now let's go to the next one and we're just going to kick the hell out of this playerprefs system.

06:30.770 --> 06:32.030
Okay, I'll see you there.
