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 Games: PlayerPrefs vs persistentDataPath?

Discussion in 'Scripting' started by frosted, Jun 22, 2014.

  1. frosted

    frosted

    Joined:
    Jan 17, 2014
    Posts:
    4,044
    Just curious what the best practice is for saving games while keeping maximum cross platformability (is that a word?)

    In general I've been using PlayerPrefs to store save game data, but I just watched a unity tech tutorial by a unity dev who on frowned on the usage of PlayerPrefs to save game data and preferred persistentDataPath.

    My question is, is persistentDataPath really reliable? Will it work on webplayer?

    What's really the best practice in terms of actually storing save game files if you want it to work everywhere?

    EDIT: Worth noting that this is Unity 4.5.1.
     
  2. Erisat

    Erisat

    Joined:
    Jan 31, 2013
    Posts:
    88
    I haven't used persistentdatapath personally, and it doesnt look like theres too much documentation about its usage. theres a post here where the guy describes it as being equivalent to the my documents folder, varying between different OS's. http://answers.unity3d.com/questions/209108/when-to-use-persistentdatapath-versus-datapath.html

    i do believe hes incorrect about saying that application.datapath is read only though, as thats where i save my data (cant do that on webplayer btw, atleast not in my case where i am using script to create new files in the directory)
    and I do it successfully, leading me to believe it's not read-only.

    For instance, Application datapath would be the location where the Game_Data folder and the Game.exe files would be found, but apparantely according to that answer, persistentdatapath would be the my documents folder (varying between os). I have not used playerprefs either so I can't answer questions about that. I successfully save all my data in regular datapath, but like I said I do not believe this would work in webplayer, It didn't work for me.

    Doesn't look like many questions about persistentdatapath get answered, through googling most questions i saw have no replies, and the ones that do generally have a reply similar to mine right here. "I'm not really sure, but I think it is blah"

    heres another post with a discussion about persistentdatapath http://answers.unity3d.com/questions/478626/is-it-possible-to-make-unity-initialize-files-in-t.html

    edit: correction: application datapath would be the path to WITHIN the Game_Data folder, NOT the directory above Game_Data which contains that and Game.exe

    edit2: side note, ive only tested used application.datapath in windows. I'm not sure if it behaves differently in different OS environments, but I imagine it would be equivalent
     
  3. frosted

    frosted

    Joined:
    Jan 17, 2014
    Posts:
    4,044
    Indeed there does seem to be a lot of disinformation about this - which is sort of the reason for this post. I've also seen a few posts that say that webplayer does not support persistentdatapath, which is a pretty major problem for me.

    I just wanted a definitive answer, I could run a test locally, but I'm not sure if this would potentially vary between platforms or browsers or whatnot, so having it work locally is no guarantee that it would work elsewhere.

    PlayerPrefs is only 1 meg total, so storing save game data there needs to be somewhat terse or compressed - so it'd be nice to have actual file system access. But does it reliably work?
     
  4. Erisat

    Erisat

    Joined:
    Jan 31, 2013
    Posts:
    88
    well i can confirm that using application.datapath to save files under windows IS reliable, I have not yet encountered an issue with using it in any way, all data i've saved there has been correct and reliable. I wish i could give more info about playerprefs or persistentdatapath, but like i said i havent used either so i would probably just end up feeding the misinformation beast. BTW, the method i use for saving files is by using this from the wiki : http://wiki.unity3d.com/index.php?title=Save_and_Load_from_XML

    but unfortunately i haven't gotten that to work with a webplayer build, it gives errors about a webplayer not being able to modify files on the local computer. Luckily for my situation the webplayer is just a client, i do my saving on the server side and send any needed data through rpc calls. I don't think that would apply to your situation though, as you didnt mention anything about networking.
    Sorry i can't be more help, just trying to throw in my experiences with saving data between runs

    edit: (although due to the nature of the error i got during using datapath in a webplayer, it would lead me to believe that persistentdatapath wouldnt work in webplayer either, as it would also be saving files to the local computer, but again, I have not used it so can not say for certain)
     
  5. frosted

    frosted

    Joined:
    Jan 17, 2014
    Posts:
    4,044
    Thanks for the insight.

    It really does seem unlikely that a browser plugin would allow direct io to local file system. I guess the assumption is that if you're going for a web player release you're probably going to write to server/cloud.

    Thx!
     
  6. Erisat

    Erisat

    Joined:
    Jan 31, 2013
    Posts:
    88
    I do know that playerprefs works with webplayer, but not sure about usage or limitations. thats all i know about it lol. hopefully someone else dives in this thread and offers some insight. I'd like to know also lol

    edit: since your going for a webplayer build, i assume you have a website, maybe you could figure a way to store your data on your web server and access it from the webplayer. just a hunch.
     
  7. landon912

    landon912

    Joined:
    Nov 8, 2011
    Posts:
    1,579
    The webplayer doesn't have access to System.IO. However, PlayerPrefs do work in the webplayer.

    I would personally suggest writing whatever data this is to a database on your web server. With some work you can make it secure and hack proof-ish.
     
  8. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    I save my save-game as JSon, which is string. On top, I save the assembly qualified named of the types saved. It's quite expensive, and I never get anywhere near 1Mb.

    1Mb of string is a LOT of string. What kind of data do you really want to save in there?
     
  9. Erisat

    Erisat

    Joined:
    Jan 31, 2013
    Posts:
    88
    LightStriker is right, 1MB IS alot of data, it sounds like less so i didnt think of checking before. but i checked out some of my file lengths, for instance I'm saving account data including everything from a uma text recipe string (quite long), inventory & equipment arrays, stats, login info, friends list, positions, and a large amount of other data. the file size of one account is 31kb of serialized xml data. So thinking about it, idk if i would ever reach that 1mb limit if i used playerprefs (if only saving data for one account that is, it would add up with hundreds of accounts :p) but you get what i mean. I think you'd be able to successfully save all the data you could possibly need within that 1MB limit easily. Unless your planning on saving something obnoxiously large.
     
  10. Sharp-Development

    Sharp-Development

    Joined:
    Nov 14, 2013
    Posts:
    353
    Considering serializing in Json or any other string format:

    1MB = 1024KB = 1.048.576Bytes
    Considering a normal ASCII encoding where one char equals to one byte, you can write the above amount to disk. Of course this is only theoretical, disregarding any header info etc. But yea, even with unicode encoding, it does leave you with 524288 characters. I guess thats a limit which will probably never be reached by normal player data.
     
  11. frosted

    frosted

    Joined:
    Jan 17, 2014
    Posts:
    4,044
    Unfortunately my game has an unusual amount of state. A worst case is probably around 500 fairly detailed characters per save game, in addition to a fair amount of supporting game state data. I'd like to allow for 3-5 save game slots. The character system is sort of like a mount & blade, or crusader kings. I might have to cut down those numbers significantly, but I'd like to keep them on the table for now.

    Most likely I'll try bson, which might be much smaller than raw json. If I run into problems I'll look into compression under unity - I'm assuming that the System.IO compression is off the table, but I'm sure I can find something.
     
  12. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    The player created or modified 500 characters in his game? Are you sure it's not a bit over designed?
     
  13. frosted

    frosted

    Joined:
    Jan 17, 2014
    Posts:
    4,044
    If you've ever played a game like Crusader Kings or Mount & Blade, it's sort of a similar character system to those games. As a rough idea, I'm targeting something in the range of 25 'teams' each with up to 20 guys. These are all AI driven - the AI acts at the 'team' level, so the processing isn't that intense - but I'd like to record results at the character level. This might have to be scaled down for one reason or another, but of all the reasons to scale this down or abstract some of the data, hitting a save game cap of 333k for 3 save slots would be one of the suckier ones.
     
  14. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Json is far more compact than xml, but you may want to consider using a binary format like BSON which will get you an even more compact file size, though it's not really "human readable" so it's a bit harder to troubleshoot.
     
  15. frosted

    frosted

    Joined:
    Jan 17, 2014
    Posts:
    4,044
    Yeah Dustin, that's the plan. I'm actually using your json .net implementation.

    As a side, you did really nice work on the port. Even the no default constructor parameter matching works as expected.
     
  16. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Awesome, glad to hear it. :) Don't hesitated to reach out if you need assistance.
     
  17. frosted

    frosted

    Joined:
    Jan 17, 2014
    Posts:
    4,044
  18. NoseKills

    NoseKills

    Joined:
    Jun 4, 2013
    Posts:
    25
    I had the same brain-fart at first too and thought that's what it means, but it just means that Application.persistentDataPath variable is read only :) i.e. you can't assign a new value to it and make your game save to another folder that way
     
  19. Nubz

    Nubz

    Joined:
    Sep 22, 2012
    Posts:
    553
    One problem with playerprefs in windows is that it goes to your registry.
    Which never sounded like a good idea to me.
    I think on a mac it goes to a folder somewhere not sure since I have never used one.
     
  20. Meltdown

    Meltdown

    Joined:
    Oct 13, 2010
    Posts:
    5,797
    PlayerPrefs has a 1MB limit per key, so not ideal.
    I would use PlayerPrefs only for local information storage such as screen resolution, graphics and audio settings.

    PersistentDataPath can be used for larger storage such as database files etc.

    If you're using WebPlayer, or even multiple platforms I'd suggest rather storing player information on a server and using www and JSON to retrieve it. This is the most secure way to store data and can stop players from hacking/cheating if you implement it correctly.
     
  21. BrentMichie

    BrentMichie

    Joined:
    Sep 10, 2014
    Posts:
    32
    Can someone please point me to a tutorial which shows how to send and receive simple data between web player and webpage. I have my own webpage. I have a web player. I don't know how to save and load information between. I've read a bunch of stuff and it all seems way over complicated for what I actually need.