Should I use PlayerPrefs to save multiple variables relevant to a single element?

What I have:

I’m creating a World Builder.

With each World, I’ll need to save multiple variables associated with it.
Since PlayerPrefs don’t Get or Set arrays, I’ve coded a function to do that.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


namespace GeneratorTools
{
    public class Data : MonoBehaviour
    {
        public void Save(string FileType, string FileName, int[] IntArrayToSave, string[] StringArrayToSave)
        {
            PlayerPrefs.SetString(FileType + "-" + FileName + "-Name-", FileName);

            for (int i = 0; i < StringArrayToSave.Length; i++)
            {
                PlayerPrefs.SetString(FileType + "-" + FileName + "-Info-" + i, StringArrayToSave[i]);
            }

            for (int i = 0; i < IntArrayToSave.Length; i++)
            {
                PlayerPrefs.SetInt(FileType + "-" + FileName + "-Data-" + i, IntArrayToSave[i]);
            }

        }


        public void Load(string FileType, string FileName, out string WorldName, out int[] WorldData, out string[] WorldInfo)
        {

            int ArraySize = 0;

            for (int i = 0; PlayerPrefs.HasKey(FileType + "-" + FileName + "-Data-" + i); i++)
            {
                ArraySize++;
            }

            if (ArraySize == 0)
            {
                Debug.LogError("File doesn't exist.");
                WorldName = "Mundo Muito Genérico";
                WorldData = new int[3] { 2, 2, 2 };
                WorldInfo = new string[3] { "Médio", "Médio", "Moderada" };
                return;
            }

            WorldName = PlayerPrefs.GetString(FileType + "-" + FileName + "-Name-");
            WorldData = new int[ArraySize];
            WorldInfo = new string[ArraySize];

            for (int i = 0; i < ArraySize; i++)
            {
                WorldData[i] = PlayerPrefs.GetInt(FileType + "-" + FileName + "-Data-" + i);
                WorldInfo[i] = PlayerPrefs.GetString(FileType + "-" + FileName + "-Info-" + i);
            }
        }
      

    }
  

}

What I need:
A list of all Keys that start with a specific FileType, so I can populate a Scroll View and make a “list of save files”.

Another possible problem:
I’m not quite sure, but it seems counter-intuitive to save multiple things associated with the same element under many different keys.
In the above code, I’d have, for example:
WORLD-GenericWorld-Name
WORLD-GenericWorld-Data-0
WORLD-GenericWorld-Data-1
WORLD-GenericWorld-Data-2
WORLD-GenericWorld-Info-0
WORLD-GenericWorld-Info-1
WORLD-GenericWorld-Info-2

All associated with “Generic World”.

Possible solution:
It’d seem way more intuitive to me, and possibly more optimized, to have all of this under one single file named “GenericWorld”, with its Name on a line, Data on another line (with 0, 1 and 2 separated by commas), and Info on another line (separated by commas as well).

How can I do that?
Which file type/structure should I use to save this data?
This app will run mainly on Android and iOS, so I need something that works on both.
Should I use XML? Maybe JSON?
If so, can you point me to a tutorial on how to save/load data using the recommended file structure?

PlayerPrefs is a quick and dirty and passable-but-not-great solution to saving small bits of user data. The instant you’re looking at any complex data along the lines you’re describing, it’s immediately MUCH less appealing. Using class serialization of any kind will be much easier (once you learn how to do it) than doing anything by abusing PlayerPrefs. My usual go-to techniques are either LitJson (for JSON, good for anything that benefits from being human-readable or needs to be transmitted over a network), or binary files for optimal file sizes when storing larger amounts of data (here’s a classic tutorial for that). (Don’t bother with XML, XML was never good and has lousy tool support) Both JSON and the binary file techniques work very similarly and scale to hold arbitrary amounts of information with little to no additional effort.

1 Like

Whenever you want to use PlayerPrefs to store anything outside of simple game settings, you’re probably using the wrong tool even though technically it will work.

I’m personally a big fan of using CSV files for this kind of thing. Even though they are less flexible than JSON or XML, the format allows looking at more of your data on the same screen without scrolling, and they are easy to hand edit in a text editor or spreadsheet program. Downside is you’re probably writing your own script for handling CSV files, where JSON has several good libraries ready made.

1 Like

Thanks, guys!

I’ve watched a tutorial about making my own format with a binary formatter, and it’s working wonders.

If anyone sees this and has the same question, watch Brackeys’ tutorial on this.

1 Like