Do i need to make Savefile for every Level ?

Hi Guys
My game runs very well from the start Menu, to first level, till the end of it. Now i have finished the first level with all my points. I get back to the Start Menu to choose the second level to play it. Because i’m using the same savefile in the first level, I get all my previous save details to the second level (money, points and weapons). Yes, I can use DeleteKey or DeleteAll, so I can play second level as its. But what if I will make a statistic for every level in the game (Time, Score, Number of enemies you killed, Number of death) ?
How to make every level had it own savefile ? do i need to make script for every level ?
I remember there was an old tutorial talking about this in youtube. To solve this, He uses “If + level name” in the same savefile.
Yes, In my mind there are some solutions (I didn’t try it yet). But what the professionals do in this situation. I need some clues and advices.

For example. this pic from Cut the rope.


Look to the levels buttons. you will find that evey level have it own statistic (stars, points). how to do this ?
Are there script for every level ?

Then you need to use keys that reference the level number. Instead of saving the number of stars with something like

PlayerPrefs.SetInt("stars", starsAchieved);

you will do something like

PlayerPrefs.SetInt("stars" + levelNum, starsAchieved);

where levelNum is the current level number. So this will save values for stars1, stars2, stars3, etc. And when loading this data, of course, you use the same level-referencing keys.

Best,

  • Joe
3 Likes

Perfect. It work, like every level had separate score file. Thank you so much :). Just two things i need to clear it.
1/ As the pic. it show number of game levels with 3 starts under level icon. I would replace these stars (Image) with score (Text). I did the same thing in my game. Every level icon had small text box. How to make it show final score that got it inside it ?

        PlayerPrefs.SetFloat("score" + numlevel,score);
        score = PlayerPrefs.GetFloat("score" + numlevel);
        level2.text = (score.ToString () + /* number of the level is 2 inside index ? */);

2/ when i playing any level, at the end it save “auto”, and get me out from the level scene to main menu scene. If the player select any new level to play it have to delete previous game save. correct ? Because, If i played first level and reached the middle of the level, saved the game and exit. I went to play the second level and did the same thing. If i get back to the first level it will load from the middle of the level ! and if i return to the second level the same thing will happen. Like both levels have save file (( That is way when i was playing Call of duty and select new mission to play, message pop-up tell me that i will lose all previous save /)). So, better for me to make another save file work to save the final result only ?

Question 1 displays some fundamental confusion. You already have the score, and the level number, on line 2. You’re using it to store the score in prefs — that’s great, for code that runs at the end of the level when you have a score. On line 3, you’re pulling the score out of the prefs. That doesn’t make any sense to do if you have just stored it. You would do that at other times, when you don’t already have the score.

And then in line 4, you’re stuffing the score into the text. That line is correct as it is; just remove the comment. You already have the score, either because you had it before we got to line 2, or because you just pulled out the correct score for the level in line 3. So now you just display it.

I don’t quite understand question 2. If I have an auto-saved area, I would simply include the level number along with all the other data. Then when loading a level, I would compare my current level number to the auto-saved level number. If they’re the same, load the rest of the auto-save data and continue. If not, wipe out the auto-save data and start fresh.

1 Like

You are correct. I finished from save and load data. OK. I tried to show you how to make Text box appears score of a level.
But what i’m trying to do. Evey level had it own text box (Three levels = three text box). I want to load level score of every level and read it inside it own text box ?

        level1.text = (score.ToString ());  // score of level 1
        level2.text = (score.ToString ());  // here should appear the score of level 2
        level3.text = (score.ToString ());  // here should appear the score of level 3

This what happen with me. What i meant is if you did not finish the first level and went to play the second one. Your progress in the first level will be deleted. OK. Just let us skip this point. its not a big problem. Later I will deal with it.

Thank you so much Joe.

level1.text = PlayerPrefs.GetFloat("score1").ToString();
level2.text = PlayerPrefs.GetFloat("score2").ToString();
level3.text = PlayerPrefs.GetFloat("score3").ToString();

There is nothing mysterious here. I suggest you dig up the PlayerPrefs file, open it with a text editor, and see what it contains. It’s just a bunch of key/value pairs, and looking at them may help you understand.

1 Like

OK. Now is good. My mistake that i use one key for all levels. That’s why i was talking about insert the number of the level with the value key.
Now I changed all my keys and all things is good. Thank you Joe so much Joe. You should really make some tutorials and upload it to Unity tutorial page. People need this.
[SOLVED]

1 Like