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

Playerprefs max size standalone

Discussion in 'Scripting' started by SmokyZebra, Apr 28, 2014.

  1. SmokyZebra

    SmokyZebra

    Joined:
    Mar 13, 2014
    Posts:
    139
    Hello, I know there's a limit of one mb for the webplayers games and i was wondering if there's a limit for the playerpref file size for windows standalone. It seems that not as it's not precised in unity doc.
     
  2. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    I may be mistaken, but I don't think that there is a hard set limit for windows/mac/mobile.

    Though if you are storing large amounts of data you might want to consider saving to an external data file within the persistent data path.

    For example:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.IO;
    4.  
    5. public class Foo {
    6.     public void WriteDataFile() {
    7.         string path = Path.Combine(Application.persistentDataPath, "YourDataFile.txt");
    8.         File.WriteAllText(path, "Hello World!");
    9.     }
    10. }
    11.  
     
  3. SmokyZebra

    SmokyZebra

    Joined:
    Mar 13, 2014
    Posts:
    139
    I don't know if that's really big, but i would need to save the state of 200+ planets with 100 variables each and the state of, for now 200 ships, also with around 100 variables each, and in the future, i plan to let the game generate up to 800/1000 planets and to have 8 playable race, so around 800 ships to save.So i have, at the moment, around 40000 variables to store and that would make around 180000 variables when the game will be finished. Wish should take i think max 10 mb, if each variables take 50 bit to be stored. But i don't know what space take a variable to be stored so it may take less or more space. I'm not there yet but i think that write the code to store 180000 variables in playerprefs isn't fun even if it will be mostly copy paste and change a number for each variable.
     
  4. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    For that kinda data I would personally opt to use JSON or XML...
     
  5. SmokyZebra

    SmokyZebra

    Joined:
    Mar 13, 2014
    Posts:
    139
    Trouble is that using JSON or XML seems really complicated compared to just store variables in playerprefs. I'm quite new to coding and i'm not sure that i would be able to use JSON or XML... Do you know any easy tutorials on the subject ?
     
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    Keep in mind that on Windows, PlayerPrefs is written to the registry. The registry is bad enough without writing megabytes of game data to it....

    Never ever do that, for any reason. ;) Any time you consider copy-pasting code, that means you should be using a loops/arrays as appropriate.

    --Eric
     
  7. SmokyZebra

    SmokyZebra

    Joined:
    Mar 13, 2014
    Posts:
    139
    How would that possible to make a loop to store values in PlayerPrefs ? Can the name of a Playerprefs be an int ? could i wrote something like :


    Code (csharp):
    1. function SaveGame(){
    2.      var counter : int = 0;
    3.      nameofPlayerPrefEntry : String;
    4.      while(counter < 10){
    5.           counter += 1;
    6.           nameofPlayerPrefEntry = "Name" + counter;
    7.           PlayerPrefs.SetInt(nameofPlayerPrefEntry, AScript.somevalue);
    8.      }
    9. }


    If this is possible, i will for sure make a loop instead of writing the same thing over and over
     
    Last edited: Apr 28, 2014
  8. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,731
    if
    Code (csharp):
    1. nameofPlayerPrefEntry = "Name" + counter;
    doesn't work then

    Code (csharp):
    1. nameofPlayerPrefEntry = "Name" + counter.ToString();
    will.
     
  9. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    As I mentioned, you need to be using arrays. That's really not optional...when you mention 180000 variables, I sincerely hope you didn't actually mean 180000 separate variables, but rather some arrays where the total number of entries would add up to 180000.

    @Munchy2007: Using ToString in that context is irrelevant since variables are already converted to strings when you do concatenation like that.

    --Eric
     
  10. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,731
  11. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    I'll second what Munchy linked, and the only thing I would change in his post would be changing "might be better off" to something like "have a better chance of not wanting to kill yourself down the road". It's an incredibly useful, powerful, and simple technique for saving and loading binary games, and Mike's an excellent teacher. Ditch PlayerPrefs, like now, and save yourself a LOT of headaches later on.
     
  12. SmokyZebra

    SmokyZebra

    Joined:
    Mar 13, 2014
    Posts:
    139
    Yes, it's indeed some arrays that I use, it's not 180000 separate variables. The final number of entries may not reach 180000, but it will for sure reach at least 100000 ( 200 planets x 100 values and 800 ships (100 max per race) x 100 values). I'll watch that tutorial hoping it's not too complicated to use binary reader/writer.
     
    Last edited: Apr 28, 2014
  13. SmokyZebra

    SmokyZebra

    Joined:
    Mar 13, 2014
    Posts:
    139
    Can one of u show me how to serialize and save a data in UnityScript ? Because the tutorial is for C# but my arrays are in JS scripts, wich I would need to save. And the trouble is that my arrays are javascript arrays, wich i've read it's not serializable. And what would be the benefit of using serialization if i can use Playerpref with some loops to save stuffs ? If it's for security, i don't need it, i don't mind player changing some values in PlayerPref.Also, i don't need the save system to be fast: I've read that saving 20000 ref to playerprefs take 8 seconds, so that would make 72 sec to save 180000 ref. That's only 1 min and 12 sec, And that would take only 40sec for 100000 ref, wich is the number of ref needeed for the most basic game, with 200 planets and max 800 ships. Wich is ok for my game : save would only occur if player click the save button, and as my game is turn based and quite "slow" and long, player would only hit save once per hour or each two hours. So again, why should I use serialization (wich is more complicated and would need tweaks to my scripts, changing my javascript arrays for build in arrays or list for example) when Playerpref can do what i need ? I don't need fast save nor securised data...
     
    Last edited: Apr 28, 2014
  14. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    It's always a bad idea to use Javascript arrays. Convert those to generic Lists or built-in arrays, depending on whether you resize them or not.

    --Eric
     
  15. SmokyZebra

    SmokyZebra

    Joined:
    Mar 13, 2014
    Posts:
    139
    Can you please show me how to declare a built-in array with values inside (let's say 10 values) in JS ? All i can find is how to declare an array without values inside... And do you have to put the line with import.Generic or something in each script that have access to built-in arrays or list or just in the script where you declare those arrays or list ?
     
  16. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    var foo = [1, 2, 3]; (Well, 3 values. ;) ) Or:

    Code (csharp):
    1. var foo = new int[3];
    2. foo[0] = 1;
    3. // etc.
    You don't need to import System.Collections.Generic if you're using built-in arrays.

    --Eric
     
  17. SmokyZebra

    SmokyZebra

    Joined:
    Mar 13, 2014
    Posts:
    139
    Thanks ! Can you (or someone else) show me how to serialize an array and to save it to a file in JS please ? And another trouble is that all my arrays are static and i've read that you can't serialize static stuffs... But i need those arrays to be static, else my planets and ships can't have acces easily to them. So maybe i would need to duplicate those arrays as non static and to fill them with the same values as the statics ones when saving.
     
  18. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    Don't make duplicate arrays...they don't need to be static, just public. Then you can use GetComponent to access them.

    --Eric
     
  19. SmokyZebra

    SmokyZebra

    Joined:
    Mar 13, 2014
    Posts:
    139
    So i gived a try to Playerprefs to save 180000 ref with loops and it's been 20 min now and it's still at 46%, not bugged but very slow. So I'll need to use serialization but I would need that someone gives me and example of how to serialize/Deserialize an array and how to save it to a file and how to read that file, and all that in JS. Please, that's really important because as my game is turn based and can be very long i really need to have a save system.
     
  20. SmokyZebra

    SmokyZebra

    Joined:
    Mar 13, 2014
    Posts:
    139
    Searching the web, i've found a tutorial on Utube that show how to save data to a txt file without serialization :

    https://www.youtube.com/watch?v=kxNpcB8UMgY&hd=1


    And it's really efficient, tried this method with a loop and the save system wrote 180000 entries in 27 secondes. And it may be able to be faster, as i was writing to disk 100 entries per frame, (my loop was in fact the update function with a condition and calling a function that was writing 100 entries each time, with a name based on a counter) i think that if i had 2 function sending data at the same time it would write 2 time faster. So I think i will give this method a try for my game instead of using serialization. The only weird thing is that the loop stopped at 1800 but only 1796 x 100 entries were writen to disk. I tried with 1810 loops of 100 and that time 1807 x 100 entries were saved, so i don't understand why the last 3 or 4 loop don't perform well. I've tried it with only 100 and 200 loops and it's the same, last 4 loop are doing counter +=1 but are not lauching save function, it seems. But if i put 205 it will run up to 200. So i guess i'llset up the loop for my game with a limit of planetsNumber + 5. The text file with the 180000 entry is only 3 mb large.


    edit : i found what was wrong, i forgot to put Mytext.Close(); and that's why it wasn't going to the end, now it's working, when i ask for 180000 entries i have them.
     
    Last edited: Apr 29, 2014
  21. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    It should be much faster than that. < 1 second.

    That's really not a good idea; you should write all the entries at once.

    --Eric
     
  22. SmokyZebra

    SmokyZebra

    Joined:
    Mar 13, 2014
    Posts:
    139
    Yeah, i tried to make a standard loop instead of using Update and the 180000 entries are saved in less than 1 second.