Search Unity

Scriptableobject Inventory System Clear OnDisable & Save

Discussion in 'Scripting' started by UDN_5c806b49-d8a0-4f67-a296-c12c91aa7396, Jan 16, 2020.

  1. UDN_5c806b49-d8a0-4f67-a296-c12c91aa7396

    UDN_5c806b49-d8a0-4f67-a296-c12c91aa7396

    Joined:
    Jan 9, 2017
    Posts:
    152
    Hi, I'm trying to get my inventory objects in the array to clear and then save to maybe an INI file or something. I was from Gamemaker Studio and I used INI. I'm not sure how to do it in Unity. The codes you're seeing here is me trying to take what worked in Gamemaker for me previously and port it into Unity. But... I'm not sure if this is the right way.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. [CreateAssetMenu(fileName = "New Inventory", menuName = "Inventory System/Inventory")]
    4. public class InventoryObject : ScriptableObject
    5. {
    6.     public int inventorySlots = 8;
    7.     public InventorySlot[] slot;
    8.  
    9.     public void OnEnable()
    10.     {
    11.         slot = new InventorySlot[inventorySlots];
    12.  
    13.         // TODO: Load from externally.
    14.     }
    15.  
    16.     public void OnDisable()
    17.     {
    18.         // TODO: Save externally.
    19.     }
    20.  
    21.     public void AddItem(ItemObject _item, int _amount)
    22.     {
    23.         for (int i = 0; i < slot.Length; i++)
    24.         {
    25.             if (slot[i].item == _item)
    26.             {
    27.                 slot[i].AddAmount(_amount);
    28.                 break;
    29.             }
    30.             else
    31.             {
    32.                 slot[i].AddItemAmount(_item, _amount);
    33.                 break;
    34.             }
    35.         }
    36.     }
    37. }
    38.  
    39. [System.Serializable]
    40. public class InventorySlot
    41. {
    42.     public ItemObject item;
    43.     public int amount;
    44.     public InventorySlot(ItemObject _item, int _amount)
    45.     {
    46.         item = _item;
    47.         amount = _amount;
    48.     }
    49.  
    50.     public void AddAmount(int value)
    51.     {
    52.         amount += value;
    53.     }
    54.  
    55.     public void AddItemAmount(ItemObject _item, int value)
    56.     {
    57.         item = _item;
    58.         amount += value;
    59.     }
    60. }
    61.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Generally ScriptableObjects are for predefined data that you create as separate asset instances in your project. You actually CAN use them for anything, but they do NOT get OnEnable/OnDisable calls the way you expect them to: the lifecycle of a ScriptableObject is subtly different from a Monobehavior.

    I think what you want is a Monobehavior and to put it in an inventory Scene (or on a Prefab) and make your Inventory that way. That way when you disable that object (or unload or destroy it), you will receive OnDisable() calls. I highly recommend using a Scene rather than a Prefab for something as complex as an inventory system, and then additively loading it, and then unloading it when dismissed.
     
  3. UDN_5c806b49-d8a0-4f67-a296-c12c91aa7396

    UDN_5c806b49-d8a0-4f67-a296-c12c91aa7396

    Joined:
    Jan 9, 2017
    Posts:
    152
    Thanks man! Ok, I'll try to change things up accordingly.