Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Save & load system : playerprefs can save to a .save file instead of registry ?

Discussion in 'Scripting' started by NRayta, Nov 12, 2021.

  1. NRayta

    NRayta

    Joined:
    Sep 6, 2021
    Posts:
    8
    Hi yall, I'm trying to create a save & load system that will
    1. save the spawned movable objects position, rotation, scale & whether or not it was deleted from the scene.
    2. let the user input the file name that he wants to save the file as and in the load menu give him the list of saved files and he can choose the one he wants to load.

    Now I was able to achieve the first requirement with playerprefs but for the second one I'm stuck. Is there a way to have such .save file instead of saving it to registry with playerprefs ?

    or if anyone knows any tutorial that satisfies both of my requirements please suggest it to me.Thankyou.
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,741
    PlayerPrefs is a very well-named class, in that it is useful for saving preferences. If you're trying to save anything else with PlayerPrefs, you're gonna have a miserable time. It really isn't at all designed for saving any data more complicated than a single value.

    There are many ways to save actual data structures to files rather than just one variable at a time. One of the most popular is JSON. Here's a JSON tutorial.

    Note for the future: That tutorial uses Unity's builtin JsonUtility, which is fine and certainly a good starting point (and leagues better than using PlayerPrefs). However, be advised that JsonUtility has some limitations, most notably in that it doesn't support a lot of common data structures such as Dictionary. If you start running into problems like that down the line, install the Newtonsoft Json.NET Unity package and use that instead. Using it is nearly identical to using JsonUtility, so you should be able to simply find/replace your JsonUtility code with the Newtonsoft code and you'll be alright.
     
    NRayta likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,769
    Adding onto @StarManta's excellent post above, there's a cool bridge you can use that takes you nearly seamlessly from PlayerPrefs over to a JSON-formatted save file.

    https://gist.github.com/kurtdekker/7db0500da01c3eb2a7ac8040198ce7f6

    You basically create one of these objects, associated with a particular filename, and use it exactly as if it was a PlayerPrefs. You can even make the created reference static so it's VERY similar in many ways.

    It's not an ideal save game system, but if you've gone a significant way down the PlayerPrefs hole already, it might be a handy bridge to get you out to writing to a file, and even having multiple different savefiles, all while only changing a little bit of your code.
     
  4. a1creator

    a1creator

    Joined:
    Jan 18, 2021
    Posts:
    23
    I cannot believe how simple this was to use. I luckily was smart enough to already collect all PlayerPrefs usage in a "GameSettings.cs" class that i reference everywhere else, so all I had to do was CMD + F in that script and change "PlayerPrefs." to the new variable name of the JsonPlayerPrefs class.
    Thank you, so much!
     
    Kurt-Dekker likes this.