Save Inventory/Equipment with BinaryFormatter

Updated on October 12, 2017 in Answers
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
4 on October 10, 2017

Hi, my save system youse a BinaryFormatter wanting to add Brackeys inventory/equipment that is based on  Making RPG system. I can add the list of equipment in the inventory to my save system but can’t save it. At first, it told me to add [System.Serializable] to Item Equipment scripts. Now the error is SerializationException: Type UnityEngine.ScriptableObject in assembly UnityEngine, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null is not marked as serializable and its goes on more then that. 

  • Liked by
Reply
3 on October 11, 2017

I will try to look into the error more specifically tomorrow, but since I have already got into some errors before when using the binary formatter, I could as well give you an advice for an error I ran into before in the case it is the error you are getting. It is kind of a no-brainer, but make sure to delete or update your save file, since when you are changing the code, the binary formatter will always try to load it in the way the current version is written in, since you will change the code sometimes eventually or in your case it might already has happened, the file will be incompatible with the updated code. To fix this try to make your code backward compatible or delete it so your game can save the file again with the updated structure.

on October 11, 2017

Thank You, yes I delete the data everytime I add more to the code. here is save code. with just for the inventory stuff 

using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Collections.Generic;
using UnityEngine;
public class SaveSystem : MonoBehaviour {
public static SaveSystem saveSystem;
[Header("Inventory")]
public List<Item> items = new List<Item>();
 
void Awake () {
if (saveSystem != null) 
{
Debug.LogError ("More then one GameControllers in the scene");
} 
else 
{
saveSystem = this;
}
Load ();
}
 
public void Save()
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/playerInfo.data");
PlayerData data = new PlayerData();
data.items = items;
bf.Serialize(file, data);
file.Close();
Debug.Log("has been saved");
}
public void Load()
{
Debug.Log("has deen Laoding");
if (File.Exists(Application.persistentDataPath + "/playerInfo.data"))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open (Application.persistentDataPath + "/playerInfo.data", FileMode.Open);
PlayerData data = (PlayerData)bf.Deserialize (file);
file.Close();
items = data.items;
Debug.Log ("has deen Laoded");
}
}
public void Delete()
{
File.Delete(Application.persistentDataPath + "/playerInfo.data");
Debug.Log("has deen deleted");
items.Clear();
Debug.Log ("has staets has been reset");
}
}
[Serializable]
class PlayerData
{
public List<Item> items = new List<Item>();
}
//End of savestsyem start of Inventory;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class Inventory : MonoBehaviour {
#region Singleton
public static Inventory instances;
void Awake()
{
if(instances != null)
{
Debug.LogWarning("More then 1 inventory in scene");
return;
}
instances = this;
}
#endregion
public delegate void OnItemChanged();
public OnItemChanged onItemChangedCallback;
public int space = 20;
public List<Item> items = new List<Item>();
private void OnEnable()
{
items = SaveSystem.saveSystem.items;
}
public bool Add(Item item)
{
if (!item.isDefaltItem)
{
if(items.Count >= space)
{
Debug.Log("Not enough room");
return false;
}
items.Add(item);
SaveSystem.saveSystem.items.Add(item);
if (onItemChangedCallback != null)
onItemChangedCallback.Invoke();
}
return true;
}
public void Remove(Item item)
{
items.Remove(item);
SaveSystem.saveSystem.items.Remove(item);
if (onItemChangedCallback != null)
onItemChangedCallback.Invoke();
}

 

if you want to see all of the savesystem scripts https://pastebin.com/ZbbnXLiw

 

Helpful
on October 11, 2017

Interesting, I just tested it and defined Item and ShopStuff just as empty classes, since I do not have the original code for them and it doesn’t throw me any errors if I load or save, regardless of the times I load and save it.

on October 12, 2017

sorry here is the Item and Equipment code

using UnityEngine;
[System.Serializable]
[CreateAssetMenu(fileName = "New Item", menuName = "Inventory/Item")]
public class Item : ScriptableObject {
new public string name = "New Item";
public Sprite icon = null;
public bool isDefaltItem = false;
public virtual void Use()
{
//Use the item
//Something might do someing
Debug.Log("Using " + name);
}
public void RemovFromInventory()
{
Inventory.instances.Remove(this);
}
}
//End of Item start of the Equipment
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(fileName = "New Equipemnt", menuName = "Inventory/Equipemnt")]
[System.Serializable]
public class Equipemnt : Item {
public EquipemntSlot equipSlot;
public SkinnedMeshRenderer mesh;
public EquipemntMeshRegion[] coveredMeshRegions;
public int armorModifier;
public int damageModfiier;
public override void Use()
{
base.Use();
EquipemntManager.instance.Equip(this);
RemovFromInventory();
}
}
public enum EquipemntSlot { Head, Chest, Legs, Melee, Feet, range, Arrow }
public enum EquipemntMeshRegion { Legs, Arms, UpperArms, Toros};// for the body blendshapes

 

And the ShopStuff is  for my ShopSystem 

[System.Serializable]
public class ShopStuff {
public string itemName;
 public int mainID;
 public int sideID;
public string spriteName;
public int itemPrice;
 public bool bought;
 public Equipemnt equipemnt;
 public ItemTpye itemtpye;
}
 public enum ItemTpye { Head, Chest, Legs, Melee, Feet, range, Arrow }
Show more replies
  • Liked by
Reply
Cancel