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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How Do I Have PlayerPrefs Save a String to an Array?

Discussion in 'Scripting' started by Evil Square, Mar 23, 2015.

  1. Evil Square

    Evil Square

    Joined:
    Mar 21, 2015
    Posts:
    18
    Hi, all! I am a newbie indie developer/programmer, so I am sort of learning as I go with my first project. I am using UnityScript and have basic knowledge of JavaScript/UnityScript.

    When a player completes a level, I want their best time to be saved to an array, but when the player goes to the next level, I want there to be a separate best time to achieve, rather than the same one as in the previous level.

    I would think that I could get this to work using arrays, but I'm not that familiar with the use of arrays. Could someone please explain how I can create an array with either 50 or 100 items and have each item be equal to an individual level's PlayerPrefs.GetString ("Best Time") value?

    To be exact, I will have a different PlayerPrefs.SetString for each level and each one will be added to the scoreArray. So, for example, if you're on level 1 and your best time is 00:04:07, your best time for level 1 will be 00:04:07, but when you go to level 2 and you best time is different, it won't be that same 00:04:07 as in the last level. I hope this isn't too confusing. :p

    Many thanks,
    Chris
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
  3. Evil Square

    Evil Square

    Joined:
    Mar 21, 2015
    Posts:
    18
    Thanks for the quick reply!

    I have decided to not go on with the arrays, but I am still having difficulty with getting PlayerPrefs to save. When the player reaches the finish, if your current time is less than or equal to your quickest time, make the quickest time equal that of the current time. After that, it will save the new quickest time so that it will be there when you return to that level.

    Right now, after playing with PlayerPrefs for several hours, I have gotten as far as it updating the time before the level resets (there are 2 seconds after the player reaches the finish before the player is sent to the next level), but when you return to that level, the best time is reset.

    I want the best time to be 59:59:99 for the first time you are playing that level, but if you complete the level quicker than your best time, your old best time will be replaced by the current time at which you reached the finish at. Then, if you close the game down and reopen it, your new best time will still be there, instead of it defaulting back to 59:59:99.

    Can you give me a quick sample of code demonstrating how to do this instead of giving me a link to a page that looks really confusing? :p PlayerPrefs is hard enough for me without using it in arrays!

    -Chris
     
  4. TheValar

    TheValar

    Joined:
    Nov 12, 2012
    Posts:
    760
    It would be better if your current code for setting the best time at the start of the scene but you should do something like this.
    Code (CSharp):
    1. string bestTime = PlayerPrefs.getString("level2BestTime","59:59:99");
    "level2BestTime" you would replace with whatever id you give the playerpref when you save it.
    "59:59:59" is the default value that is returned if the playerpref you are getting doesn't exist.

    This way when you try to get the playerpref it will return the previously saved value if one exists or return the default value if not.

    Of course Eric is right that you should probably don't want to save time as a string.

    Additionally once you have the basics down I would recommend using the extended playerprefs Eric linked to so that you can store all the level scores in an array in playerprefs.
     
  5. Evil Square

    Evil Square

    Joined:
    Mar 21, 2015
    Posts:
    18
    Thanks so much! That is exactly what I needed! I spent two days trying to figure out what I did wrong, and all along it was that PlayerPrefs defaulted to "00:00:00" when there was no PlayerPrefs saved instead of "59:59:99". A little tricky devil this whole thing was; the solution hiding right in plain sight. :p