Search Unity

Question About PlayerPrefs

Discussion in 'Scripting' started by NowakSotto, Sep 1, 2019.

  1. NowakSotto

    NowakSotto

    Joined:
    Aug 26, 2019
    Posts:
    10
    Im reading in the documentation about it.

    "Stores and accesses player preferences between game sessions."

    And come to my mind, the "playerprefs" is the only way to create "saves" in the games?

    Thanks
     
  2. Dextozz

    Dextozz

    Joined:
    Apr 8, 2018
    Posts:
    493
    No, but it's the easiest way.

    Here's one way without player prefs:
     
  3. NowakSotto

    NowakSotto

    Joined:
    Aug 26, 2019
    Posts:
    10
    Thanks man.

    Saying it is the easy way is good, because really, i liked the fast way to make it.
    But i can use it in any game i want or for some style of games its not recommended?
     
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,336
    Playerprefs has some downsides:
    - It's hard to create different save files. If you're saving eg. the number of lives the player has, you can't just save "Health" if you want to have a bunch of saves. So you'll have to build some system around that.
    - You have no ability to have a save "file". For testing and debugging, it's usually usefull to look at the entire save file, and look at what values it has, and change it or swap it completely.
    - It won't be possible to use it on some platforms - like consoles and perhaps not on some phones? Unsure about the phones.
     
  5. NowakSotto

    NowakSotto

    Joined:
    Aug 26, 2019
    Posts:
    10
    And in the video the guy said is easy to change the data of "playerprefs".

    So there is one way to save, have others?
     
  6. Nanior

    Nanior

    Joined:
    May 4, 2019
    Posts:
    101
    For playerprefs you can use SetInt and more, you can let it setfloat x y z when you quit and when you start, get your float x y z and paste the float x y z into your transform.position
     
  7. Nanior

    Nanior

    Joined:
    May 4, 2019
    Posts:
    101
  8. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    Playerprefs is generally designed for exactly what it sounds like. Prefs.

    So, sound level. Gamma setting. Subtitles on or off. Those sorts of things are what I would suggest using it for.

    On some systems it's pretty easy to modify these values outside of the app, which can be fine if you don't mind that. I also just don't like saving huge amounts of data to playerprefs, which may or may not be the case for a save file.

    Personally, I prefer a save file. You can save out to plain text using json or just a text file if you want and allow the player to modify the save file or you can add some encryption if you'd rather they didn't easily modify it.

    There are plenty of assets free on the store that will help you. Here is one example https://assetstore.unity.com/packages/tools/input-management/save-game-110382 that claims to work similar to playerprefs, but using a save file and with the option to do encryption.

    Edited post to clarify differences.
     
    Last edited: Sep 5, 2019
  9. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,336
    Why?

    I don't get this thing where everyone wants to encrypt their save files. That doesn't gain you anything! If the player wants to dig in the save file, let them!

    If you're making a multiplayer game where cheating is a problem, then you don't save stuff on the player's computer. If you're not making a multiplayer game, cheating isn't a problem. Stop making your users files illegible because you want to be in charge of how they play your game.

    Gah
     
    Joe-Censored likes this.
  10. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    It's true, in both cases it's a personal opinion. I guess in both cases (you and I) we should clarify what the options are and not make it seem like we're trying to force our opinion on other developers.

    So, to clarify, you can leave a save file as plain text where a user could modify it or you can encrypt it making it harder to be modified.

    In the end, each developer is free to make their own choice on what they go with.
     
    Baste likes this.
  11. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    I mostly agree with you, but some single player games include online achievements, leaderboards, and microtransactions. Because requiring the user to be online to play a single player game is not popular (see the PR disaster following the release of SimCity 2013), encrypting local saves is probably the only way to go even though it will not be 100% effective.