1 00:00:04,010 --> 00:00:10,460 ‫In this video, you will learn how to use player preferences, which allows you to save data such as 2 00:00:10,460 --> 00:00:14,180 ‫the high score player names and so forth. 3 00:00:14,390 --> 00:00:15,980 ‫So let's go ahead and do that. 4 00:00:16,190 --> 00:00:22,460 ‫Let's create a new script and let's call that one saving data. 5 00:00:25,800 --> 00:00:28,910 ‫Now let's go ahead and go into that saving data. 6 00:00:28,980 --> 00:00:34,930 ‫And here I want to increase a number every time that I pressed the update button. 7 00:00:34,950 --> 00:00:36,660 ‫So let's create a number. 8 00:00:38,080 --> 00:00:43,330 ‫INT number and let's set that to zero. 9 00:00:43,960 --> 00:00:46,670 ‫And each time that I press the space key. 10 00:00:46,690 --> 00:00:50,800 ‫So if input dot get key down. 11 00:00:54,580 --> 00:00:56,530 ‫Key code is space. 12 00:00:56,530 --> 00:01:01,660 ‫So each time I press space, I want to increase the number by one. 13 00:01:02,620 --> 00:01:04,480 ‫So number plus plus. 14 00:01:05,410 --> 00:01:08,380 ‫And at the same time I would like to save that number. 15 00:01:08,380 --> 00:01:16,240 ‫So I would like to store the number that I had last time or even better that I had as a maximum. 16 00:01:16,240 --> 00:01:19,930 ‫So I will need one method in order to find out what the stored number was. 17 00:01:19,930 --> 00:01:23,800 ‫And then I need to compare it and I can override the stored number. 18 00:01:23,890 --> 00:01:25,450 ‫So let's go ahead and write a method. 19 00:01:25,450 --> 00:01:30,520 ‫So int because it's going to return it in integer get number. 20 00:01:31,600 --> 00:01:36,540 ‫And in here I want to search for player preferences. 21 00:01:36,550 --> 00:01:48,100 ‫There is a class called player pref and that class has a method called get int and set int. 22 00:01:48,190 --> 00:01:52,180 ‫So I want to get an integer, so I'm going to get int. 23 00:01:52,630 --> 00:01:57,520 ‫Of course you can see here it also can get strings which you can use for usernames, get floats which 24 00:01:57,520 --> 00:01:59,530 ‫you can use for floating point numbers. 25 00:01:59,530 --> 00:02:02,770 ‫But I'm just need a whole number which is a integer. 26 00:02:02,860 --> 00:02:08,340 ‫So I'm going to get the integer and now I need to give it a string key. 27 00:02:08,350 --> 00:02:10,300 ‫As you can see, there are two different overloads. 28 00:02:10,300 --> 00:02:13,450 ‫One is just the key and one is the default value. 29 00:02:13,450 --> 00:02:18,340 ‫So the one with just the key is the one where I will just get the key. 30 00:02:18,340 --> 00:02:20,260 ‫If there is one, then it's fine. 31 00:02:20,260 --> 00:02:21,310 ‫I will get a number. 32 00:02:21,310 --> 00:02:24,040 ‫If it doesn't exist, then I won't get a number. 33 00:02:24,100 --> 00:02:30,520 ‫And this one with the default value that will set the value that I have assigned here if there is no 34 00:02:30,520 --> 00:02:30,880 ‫number. 35 00:02:30,880 --> 00:02:34,420 ‫So let's say there was nothing saved and stored under the key. 36 00:02:34,420 --> 00:02:36,880 ‫Let's say my number. 37 00:02:37,150 --> 00:02:39,580 ‫You could also call it high score or whatever. 38 00:02:40,480 --> 00:02:47,260 ‫If there's nothing safe there, I want the value to be zero or I want the value to be one or whatever. 39 00:02:47,260 --> 00:02:48,790 ‫So you can assign that. 40 00:02:48,790 --> 00:02:53,170 ‫So what I'm going to do is I'm going to create a new integer. 41 00:02:53,170 --> 00:03:03,010 ‫So int my number is player press, get into my number and if there is none then use the default value 42 00:03:03,010 --> 00:03:03,760 ‫of zero. 43 00:03:03,790 --> 00:03:05,590 ‫Now we need to return that. 44 00:03:05,590 --> 00:03:14,950 ‫So return my number and now we can use that method in order to check if my number is bigger than the 45 00:03:14,950 --> 00:03:15,700 ‫current number. 46 00:03:15,700 --> 00:03:21,640 ‫So here this is the current number and my number get number will be the number that is stored in player 47 00:03:21,640 --> 00:03:22,120 ‫press. 48 00:03:22,120 --> 00:03:33,670 ‫So I'm checking if the number is higher, then get number, go ahead and store that number. 49 00:03:33,940 --> 00:03:38,350 ‫So go ahead and use player press dot set int. 50 00:03:38,350 --> 00:03:46,630 ‫So save the int at the position my number with the value of number. 51 00:03:47,890 --> 00:03:53,320 ‫So that means this number here which is which starts off with zero and is increased each time that we 52 00:03:53,320 --> 00:03:54,610 ‫press the space key. 53 00:03:54,970 --> 00:03:59,950 ‫So set that up if it's higher then whatever that is. 54 00:04:00,520 --> 00:04:11,380 ‫So what we can do here is even use debug dot log and give us the number onto the console. 55 00:04:11,950 --> 00:04:17,080 ‫All right, so let's save that script and let's add it to our scripts. 56 00:04:18,310 --> 00:04:19,840 ‫Saving data is there. 57 00:04:19,840 --> 00:04:20,530 ‫All right. 58 00:04:21,370 --> 00:04:25,960 ‫We still will create the cubes, but that shouldn't be that much of an issue. 59 00:04:26,470 --> 00:04:26,890 ‫All right. 60 00:04:26,890 --> 00:04:32,800 ‫So I'm going to start and as you can see, it shows me one, two, three, four, five. 61 00:04:32,890 --> 00:04:36,310 ‫Now I'm going to stop this game. 62 00:04:36,730 --> 00:04:46,330 ‫Let's go ahead in the start method and debug log our current high score. 63 00:04:46,330 --> 00:04:49,450 ‫So let's get our number that was stored. 64 00:04:49,450 --> 00:05:04,660 ‫So let's get number here and maybe we should even write some text stored number is plus get number. 65 00:05:06,370 --> 00:05:08,350 ‫All right, so let's check that out. 66 00:05:08,350 --> 00:05:09,280 ‫Let's run it. 67 00:05:11,680 --> 00:05:15,100 ‫And our console will say stored number is five. 68 00:05:15,370 --> 00:05:19,860 ‫Now, if we go ahead and press the key, we can see six, seven, eight, nine, ten. 69 00:05:19,870 --> 00:05:24,730 ‫So it only is displayed if we have more than the stored number. 70 00:05:25,000 --> 00:05:29,650 ‫So you can see here this debug log number is only printed when it's higher. 71 00:05:29,650 --> 00:05:32,830 ‫So maybe we should print it any time. 72 00:05:33,460 --> 00:05:34,930 ‫Now let's try that again. 73 00:05:36,400 --> 00:05:38,620 ‫Let's run the game and we will see. 74 00:05:38,630 --> 00:05:40,690 ‫Store number is 24. 75 00:05:40,720 --> 00:05:42,760 ‫So now let's press space. 76 00:05:42,760 --> 00:05:49,470 ‫And as you can see, we will increase the number by one each time we press space and now the stored 77 00:05:49,480 --> 00:05:51,210 ‫number will be 35. 78 00:05:51,220 --> 00:05:53,440 ‫So let's pause the game. 79 00:05:54,290 --> 00:05:57,080 ‫Or end the game pretty much and start it again. 80 00:05:57,350 --> 00:06:00,000 ‫And now you will see that the store number is 35. 81 00:06:00,020 --> 00:06:00,410 ‫All right. 82 00:06:00,410 --> 00:06:02,810 ‫So now you see how you can store data. 83 00:06:02,840 --> 00:06:06,950 ‫You should only use player press for a very limited amount of data. 84 00:06:06,950 --> 00:06:11,720 ‫So for things like one integer or one number or one floor. 85 00:06:11,780 --> 00:06:14,900 ‫And of course, also just one single name. 86 00:06:14,900 --> 00:06:18,610 ‫So you shouldn't use that in order to save a complex game or something like that. 87 00:06:18,620 --> 00:06:23,600 ‫That's something that you will learn in a different chapter where we will use serialization in order 88 00:06:23,600 --> 00:06:27,980 ‫to save complex data or in order to save even whole game objects. 89 00:06:28,250 --> 00:06:28,640 ‫All right. 90 00:06:28,640 --> 00:06:32,600 ‫I hope you could follow along and try to do that yourself. 91 00:06:32,630 --> 00:06:34,750 ‫Please go ahead and go to the next video. 92 00:06:34,760 --> 00:06:36,500 ‫We will do some more fancy stuff. 93 00:06:36,500 --> 00:06:37,490 ‫So see you there.