- Home /
Playerprefs and Star rating system
Hello!
I want to make a Three Star -rating system (like Angry Birds etc.) for my game, but I don't know how I would use PlayerPrefs to save my progress.
The medal system now works like:
if (Time < 20) {
firstStar = true;
Debug.Log ("You get first star!");
}
if (Time < 18) {
secondStar = true;
Debug.Log ("You get second star!");
}
if (Time < 16.5) {
thirdStar = true;
Debug.Log ("You get third star!");
}
Answer by moonstruck · Jan 15, 2015 at 03:09 PM
Hello!
For instance, you can do the following: PlayerPrefs.SetInt(levelKey, levelStars)
, where levelkey
is a unique key for a level (something like string.Concat("Level", levelIndex.ToString())
(assuming levelIndex
is the index of the level which result you want to save) and levelStars
is the amount of star the player achieved. It can be calculated like this (rewriting your code):
if (Time < 16.5f) levelStars = 3; else if (Time < 18) levelStars = 2; else if (Time < 20) levelStars = 1; else levelStars = 0;
Thank you for your answer. However I forgot to mention that some stars may be acquired for example by collecting all coins, and thus star #2 can be acquired before star #1.
For example it could be:
if (Time < 20) {
firstStar = true;
Debug.Log ("You get first star!");
}
if (CoinController.coinsCollectedThisLevel == 9 && raceTime < 20) {
secondStar = true;
Debug.Log ("You get second star!");
}
if (Time < 16.5) {
thirdStar = true;
Debug.Log ("You get third star!");
}
Then ins$$anonymous$$d of simple star count levelStars
use something like this:
int levelStars = 0;
if (Time < 20)
levelStars |= 1;
if (CoinCollector.coinsCollectedThisLevel == 9 && raceTime < 20)
levelStars |= 2;
if (Time < 16.5f)
levelStars |= 4;
And then save this value. In order to test if the user got 1, 2 or 3 stars use if (levelStars & 1)
, if (levelStars & 2)
or if (levelStars & 4)
correspondingly. Even though it may be more efficient if you store all star data in one pref value. However you will need some parsing to get them.
Thank you.
Will this work for a graphic that if I collect 9 coins and time < 20, the middle star will unlock?
Of course, your if's are separate (not if-else) and if you want to learn more about using such types of algorithms search for C# bit-wise operators and binary representation of numbers.
That's a bit-wise operation and a shortened syntax of levelStars = levelStars | 1
. It sets the first bit of the variable levelStars
(because number 1 has only 1 bit at position 1 and all others 0). Other numbers (2, 4) set 2nd and 3rd bit of the variable. And the oparator | itself is "or" operation.
Answer by Owen-Reynolds · Jan 15, 2015 at 03:15 PM
This might sound mean, but get better at programming -- just read any "How to write programs" book, not even Unity -- and this will be a lot easier.
For this, just have one variable int stars;
and set it to 1 2 or 3. Then playerPrefs can save the level number and the stars
variable. To have all the levels, use an array of stars (`Stars[5]=2` would say you got two stars on level 5.)
What you have so far is a very common not-so-good way to start. And everyone who learns to program, even for banks, learns better ways to use variables and other basic computer tricks.
Thank you for your input. However I forgot to mention that some stars may be acquired for example by collecting all coins, and thus star #2 can be acquired before star #1.
This is why I can't simply do how you proposed.
For example it could be:
if (Time < 20) {
firstStar = true;
Debug.Log ("You get first star!");
}
if (CoinController.coinsCollectedThisLevel == 9 && raceTime < 20) {
secondStar = true;
Debug.Log ("You get second star!");
}
if (Time < 16.5) {
thirdStar = true;
Debug.Log ("You get third star!");
}
Should've included it in the original question.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Making a fill take exactly n seconds to complete 2 Answers