Saving Collectibles That Have Been Picked Up

Hi guys,
I bought Easy Save from the asset store to help me but I still don’t get how to achieve this.

The situation:
I’m making a 2D platformer that has several levels (50 in this case). On each level I have collectible items (coins, health pick ups etc).

What I need to do:
I need to save the level progress (collectibles that have been picked up) so these items don’t re-appear if the player changes scene then comes back to the level in question. I also want the player to able to save his game and come back to it in the same state it was before (without the coins and other collectibles that were already picked up.

Done so far:
I can save the score, high-score, player position using Easy Save. No problem there.

I have no idea how to do this and all research that i’ve done leads to using playerPrefs which I don’t want to do for safety reasons or really complicated solutions that I don’t understand.

Any ideas or script that you could share would be greatly appreciated.

Thanks
Chris

How i do it in my game. A public static data save class.

using System.Collections.Generic;
[System.Serializable]
public class Save
{
    //parrot the needed values from the data obj
 
    public List<List<int>> AllColors = new List<List<int>>();
    public int[] LvlKeys;
}

Generally Idont populate this save class until its time to save. My data is stored in a scriptable object during play.

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

[CreateAssetMenu(fileName = "Data", menuName = "Persistant Data")]
public class Game_Data : ScriptableObject
{
    public Sprite[] sprites;
    public int[] LevelKeys;
}

This I use this big boy to do the saving… a little bit of extra work to get the color saving working but… idk a better way. Maybe it will help.

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

public class Read_Write : MonoBehaviour
{
    public bool writeAllSprites;
    public string DataFileName;

    string dataFilePath;
    Default_Settings Defaults;

    private void Awake()
    {
        Defaults = GetComponent<Default_Settings>();
        DefaultSetup();

        dataFilePath = Application.persistentDataPath + "/" + DataFileName + ".dat";
        if (File.Exists(dataFilePath)) { LoadFile(); }
     
    }

    private void DefaultSetup(){
        StaticData.AllSprites = Defaults.DefaultAllSprites;
        StaticData.colors = Defaults.DefualtColors;
        StaticData.LevelKeys = Defaults.DefaultLvlKey;


    }

    private void Start()
    {
     
     
    }

    //================Sprites==================

    public void WriteSpritetoPNG(Sprite image)
    {
        var Bytes = image.texture.EncodeToPNG();
        File.WriteAllBytes(Application.persistentDataPath + "/" + image.name + ".png", Bytes);
    }

    //================Files================

    public void SaveFile()
    {
        Save s = CreateSaveGameObject();
        BinaryFormatter bf = new BinaryFormatter();
        FileStream file = File.Create(dataFilePath);
        bf.Serialize(file, s);
        file.Close();

    }

    private Save CreateSaveGameObject()
    {
        Save s = new Save();

        //set save from data
        s.AllColors = Color32ArraytoList(StaticData.colors);
        s.LvlKeys = StaticData.LevelKeys;
        return s;
    }

    public void LoadFile()
    {
        if (File.Exists(dataFilePath))
        {
            BinaryFormatter bf = new BinaryFormatter();
            FileStream file = File.Open(dataFilePath, FileMode.Open);
            Save save = (Save)bf.Deserialize(file);


            file.Close();

            //set data from save file
            StaticData.LevelKeys = save.LvlKeys;
            StaticData.colors = ListtoColor32Array(save.AllColors);
        }
    }




    //------Color to lists and lists to color

    //Load
    private Color32 ListtoColor32(List<int> ListIN)
    {
        return new Color32((byte)ListIN[0], (byte)ListIN[1], (byte)ListIN[2], (byte)ListIN[3]);
    }

    private Color32[] ListtoColor32Array(List<List<int>> SavedColorList)
    {
        Color32[] DataColors = new Color32[SavedColorList.Count];

        int count = 0;
        foreach (List<int> savedColor in SavedColorList )
        {
            DataColors[count] = ListtoColor32(savedColor);
            count++;
        }
        return DataColors;


    }


    //Save
    private List<int> Color32toList(Color32 colorIN)
    {
        List<int> ListOUT = new List<int>();
        ListOUT.Add(colorIN.r);
        ListOUT.Add(colorIN.g);
        ListOUT.Add(colorIN.b);
        ListOUT.Add(colorIN.a);
        return ListOUT;
    }

    private List<List<int>> Color32ArraytoList(Color32[] ColorArray){
        List<List<int>> ListOcolors = new List<List<int>>();

        int count = 0;

        foreach(Color dataColor in ColorArray){
            ListOcolors.Add (Color32toList(ColorArray[count]));
            count++;
        }
        return ListOcolors;

    }
}

Everything below //------Color to lists and lists to color is just for color stuff you can ignore that

Thanks! I’ll give that a shot :slight_smile: