Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question player prefs save loot index scenes

Discussion in 'Scripting' started by Pandausers, Apr 3, 2024.

  1. Pandausers

    Pandausers

    Joined:
    Feb 17, 2024
    Posts:
    36
    Hello everyone, is it possible to somehow store scene indices in 1 PlayerPrefs, for example, if the script is played in the first scene, then 1 is saved, and if the script is played in both scenes, then 1,2 is saved, is it possible to do this? Thank you very much in advance for your reply.
     
  2. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,491
    It's possible. A scene index is just an integer.

    But you really should save all gameplay data to a file in the user's Application.persistentDataPath instead. You can choose the file format, just as JSON or text or whatever you learned in C# class. The PlayerPrefs is really meant to be for users' preferences, not gameplay data. It's not very good at storing lists of things like you're describing. On Windows PlayerPrefs is stored in the registry, making it cumbersome to delete or clear out.
     
    spiney199 likes this.
  3. Pandausers

    Pandausers

    Joined:
    Feb 17, 2024
    Posts:
    36
    Understood, but I have a question if people can change my playerprefs saves on phones, and I also have a question what code is needed to do what I asked from the beginning.
     
  4. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,491
    Every byte of data on the player's phone is the player's data. You have to assume they can change it.
     
    Nad_B likes this.
  5. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,093
    As halley already mentioned every bit of data on a user's device can be assumed to be changed.
    If you're trying to prevent cheating, forget about it. Let them cheat when they want to. It requires 1 kid to create a script, spread it over the internet and everyone can cheat.
    If you really want to go against cheating you'd have to have all data stored external in a database. Only make calls through web API's to modify that data. Even then people can manipulate when those calls are made. You'd have to protect your web API as well. Anti-cheats are difficult and you shouldn't waste time on them. That time is better spent elsewhere.
     
  6. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,455
    You can do it with basic string manipulation. Just keep in mind that PlayerPrefs on some platforms is problematic at best and is never intended for more than settings. On WebGL you're locked to 1MB and on Windows it goes into the registry which also has tight limits.

    It's fine while you're learning but eventually you want to use files stored in
    Application.persistentDataPath
    or store the data on a server to protect it against players tampering with the data.

    Code (csharp):
    1. public static void AddSceneIndex(int sceneIndex)
    2. {
    3.     string storedIndices = PlayerPrefs.GetString("SceneIndices", "");
    4.     if (!storedIndices.Contains(sceneIndex.ToString()))
    5.     {
    6.         storedIndices = string.IsNullOrEmpty(storedIndices) ? sceneIndex.ToString() : storedIndices + "," + sceneIndex;
    7.         PlayerPrefs.SetString("SceneIndices", storedIndices);
    8.         PlayerPrefs.Save();
    9.     }
    10. }
    Code (csharp):
    1. public static List<int> GetStoredSceneIndices()
    2. {
    3.     List<int> sceneIndices = new List<int>();
    4.     string storedIndices = PlayerPrefs.GetString("SceneIndices", "");
    5.     foreach (string index in storedIndices.Split(','))
    6.     {
    7.         if (int.TryParse(index, out int sceneIndex))
    8.         {
    9.             sceneIndices.Add(sceneIndex);
    10.         }
    11.     }
    12.     return sceneIndices;
    13. }
     
  7. Pandausers

    Pandausers

    Joined:
    Feb 17, 2024
    Posts:
    36
    Thanks for the answer, I'm using playerprefs for now to understand how it all works, but in the future when I release a game I will consider more practical and secure methods of storing data.
     
  8. DA-VANCI

    DA-VANCI

    Joined:
    Jan 6, 2017
    Posts:
    4