‫In this video, you will learn how to use player preferences, which allows you to save data such as
‫the high score player names and so forth.
‫So let's go ahead and do that.
‫Let's create a new script and let's call that one saving data.
‫Now let's go ahead and go into that saving data.
‫And here I want to increase a number every time that I pressed the update button.
‫So let's create a number.
‫INT number and let's set that to zero.
‫And each time that I press the space key.
‫So if input dot get key down.
‫Key code is space.
‫So each time I press space, I want to increase the number by one.
‫So number plus plus.
‫And at the same time I would like to save that number.
‫So I would like to store the number that I had last time or even better that I had as a maximum.
‫So I will need one method in order to find out what the stored number was.
‫And then I need to compare it and I can override the stored number.
‫So let's go ahead and write a method.
‫So int because it's going to return it in integer get number.
‫And in here I want to search for player preferences.
‫There is a class called player pref and that class has a method called get int and set int.
‫So I want to get an integer, so I'm going to get int.
‫Of course you can see here it also can get strings which you can use for usernames, get floats which
‫you can use for floating point numbers.
‫But I'm just need a whole number which is a integer.
‫So I'm going to get the integer and now I need to give it a string key.
‫As you can see, there are two different overloads.
‫One is just the key and one is the default value.
‫So the one with just the key is the one where I will just get the key.
‫If there is one, then it's fine.
‫I will get a number.
‫If it doesn't exist, then I won't get a number.
‫And this one with the default value that will set the value that I have assigned here if there is no
‫number.
‫So let's say there was nothing saved and stored under the key.
‫Let's say my number.
‫You could also call it high score or whatever.
‫If there's nothing safe there, I want the value to be zero or I want the value to be one or whatever.
‫So you can assign that.
‫So what I'm going to do is I'm going to create a new integer.
‫So int my number is player press, get into my number and if there is none then use the default value
‫of zero.
‫Now we need to return that.
‫So return my number and now we can use that method in order to check if my number is bigger than the
‫current number.
‫So here this is the current number and my number get number will be the number that is stored in player
‫press.
‫So I'm checking if the number is higher, then get number, go ahead and store that number.
‫So go ahead and use player press dot set int.
‫So save the int at the position my number with the value of number.
‫So that means this number here which is which starts off with zero and is increased each time that we
‫press the space key.
‫So set that up if it's higher then whatever that is.
‫So what we can do here is even use debug dot log and give us the number onto the console.
‫All right, so let's save that script and let's add it to our scripts.
‫Saving data is there.
‫All right.
‫We still will create the cubes, but that shouldn't be that much of an issue.
‫All right.
‫So I'm going to start and as you can see, it shows me one, two, three, four, five.
‫Now I'm going to stop this game.
‫Let's go ahead in the start method and debug log our current high score.
‫So let's get our number that was stored.
‫So let's get number here and maybe we should even write some text stored number is plus get number.
‫All right, so let's check that out.
‫Let's run it.
‫And our console will say stored number is five.
‫Now, if we go ahead and press the key, we can see six, seven, eight, nine, ten.
‫So it only is displayed if we have more than the stored number.
‫So you can see here this debug log number is only printed when it's higher.
‫So maybe we should print it any time.
‫Now let's try that again.
‫Let's run the game and we will see.
‫Store number is 24.
‫So now let's press space.
‫And as you can see, we will increase the number by one each time we press space and now the stored
‫number will be 35.
‫So let's pause the game.
‫Or end the game pretty much and start it again.
‫And now you will see that the store number is 35.
‫All right.
‫So now you see how you can store data.
‫You should only use player press for a very limited amount of data.
‫So for things like one integer or one number or one floor.
‫And of course, also just one single name.
‫So you shouldn't use that in order to save a complex game or something like that.
‫That's something that you will learn in a different chapter where we will use serialization in order
‫to save complex data or in order to save even whole game objects.
‫All right.
‫I hope you could follow along and try to do that yourself.
‫Please go ahead and go to the next video.
‫We will do some more fancy stuff.
‫So see you there.