Search Unity

How to Save & Load Inventory

Discussion in 'Scripting' started by Kunalz, Jan 25, 2019.

  1. Kunalz

    Kunalz

    Joined:
    May 11, 2018
    Posts:
    25
    Hi, I just wanted to know if anyone can help with saving and loading inventory, I'm using ScriptableObjects and its just so confusing for me when it comes to saving inventory, I just can't seem to think my way around it. Saving int, string, float etc works fine but since inventory deals with slots, icon etc. I have no idea how to proceed with it.

    I've been stuck on this for 3-4 days now :c

    Link: to my project. (Its a small file just used for prototyping)
    https://mega.nz/#!X9MDlQxb!O_maBmSEX-XZLlPGkSKbFyexTpdOi_qamE156xB28rU

    EDIT: Theres also the codes I pasted below if you don't want to download the project.

    So if anyone can look at my codes and point me to the right direction, I would really appreciate it. Thanks
     
    Last edited: Jan 25, 2019
  2. qqqbbb

    qqqbbb

    Joined:
    Jan 13, 2016
    Posts:
    113
    Noone will bother downloading your project. Post your code.
     
  3. Kunalz

    Kunalz

    Joined:
    May 11, 2018
    Posts:
    25
    Lol alright, I just thought it would be a better way to show my issue but oh well heres the code.
     
  4. Kunalz

    Kunalz

    Joined:
    May 11, 2018
    Posts:
    25
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [CreateAssetMenu(fileName = "New Item", menuName = "Inventory/Item")]
    6. public class Item : ScriptableObject {
    7.  
    8.     public int itemID;
    9.     public string itemName = "New Item";
    10.     public string itemDescription = "New Description";
    11.     public Sprite icon;
    12.     public int price;
    13.  
    14.     public enum Type { Default, Consumable, Weapon}
    15.     public Type type = Type.Default;
    16.  
    17.     public Item(int itemid)
    18.     {
    19.         itemID = itemid;
    20.     }
    21. }
    22.  
    Code (CSharp):
    1. public class InventorySlot : MonoBehaviour {
    2.  
    3.     public Item item;
    4.     public GameObject icon;
    5.  
    6.  
    7.  
    8.     public void UpdateSlot()
    9.     {
    10.         if(item != null)
    11.         {
    12.             icon.GetComponent<Image>().sprite = item.icon;
    13.             icon.SetActive(true);
    14.         }
    15.         else
    16.         {
    17.             //otherwise deactive the icons
    18.             icon.SetActive(false);
    19.         }
    20.     }
    21.  
    22.  
    23. }
    Code (CSharp):
    1. public class Inventory : MonoBehaviour {
    2.  
    3.     public Item[] itemList = new Item[20];
    4.     public List<InventorySlot> inventorySlots = new List<InventorySlot>();
    5.  
    6.     private bool Add(Item item)
    7.     {
    8.         for (int i = 0; i < itemList.Length; i++)
    9.         {
    10.             if (itemList[i] == null)
    11.             {
    12.                 itemList[i] = item;
    13.                
    14.                 inventorySlots[i].item = item;
    15.                 return true;//end the function
    16.             }
    17.         }
    18.         return false; //if theres no space left
    19.     }
    20.  
    21.     public void UpdateSlotUI()
    22.     {
    23.         for (int i = 0; i < inventorySlots.Count; i++)
    24.         {
    25.             inventorySlots[i].UpdateSlot();
    26.         }
    27.     }
    28.  
    29.     void Start()
    30.     {
    31.        
    32.         UpdateSlotUI();
    33.        
    34.     }
    35.  
    36.  
    37.     public void AddItem(Item item)
    38.     {
    39.         bool hasAdded = Add(item);
    40.         if (hasAdded)
    41.         {
    42.             UpdateSlotUI();
    43.         }
    44.     }
    45. }

    Code (CSharp):
    1. public class GameSaveManager : MonoBehaviour {
    2.  
    3.     public Character character; // For save slot 1
    4.  
    5.  
    6.     public bool IsSaveFile()
    7.     {
    8.         return Directory.Exists(Application.persistentDataPath + "/game_save");
    9.     }
    10.  
    11.  
    12.     //SAVE & LOAD SLOT 1
    13.     public void SaveGame()
    14.     {
    15.         if (!IsSaveFile())
    16.         {
    17.             Directory.CreateDirectory(Application.persistentDataPath + "/game_save");
    18.         }
    19.         if (!Directory.Exists(Application.persistentDataPath + "/game_save/character_data"))
    20.         {
    21.             Directory.CreateDirectory(Application.persistentDataPath + "/game_save/character_data");
    22.         }
    23.         BinaryFormatter bf = new BinaryFormatter();
    24.         FileStream file = File.Create(Application.persistentDataPath + "/game_save/character_data/character_save");
    25.         var json = JsonUtility.ToJson(character);//pass in characters data
    26.         bf.Serialize(file, json);
    27.         file.Close();
    28.     }
    29.     public void LoadGame()
    30.     {
    31.         if (!Directory.Exists(Application.persistentDataPath + "/game_save/character_data"))
    32.         {
    33.             Directory.CreateDirectory(Application.persistentDataPath + "/game_save/character_data");
    34.         }
    35.         BinaryFormatter bf = new BinaryFormatter();
    36.         if (File.Exists(Application.persistentDataPath + "/game_save/character_data/character_save"))
    37.         {
    38.             FileStream file = File.Open(Application.persistentDataPath + "/game_save/character_data/character_save", FileMode.Open);
    39.             JsonUtility.FromJsonOverwrite((string)bf.Deserialize(file), character);//loading the character data
    40.             file.Close();
    41.         }
    42.     }
     
  5. qqqbbb

    qqqbbb

    Joined:
    Jan 13, 2016
    Posts:
    113
    You can save only serializable data. Watch some tutorial on serialization.
     
  6. Kunalz

    Kunalz

    Joined:
    May 11, 2018
    Posts:
    25
    My Character class which is also a scriptableobject seem to work fine, without putting the serializable. Which script are you talking about that needs the serializable data? Inventory?

    this is what my Character class looks like, pretty similar to Item
    Code (CSharp):
    1. [CreateAssetMenu]
    2. public class Character : ScriptableObject {
    3.  
    4.     public string playerName;
    5.     public int playerHealth = 100;
    6.     public int playerStrenghtStat = 1;
    7.     public int playerDefenceStat = 1;
    8.    
    9. }
     
  7. qqqbbb

    qqqbbb

    Joined:
    Jan 13, 2016
    Posts:
    113
    Scriptable objects are assets. You cant save them during runtime.
     
  8. Wut?

    Your inventory script is a standard class, if you want to serialize, put the [Serializeable] attribute on it and reference it in your character somehow. In theory it should be saved with your data. Or you can save it separately. The thing is, it's just data. So just dump it in a file. BTW, I, personally, wouldn't bother with JSON, just dump the serialize into a file, and deserialize from the file. Less CPU. (Although then you can't tweak your data in the file so easily)
     
    TimboInSpace and Kunalz like this.