Hi!
So i`ve been struggling with this for one(!) week now. Been in every corner of the web it feels like but i cant get the hang of this!
I have a list of scriptable objects in my inventory that im trying to save so that the inventory stays the same every time someone closes and reopens the game.
I already have a binary formatter script(DataManagement) that i would like to save the inventory to. But im open to anything that works at this point.
I understand that i cant just add the list of items directly in my DataManagement script and that i may have to create another script/class that is serializable with the scriptable objects information or something like that.
But i dont know how…
This is my current Save/Load script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
public class DataManagement : MonoBehaviour
{
public static DataManagement dataManagement;
public List<Item> inventory;
public int currentScore;
public int highScore;
public int coinsCollected;
public int goldGemCollected;
public int redGemCollected;
public int greyGemCollected;
public int level = 1;
void Awake()
{
if (dataManagement == null)
{
DontDestroyOnLoad(gameObject);
dataManagement = this;
}
else if (dataManagement != this)
{
Destroy(gameObject);
Debug.LogWarning("More than ine instance of Database found!");
}
}
public void SaveData()
{
BinaryFormatter binForm = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/gameInfo.dat");
gameData data = new gameData();
data.highScore = highScore;
data.coinsCollected = coinsCollected;
data.goldGemCollected = goldGemCollected;
data.redGemCollected = redGemCollected;
data.greyGemCollected = greyGemCollected;
data.level = level;
binForm.Serialize(file, data);
file.Close();
}
public void LoadData()
{
if (File.Exists(Application.persistentDataPath + "/gameInfo.dat"))
{
BinaryFormatter binForm = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/gameInfo.dat", FileMode.Open);
gameData data = (gameData)binForm.Deserialize(file);
file.Close();
highScore = data.highScore;
coinsCollected = data.coinsCollected;
goldGemCollected = data.goldGemCollected;
redGemCollected = data.redGemCollected;
greyGemCollected = data.greyGemCollected;
level = data.level;
}
}
public void Delete()
{
File.Delete(Application.persistentDataPath + "/gameInfo.dat");
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.L))
{
SaveData();
Debug.Log("Saving");
}
}
}
[Serializable]
class gameData
{
public int highScore;
public int coinsCollected;
public int goldGemCollected;
public int redGemCollected;
public int greyGemCollected;
public int level;
}
This is my scriptable object script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(fileName = "New Item", menuName = "Inventory/Item")]
public class Item : ScriptableObject
{
new public string name = "New Item";
public Sprite icon = null;
public ItemType type;
public int id;
public virtual void Use()
{
Debug.Log("Using " + name);
}
public void RemoveFromInventory()
{
Inventory.instance.Remove(this);
}
}
and this is my inventory script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Inventory : MonoBehaviour
{
#region Singleton
public static Inventory instance;
private void Awake()
{
if (instance == null)
{
instance = this;
DontDestroyOnLoad(gameObject);
}
else if(instance != this)
{
Destroy(gameObject);
Debug.LogWarning("More than ine instance of Inventory found!");
}
}
#endregion
public delegate void OnItemChanged();
public OnItemChanged onItemChangedCallBack;
public int space = 20;
public List<Item> items = new List<Item>();
public bool Add (Item item)
{
if (items.Count >= space)
{
Debug.Log("Not enough room!");
return false;
}
items.Add(item);
if (onItemChangedCallBack != null)
onItemChangedCallBack.Invoke();
return true;
}
public void Remove (Item item)
{
items.Remove(item);
if (onItemChangedCallBack != null)
onItemChangedCallBack.Invoke();
}
}
I would be so grateful if somebody could help me with this so i can continue making my game!