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.
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.
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.
Every byte of data on the player’s phone is the player’s data. You have to assume they can change it.
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.
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.
GPT-4 Generated Code
public static void AddSceneIndex(int sceneIndex)
{
string storedIndices = PlayerPrefs.GetString("SceneIndices", "");
if (!storedIndices.Contains(sceneIndex.ToString()))
{
storedIndices = string.IsNullOrEmpty(storedIndices) ? sceneIndex.ToString() : storedIndices + "," + sceneIndex;
PlayerPrefs.SetString("SceneIndices", storedIndices);
PlayerPrefs.Save();
}
}
public static List<int> GetStoredSceneIndices()
{
List<int> sceneIndices = new List<int>();
string storedIndices = PlayerPrefs.GetString("SceneIndices", "");
foreach (string index in storedIndices.Split(','))
{
if (int.TryParse(index, out int sceneIndex))
{
sceneIndices.Add(sceneIndex);
}
}
return sceneIndices;
}
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.
You can find my assets store package, it can help you, you can store any data type using player prefs directly, and using a powerful encryption system too, so it’s not easy to hack it. you can store all your scene indexes like an array of integer or a list of integers.
AdvancedPlayerPrefs.SetArray(“SceneIndexs”, new int[ ]);