- Home /
Is PlayerPrefs.Save() necessary ? (My game saves without it)
Hi, what does PlayerPrefs.Save() do ? I'm asking this because I just use PlayerPrefs.SetInt() and PlayerPrefs.GetInt() and my game saves perfectly. Why should I use PlayerPrefs.Save() ?
Thanks in advance.
Answer by dsada · Jul 21, 2014 at 09:44 PM
Im quoting the documentation
"By default Unity writes preferences to disk on Application Quit. In case when the game crashes or otherwise prematuraly exits, you might want to write the PlayerPrefs at sensible 'checkpoints' in your game. This function will write to disk potentially causing a small hiccup, therefore it is not recommended to call during actual gameplay."
In other words it just only for security. Of corse it is not really acceptable if the game crashes but if it does better safe than sorry. So it is recommended to write it to the disk at certain points like pause,scene change, loading etc...
Thanks, I'm going to use it then, you never know what could happen :)
There are several acceptable reasons for a game to crash that you can do nothing about, and have no warning. An operating system force quit and power failure come to $$anonymous$$d.
These will happen at some point. Its much better if the player looses ten $$anonymous$$utes of progress, rather then three hours.
Answer by rutter · Jul 21, 2014 at 09:50 PM
PlayerPrefs keeps two copies of its data:
One copy is in memory (very fast, but also temporary)
One copy is on the hard drive (not as fast, but is permanent)
You can make as many changes as you want, but you'll want to periodically call Save to make sure those changes aren't lost in a crash.
Some people call Save
after every change. That's fine, if you don't have a lot of data.
Other people call Save
each time they switch scenes, or at some other sort of reasonable checkpoint.
The docs are saying Save
should be called automatically when your application exits. That's new to me, and I'm guessing it was added recently. Possibly in Unity 4.3
Thanks for the information ! I'm going to use it after every change as my game just saves an integer on Game Over, so I guess it's okay.