- Home /
Using playerprefs to remember a level completion
I have a function that gets called whenever a level gets completed:
public void LevelComplete(){ //code here }
But how can I correctly use playerprefs to remember that I finished the level? there is a variable that contains the number of what level it is in but I don't know if playerprefs has something special that can help me do this a lot easier than what I had in mind.
Thanks,
Answer by HarshadK · Oct 21, 2014 at 11:44 AM
Since you have a variable that stores the number of level currently on you can do something like:
int levelNumber;
void Start () {
PlayerPrefs.SetBool("Level"+levelNumber.ToString(), true);
PlayerPrefs.Save();
PlayerPrefs.GetBool("Level"+levelNumber.ToString());
}
Get the logic from above code snippet as to how you can set and get a level status using the level number you have.
Now you can set bool to be true if level is complete. And you can even check if the key exists for that level using PlayerPrefs.HasKey and if there is no key then level is not complete. But mind that PlayerPrefs are not at all secure to store level completion. User can very easily cheat.
Sorry for the late response,
If it's not all that secure what would playerprefs be useful for and what would you recommend me using to store level completion?
PlayerPrefs is useful for as the name suggests, to save player preferences like player character name or any such detail that is non-essential with respect to cheating, if user decides to cheat. Playerprefs are stored in plain text file that user can open and easily make changes to it. Now consider if player changes name of his player character, it is not gonna provide him with any special way to progress in game.
But if user can easily manipulate the level he has completed then he can proceed to next level easily without completing previous levels. This clearly fails the level unlock mechanism.
One better way is to store them in a binary file. This way it will be hard for user to modify this. At least it won't be easy for user to cheat. To add an extra layer of security you can encrypt the data before storing it in the file. Security of the data is a big field in itself. And the amount of security depends on your requirements and level of security required.
But in your case it would be enough to store them encrypted in a binary file. You can watch how to read write binary file in this training video: Persistence - Saving and Loading Data
Thanks a lot, I've looked at it and yes it does make sense this is a very nice way of saving my data and I will give it a try thanks a lot for your help I will keep the thread open for now because I might have more questions if you don't $$anonymous$$d.
Your answer
Follow this Question
Related Questions
Saving with PlayerPrefs? How it works? 1 Answer
Saving and loading game with PlayerPrefs 1 Answer
Having issues with PlayerPrefs... 2 Answers
Save Datasets at runtime 2 Answers