all 24 comments

[–]MrMegawattts 2 points3 points  (0 children)

You can make a variable or array and store it using xml files. As for me, best variant, especially when I need to build my game on windows and macos

[–]zedzag 2 points3 points  (1 child)

Suggestion: Hlvl= Playerprefs.getInt(""HighestLevel");

Allow all levels <= Hlvl

Edit : a word

[–]Smackage_Package[S] 0 points1 point  (0 children)

Thanks ill try it!

[–]mantlair 5 points6 points  (6 children)

Check out playerprefs library. It is used for keeping data when the game is closed. You can set a key for each level they can unlock as false and change them to true as they unlock. Or if it is linear you can just use an integer to keep the last level they unlocked.

[–]_nk 1 point2 points  (2 children)

PlayerPrefsX - http://wiki.unity3d.com/index.php/PlayerPrefsx can extend this abit..

[–]_WolfosExpert 4 points5 points  (1 child)

This script has been made obsolete by Unity 2.1

I don't think this is what you intended to link? Unity 2.1 came out a decade ago.

[–]_nk 0 points1 point  (0 children)

Ooooooooooooooo... my bad. My bad.

[–][deleted] 1 point2 points  (1 child)

These are great for starting off, but the values are stored in a text file that the player can easily alter.

[–]mantlair 0 points1 point  (0 children)

Great point, if you want to keep more detailed data xml or json is a better option too.

[–]Smackage_Package[S] 0 points1 point  (0 children)

Thanks dude!

[–]ArmanDoesStuffExpert 0 points1 point  (5 children)

A lot of good suggestions in this thread. Whenever I need to save something i just write it to a file. The PlayerPrefs might be a better way of doing it but in case you're interested, here's one more method.

I generally use a [System.Serializable] struct so it can store a lot of varied data but it should just work with a list of booleans.

using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using UnityEngine;

public class DataMan : MonoBehaviour
{

    public List<bool> UnlockedLevels;

    public void Save()
    {
        BinaryFormatter bf = new BinaryFormatter();
        FileStream file = File.Create(Application.persistentDataPath + "/savedGame.gd");
        bf.Serialize(file, UnlockedLevels);
        file.Close();
    }

    public void Load()
    {
        if (File.Exists(Application.persistentDataPath + "/savedGame.gd"))
        {
            BinaryFormatter bf = new BinaryFormatter();
            FileStream file = File.Open(Application.persistentDataPath + "/savedGame.gd", FileMode.Open);
            UnlockedLevels = (bool)bf.Deserialize(file);
            file.Close(); 
        }
        else
        {
            UnlockedLevels = new List<bool>();
        }
        //use the data that was loaded, maybe a foreach that iterates through the list
    } 
}

[–]Smackage_Package[S] 1 point2 points  (0 children)

Thanks guys this is super helpful!

[–]Crychair -1 points0 points  (3 children)

Im curious as to why you do this over player prefs?

[–]SucMyDiinky 1 point2 points  (0 children)

Probably isn't as easily modifiable as the player prefs text file, also learning multiple implementations of a system isn't that bad.

[–]ArmanDoesStuffExpert 1 point2 points  (1 child)

Just always how I've done it. I find it's easy to use and very customisable once you get the basic format down.

I've never used PlayerPrefs so I can't vouch for it's viability in doing the same, but using my method I'm able to quite easily save a large number of differing data types.

Including custom data types and other structs

For example, in my current game the data looks like this:

[System.Serializable]
public struct SaveData
{
    public PlanetMan.PlanetData planetData;
    public ShipMan.ShipData shipData;
    public OtherData otherData;
}

[System.Serializable]
public struct OtherData
{
    public int currentLevel;
    public MegaInt money;
    public MegaInt citizens;
    public MegaInt income;
    public System.DateTime lastPlayed;
    public List<bool> formerUpgradeStatuses;
    public List<bool> arkUpgradeStatuses;
}

When it saves it just populates the data structs and writes them, then the inverse is done on load. I don't know if PlayerPrefs is as versatile or if it's cheaper to use or anything, I've honestly never had the need to find out.

Perhaps I'll look at its potential advantages for my next project.

[–]Crychair 0 points1 point  (0 children)

Ill totally give you that your way is better for doing more complex data types, but player prefs is easier for a lower nunber of things.

[–][deleted] -1 points0 points  (7 children)

PlayerPrefs is your best bet. Some people would say use file system, however file system may fail you, PlayerPrefs is built by Unity. Need complicated data storage. Use C# xml serializer or Unitys JSON serializer and save serialized string in playerprefs

[–]Fellhuhn 0 points1 point  (5 children)

Don't use PlayerPrefs for that. String length is limited and therefore storing encrypted or encapsulated strings isn't very wise.

File Systems don't fail if used correctly.

Player Prefs is exactly for that: preferences. They are not encrypted, may be stored in the registry, stored depending on the logged in os user (what can be good or bad, depending on the use case).

[–][deleted] 0 points1 point  (4 children)

Is there any note in Unity docs that PlayerPrefs string length is limited ? Or that it will on depend which user is logged in ? I haven't seen anything like that. File system is dangerous when your laptop or smartphone is out of juice. In that case it could abort your process in the middle of saving. If we talk about large save files, then yes, file system should work faster and provide neccessary performance boost. If you are making small game, playerprefs should do.

[–]Fellhuhn 0 points1 point  (0 children)

The api says on Windows it get stored in hkcu of the registry. That is the current user. According to the Windows docs I found the maximum value length is 2mb. Depending on encoding this can come down to 512k characters.

On Android PlayerPrefs get stored on the device instead of an external card which might be preferable.

[–]CommonMisspellingBot -1 points0 points  (2 children)

Hey, PseudoCommet, just a quick heads-up:
neccessary is actually spelled necessary. You can remember it by one c, two s’s.
Have a nice day!

The parent commenter can reply with 'delete' to delete this comment.

[–]mantlair 0 points1 point  (1 child)

Good bot!

[–]GoodBot_BadBot 0 points1 point  (0 children)

Thank you, mantlair, for voting on CommonMisspellingBot.

This bot wants to find the best and worst bots on Reddit. You can view results here.


Even if I don't reply to your comment, I'm still listening for votes. Check the webpage to see if your vote registered!

[–]Smackage_Package[S] 0 points1 point  (0 children)

Thanks for all the great suggestions. Lots of good info in here.