can you define a default value when serializing your saves like you can with playerprefs?

I have two questions, first how do you reset/delete a serialized save so that you can test your game from start? is there an easy code you could put in and attach to a button during testing purposes that can delete it?

second I am wondering if you can set a default value to serialized saves so that if it loads it doesnt just default everything to 0 if there is nothing saved previously?

I am checking to see if there is the file there using persistantdatapath but i tested it with just 2 varliables before i wrote out the rest of them into the code. Now when i open a game it zeros out everything except those 2 variables when it loads.

thanks!

Slow down a bit. Serialising just describes the process of converting classes into binary data. You can serialise and deserialise however you like. Just write your own default values into your deserialization script.

Sereialisation doesn’t automatically imply how you are saving the data. To do a total reset files can be deleted or server records wiped.

Posting your code might help us offer more decent solutions.

okay, sorry. here is the code im using right now.

the save code well part of it i haven't added all the variables yet.. but it will show how im setting it up.

public void Save()
    {
        BinaryFormatter bf = new BinaryFormatter();
        FileStream file = File.Create(Application.persistentDataPath + "/playerInfo.dat");
        PlayerData data = new PlayerData ();

        data.fish = fish;
        data.fishPerClick = fishPerClick;
        data.fishJewels = fishJewels;
        data.fishJewelsChance = fishJewelsChance;
        data.cookedFish = cookedFish;
        data.logs = logs;
        data.loggingRank = loggingRank;
        data.maxLoggingLevel = maxLoggingLevel;
        data.requierdLoggingXP = requierdLoggingXP;
        data.currentLoggingXP = currentLoggingXP;
        data.logsPerTap = logsPerTap;
        data.coins = coins;
        data.cookingRank = cookingRank;
        data.requieredCookingXP = requieredCookingXP;
        data.maxCookingLevel = maxCookingLevel;
        data.currentCookingXP = currentCookingXP;
        data.cookingXP = cookingXP;
        data.currentExperience = currentExperience;
        data.fishingRank = fishingRank;
        data.requieredExperience = requieredExperience;
        data.maxFishingLevel = maxFishingLevel;
        data.baseCost = baseCost;
        data.cost = cost;
        data.clickPower = clickPower;
        data.count = count;
        data.baseCostA = baseCostA;
        data.costA = costA;
        data.clickPowerA = clickPowerA;
        data.countA = countA;
        data.baseCostB = baseCostB;
        data.costB = costB;
        data.clickPowerB = clickPowerB;
        data.countB = countB;
        data.baseCostC = baseCostC;
        data.costC = costC;
        data.clickPowerC = clickPowerC;
        data.countC = countC;
        data.baseCostD = baseCostD;
        data.costD = costD;
        data.clickPowerD = clickPowerD;
        data.countD = countD;
        data.baseCostE = baseCostE;
        data.costE = costE;
        data.clickPowerE = clickPowerE;
        data.countE = countE;
        data.baseCostF = baseCostF;
        data.costF = costF;
        data.clickPowerF = clickPowerF;
        data.countF = countF;
        data.baseCostG = baseCostG;
        data.costG = costG;
        data.clickPowerG = clickPowerG;
        data.countG = countG;
        data.costH = costH;
        data.clickPowerH = clickPowerH;
        data.countH = countH;
        data.costI = costI;
        data.clickPowerI = clickPowerI;
        data.countI = countI;
        data.costJ = costJ;
        data.clickPowerJ = clickPowerJ;
        data.countJ = countJ;
        data.costK = costK;
        data.clickPowerK = clickPowerK;
        data.countK = countK;



        bf.Serialize (file, data);
        file.Close ();
    }



loading one...



public void Load()
    {
        if (File.Exists (Application.persistentDataPath + "/playerInfo.dat"))
        {
            BinaryFormatter bf = new BinaryFormatter();
            FileStream file = File.Open(Application.persistentDataPath + "/playerInfo.dat", FileMode.Open);
            PlayerData data = (PlayerData)bf.Deserialize(file);
            file.Close ();

            fish = data.fish;
            fishPerClick = data.fishPerClick;
            fishJewels = data.fishJewels;
            fishJewelsChance = data.fishJewelsChance;
            cookedFish = data.cookedFish;
            logs = data.logs;
            loggingRank = data.loggingRank;
            maxLoggingLevel = data.maxLoggingLevel;
            requierdLoggingXP = data.requierdLoggingXP;
            logsPerTap = data.logsPerTap;
            coins = data.coins;
            cookingRank = data.cookingRank;
            requieredCookingXP = data.requieredCookingXP;
            maxCookingLevel = data.maxCookingLevel;
            currentCookingXP = data.currentCookingXP;
            cookingXP = data.cookingXP;
            currentExperience = data.currentExperience;
            fishingRank = data.fishingRank;
            requieredExperience = data.requieredExperience;
            maxFishingLevel = data.maxFishingLevel;
        }
    }
}


and the serializable class

[Serializable]
class PlayerData
{
    //fish related variables
    public float fish;
    public float fishPerClick;
    public float fishJewels;
    public float fishJewelsChance;
    public int cookedFish;


    //Log related variables
    public int logs;
    public int loggingRank;
    public int maxLoggingLevel;
    public float requierdLoggingXP;
    public float currentLoggingXP;
    public int logsPerTap;

    //coins
    public float coins;

    //cooking related variables
    public int cookingRank;
    public float requieredCookingXP;
    public int maxCookingLevel;
    public float currentCookingXP;
    public float cookingXP;

    //fishing levels and rank variables
    public float currentExperience;
    public int fishingRank;
    public float requieredExperience;
    public int maxFishingLevel;

    //power ups and upgrades
    public float baseCost;
    public float cost;
    public float clickPower;
    public float count;
    public float baseCostA;
    public float costA;
    public float clickPowerA;
    public float countA;
    public float baseCostB;
    public float costB;
    public float clickPowerB;
    public float countB;
    public float baseCostC;
    public float costC;
    public float clickPowerC;
    public float countC;
    public float baseCostD;
    public float costD;
    public float clickPowerD;
    public float countD;
    public float baseCostE;
    public float costE;
    public float clickPowerE;
    public float countE;
    public float baseCostF;
    public float costF;
    public float clickPowerF;
    public float countF;
    public float baseCostG;
    public float costG;
    public float clickPowerG;
    public float countG;
    public float baseCostH;
    public float costH;
    public float clickPowerH;
    public float countH;
    public float baseCostI;
    public float costI;
    public float clickPowerI;
    public float countI;
    public float baseCostJ;
    public float costJ;
    public float clickPowerJ;
    public float countJ;
    public float baseCostK;
    public float costK;
    public float clickPowerK;
    public float countK;

}

So its basically a case of doing something like this for each variable.

if (data.fish = 0) {
    fish = defaultFishValue;
} else {
    fish = data.fish;
}
2 Likes

ok cool, thanks. do you happen to know a way to set up a delete save that is at persistintdatapath code for testing purposes? where I could just hit the button and it whipes it or something like that? Or would i need to actually find out where that path saves to for a pc and locate the file and delete it each time i want to reset the save?

2 Likes