Search Unity

Inventory Stack Problems.

Discussion in 'Scripting' started by Reaxgrim, Apr 7, 2015.

  1. Reaxgrim

    Reaxgrim

    Joined:
    Feb 15, 2014
    Posts:
    16
    Having a problem with an inventory script.

    am sure its something to do with reference and clone. but i have no idea how to sort it.
    been trying random things but nothing fixes it.

    basically when i get to the max stack size of 5 i want it to create a new stack at 1/5 but instead it keeps the 5/5 stack count when it makes the new one.

    code below any help would be great been at it all day heh.
    Sorry if its abit messy only been at this c# stuff a few days.

    INVENTORY Script
    Code (csharp):
    1.  
    2. public class Inventory : MonoBehaviour {
    3.  
    4.  
    5.     public List<GameObject> Slots = new List<GameObject>();
    6.     public List<Item> Items = new List<Item>();
    7.     public GameObject slots;
    8.     public GameObject toolTip;
    9.     public GameObject draggeditemgameobject;
    10.     public bool draggingItem = false;
    11.     public Item draggedItem;
    12.     public int indexofdraggeditem;
    13.  
    14.     ItemDatabase database;
    15.  
    16.  
    17.  
    18.     void Update()
    19.     {
    20.  
    21.  
    22.         if (Input.GetMouseButtonDown(1))
    23.         {
    24.             addItem(0);
    25.             Debug.Log("key pressed");
    26.         }
    27.  
    28.  
    29.         if (draggingItem)
    30.         {
    31.             Vector3 posi = (Input.mousePosition - GameObject.FindGameObjectWithTag("Canvas").GetComponent<RectTransform>().localPosition);
    32.             draggeditemgameobject.GetComponent<RectTransform>().localPosition = new Vector3(posi.x + 25, posi.y - 15, posi.z);
    33.         }
    34.  
    35.     }
    36.  
    37.  
    38.     public void showToolTip(Vector3 toolPos, Item item)
    39.     {
    40.         toolTip.SetActive(true);
    41.         toolTip.GetComponent<RectTransform>().localPosition = new Vector3(toolPos.x + 742, toolPos.y, toolPos.z);
    42.  
    43.         toolTip.transform.GetChild(0).GetComponent<Text>().text = item.itemName;
    44.         toolTip.transform.GetChild(1).GetComponent<Text>().text = item.itemDesc;
    45.  
    46.     }
    47.  
    48.     public void closeTooltip()
    49.     {
    50.         toolTip.SetActive(false);
    51.     }
    52.  
    53.     public void showDraggedItem(Item item, int slotNumber)
    54.     {
    55.         indexofdraggeditem = slotNumber;
    56.         closeTooltip();
    57.         draggeditemgameobject.SetActive(true);
    58.         draggingItem = true;
    59.         draggedItem = item;
    60.         draggeditemgameobject.GetComponent<Image>().sprite = item.itemIcon;
    61.     }
    62.  
    63.     public void closeDraggedItem ()
    64.     {
    65.         draggingItem = false;
    66.         draggeditemgameobject.SetActive(false);
    67.     }
    68.  
    69.     // Use this for initialization
    70.     void Start () {
    71.  
    72.         int slotAmount = 0;
    73.  
    74.         database = GameObject.FindGameObjectWithTag("ItemDatabase").GetComponent<ItemDatabase>();
    75.  
    76.         for (int i = 1; i < 7; i++)
    77.         {
    78.             for (int k = 1; k < 7; k++)
    79.             {
    80.                 GameObject slot = (GameObject)Instantiate(slots);
    81.                 slot.GetComponent<SlotScript>().slotNumber = slotAmount;
    82.                 Slots.Add(slot);
    83.                 Items.Add(new Item());
    84.                 slot.name = "slot" + i + "." + k;
    85.                 slot.transform.SetParent (this.gameObject.transform,true);
    86.                 slotAmount++;
    87.             }
    88.          
    89.  
    90.         }
    91.  
    92.         addItem(1);
    93.         addItem(0);
    94.         addItem(0);
    95.         addItem(0);
    96.         addItem(1);
    97.  
    98.     }
    99.  
    100.     void AddItemAtEmptySlot(Item item)
    101.     {
    102.         for (int k = 0; k < Items.Count; k++)
    103.         {
    104.             if (Items[k].itemName == null)
    105.             {
    106.                 Items[k] = item;
    107.                 break;
    108.             }
    109.        
    110.         }
    111.     }
    112.  
    113.     public void checkIfItemExist(int itemID, Item item)
    114.     {
    115.         for (int i = 0; i < Items.Count; i++)
    116.         {
    117.             if (Items[i].itemID == itemID && Items[i].itemValue != Items[i].itemMaxStack)
    118.             {
    119.                 Debug.Log("running if");
    120.                 Items[i].itemValue = Items[i].itemValue + 1;
    121.                 break;
    122.             }
    123.              
    124.  
    125.             else if (i == Items.Count - 1 && Items[i].itemValue == Items[i].itemMaxStack)
    126.             {
    127.                 Debug.Log("running else");
    128.  
    129.                 AddItemAtEmptySlot(item);
    130.             }
    131.         }
    132.     }
    133.  
    134.     void addItem(int id)
    135.     {
    136.         for (int k = 0; k < database.items.Count; k++)
    137.         {
    138.             if (database.items[k].itemID == id)
    139.             {
    140.                Item item = database.items[k];
    141.  
    142.                     checkIfItemExist(id, item);
    143.             }
    144.         }
    145.     }
    146.  
    ITEMDB SCRIPT
    Code (csharp):
    1.  
    2.     public List<Item> items = new List<Item>();
    3.  
    4.  
    5.     void Start ()
    6.     {
    7.                           //string name, int id, string desc, int power, int thirst, int hunger, int health, int value, ItemType type
    8.         items.Add(new Item ("Green Goo", 0, "Default Desc", 0, 0, 0, 0, 1, 5, Item.ItemType.Consumable));
    9.         items.Add(new Item ("Weak Dagger", 1, "A weak dagger, looks to have been well used.", 20, 0, 0, 0, 1, 1, Item.ItemType.Weapon));
    10.         items.Add(new Item ("c_Apple", 2, "A Rotten Apple", 0, 5, 10, 0, 1, 10, Item.ItemType.Consumable));
    11.         items.Add(new Item("c_Water", 3, "Water Bottle", 0, 10, 5, 0, 1, 20, Item.ItemType.Consumable));
    12.  
    13.     }
    14.  
    ITEM SCRITP
    Code (csharp):
    1.  
    2.  
    3. [System.Serializable]
    4. public class Item {
    5.  
    6.     public string itemName;
    7.     public int itemID;
    8.     public string itemDesc;
    9.     public Sprite itemIcon;
    10.     public int itemPower;
    11.     public int itemThirst;
    12.     public int itemHunger;
    13.     public int itemHealth;
    14.     public int itemValue;
    15.     public int itemMaxStack;
    16.     //public GameObject itemModel;
    17.     public ItemType itemType;
    18.  
    19.     public enum ItemType
    20.     {
    21.         Weapon,
    22.         Consumable,
    23.         Buildable,
    24.         Clothing
    25.     }
    26.  
    27.     public Item(string name, int id, string desc, int power, int thirst, int hunger, int health, int value, int maxstack, ItemType type)
    28.     {
    29.  
    30.         itemName = name;
    31.         itemID = id;
    32.         itemDesc = desc;
    33.         itemPower = power;
    34.         itemThirst = thirst;
    35.         itemHunger = hunger;
    36.         itemHealth = health;
    37.         itemType = type;
    38.         itemValue = value;
    39.         itemMaxStack = maxstack;
    40.         itemIcon = Resources.Load<Sprite>("" + name);
    41.        // itemModel = Resources.Load<GameObject>("DroppedItem");
    42.  
    43.  
    44.     }
    45.  
    46.  
    47.  
    48.     public Item()
    49.     {
    50.     }
    51.  
     
    Last edited: Apr 7, 2015
  2. Reaxgrim

    Reaxgrim

    Joined:
    Feb 15, 2014
    Posts:
    16
    nothing? :( lol
     
  3. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Yes it's a reference issue.

    You get the reference from your database and add it to your inventory. So the reference in your inventory still points to the database item. As soon as you add another item of the same type, you even adjust the count in the database.
    And when the stack is full, you add another reference of the same instance to the inventory. As it's the reference to the same instance, it'll show 5/5 logically.

    What you could need for your attempt is a copy-constructor or a method which works similarly and returns a complete new instance of 'Item'.
    Another approach would be to simply hold the ID + count in your slot and always lookup the item in the database, this way you wouldn't need duplicated objects of the item.

    As for your 'database', you could also use a dictionary with the ID being the key and the item being the value.

    Also, if you add the items in the correct order and not leaving out a single id, you can simply validate the ID with an if statement and if it's a valid index/id, directly access the index (just like you'd access an array item). That saves one loop.
     
  4. Reaxgrim

    Reaxgrim

    Joined:
    Feb 15, 2014
    Posts:
    16
    that went over my head so ill get back to looking for more vids on dictionary's etc heh