Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Chest Inventory Help!

Discussion in 'Scripting' started by Deanford, Apr 3, 2020.

  1. Deanford

    Deanford

    Joined:
    Oct 17, 2014
    Posts:
    93
    Hi all,

    I want to add a List in the variables in my chest script that I can drag my Items (scriptable objects) into, so I can choose what is in the chest.

    How would I go about adding the items in the list to the slots?

    Help would be appreciated!

    Here is the chest inventory code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using TMPro;
    6.  
    7. public class ChestInventory : MonoBehaviour
    8. {
    9.     public List<GameObject> slots = new List<GameObject>();
    10.  
    11.     public List<Item> itemsInChest = new List<Item>(); //<<-------- VARIABLE FOR SCRIPTABLE OBJECTS
    12.  
    13.     [HideInInspector]
    14.     public bool isDragging = false;
    15.     public Image draggingImage = null;
    16.     [HideInInspector]
    17.     public Item draggingItem;
    18.     [HideInInspector]
    19.     public int draggingAmount;
    20.  
    21.     public GameObject tooltip;
    22.     public TextMeshProUGUI tooltipText;
    23.  
    24.     public GameObject chestUI;
    25.  
    26.     private void Start()
    27.     {
    28.         chestUI.gameObject.SetActive(false);
    29.     }
    30.  
    31.     public bool AddItem(Item itemToAdd, int amount)
    32.     {
    33.         Slot emptySlot = null;
    34.  
    35.         for (int i = 0; i < slots.Count; i++)
    36.         {
    37.             Slot currentSlot = slots[i].GetComponent<Slot>();
    38.  
    39.             if (currentSlot.myItem == itemToAdd && itemToAdd.isStackable && currentSlot.myAmount + amount <= itemToAdd.maxStackAmount)
    40.             {
    41.                 currentSlot.AddItem(itemToAdd, amount);
    42.                 return true;
    43.             }
    44.             else if (currentSlot.myItem == null && emptySlot == null)
    45.             {
    46.                 emptySlot = currentSlot;
    47.             }
    48.         }
    49.  
    50.         if (emptySlot != null)
    51.         {
    52.             emptySlot.AddItem(itemToAdd, amount);
    53.             return true;
    54.         }
    55.         else
    56.         {
    57.             print("Chest inventory is full!");
    58.             return false;
    59.         }
    60.     }
    61.  
    62.     public void RemoveItem(Item itemToRemove, int amount)
    63.     {
    64.         for (int i = 0; i < slots.Count; i++)
    65.         {
    66.             Slot currentSlot = slots[i].GetComponent<Slot>();
    67.  
    68.             if (currentSlot.myItem == itemToRemove)
    69.             {
    70.                 currentSlot.RemoveItem(amount);
    71.             }
    72.         }
    73.     }
    74.  
    75.     //---------------------------------------Crafting Helpers------------------------------------------
    76.  
    77.     public bool HasItem(string lookupItem, int amount)
    78.     {
    79.         for (int i = 0; i < slots.Count; i++)
    80.         {
    81.             if (slots[i].GetComponent<Slot>().myItem != null)
    82.             {
    83.                 if (slots[i].GetComponent<Slot>().myItem.itemName == lookupItem && slots[i].GetComponent<Slot>().myAmount >= amount)
    84.                 {
    85.                     return true;
    86.                 }
    87.             }
    88.         }
    89.  
    90.         return false;
    91.     }
    92.  
    93.     //---------------------------------------Drag Drop Helpers------------------------------------------
    94.  
    95.     public void DoDrag(Item itemToDrag, int amnt)
    96.     {
    97.         draggingItem = itemToDrag;
    98.  
    99.         isDragging = true;
    100.         draggingImage.enabled = true;
    101.         draggingImage.sprite = draggingItem.itemIcon;
    102.         draggingAmount = amnt;
    103.     }
    104.  
    105.     public void EndDrag()
    106.     {
    107.         draggingItem = null;
    108.         isDragging = false;
    109.         draggingImage.enabled = false;
    110.         draggingAmount = 0;
    111.     }
    112.  
    113.     //---------------------------------------Inventory Tooltip------------------------------------------
    114.  
    115.     public void ShowTooltip(Item tooltipItem)
    116.     {
    117.         tooltip.SetActive(true);
    118.  
    119.         if (tooltipItem.itemType == Item.ItemType.Weapon || tooltipItem.itemType == Item.ItemType.Tool)
    120.         {
    121.             tooltipText.text = tooltipItem.itemName + "\n" +
    122.             tooltipItem.description + "\n" +
    123.             "+" + tooltipItem.attackDamage + " Attack Damage" +"\n" + "\n" +
    124.             tooltipItem.itemType + "\n" +
    125.             tooltipItem.currentDurability + "/" + tooltipItem.maxDurability;
    126.         }
    127.         else
    128.         {
    129.             tooltipText.text =
    130.             tooltipItem.itemName + "\n" +
    131.             tooltipItem.description + "\n" + "\n" +
    132.             tooltipItem.itemType;
    133.         }
    134.      
    135.     }
    136.  
    137.     public void HideTooltip()
    138.     {
    139.         tooltip.SetActive(false);
    140.     }
    141. }
    142.  


    Chest slot code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using TMPro;
    6. using UnityEngine.EventSystems;
    7.  
    8. public class ChestSlot : MonoBehaviour, IBeginDragHandler, IDragHandler, IDropHandler, IPointerEnterHandler, IPointerExitHandler
    9. {
    10.     ChestInventory chestInventory;
    11.     Image myImage;
    12.     TextMeshProUGUI myText;
    13.     Slider durabilityBar;
    14.  
    15.     public Item myItem;
    16.     public int myAmount;
    17.  
    18.     void Start()
    19.     {
    20.         chestInventory = GameObject.FindObjectOfType<ChestInventory>();
    21.         myImage = transform.GetChild(0).GetComponent<Image>();
    22.         myText = transform.GetChild(1).GetComponent<TextMeshProUGUI>();
    23.         durabilityBar = transform.GetChild(2).GetComponent<Slider>();
    24.  
    25.         ShowUI();
    26.     }
    27.  
    28.     public void AddItem(Item itemToAdd, int amount)
    29.     {
    30.         if (itemToAdd == myItem)
    31.         {
    32.             myAmount += amount;
    33.         }
    34.         else
    35.         {
    36.             myItem = itemToAdd;
    37.             myAmount = amount;
    38.         }
    39.  
    40.         ShowUI();
    41.     }
    42.  
    43.     public void RemoveItem(int amount)
    44.     {
    45.         if (myItem != null)
    46.         {
    47.             myAmount -= amount;
    48.  
    49.             if (myAmount <= 0)
    50.             {
    51.                 myItem = null;
    52.             }
    53.         }
    54.  
    55.         ShowUI();
    56.     }
    57.  
    58.     void ShowUI()
    59.     {
    60.         if (myItem != null)
    61.         {
    62.             //If we have an item in the inventory
    63.             myImage.enabled = true;
    64.             myText.enabled = true;
    65.             myImage.sprite = myItem.itemIcon;
    66.             myText.text = myAmount.ToString();
    67.  
    68.             durabilityBar.value = myItem.currentDurability;
    69.             durabilityBar.maxValue = myItem.maxDurability;
    70.             durabilityBar.gameObject.SetActive(false);
    71.  
    72.             if (myItem.itemType == Item.ItemType.Weapon)
    73.             {
    74.                 myText.enabled = false;
    75.             }
    76.  
    77.             //If we have and item with Durability... AND the durability is at full health
    78.             if (myItem.useDurability)
    79.             {
    80.                 durabilityBar.gameObject.SetActive(true);
    81.  
    82.                 if (durabilityBar.value == myItem.maxDurability)
    83.                 {
    84.                     //Then we disable the bar until durability is less than the max amount
    85.                     durabilityBar.gameObject.SetActive(false);
    86.                 }
    87.             }
    88.             else
    89.             {
    90.                 durabilityBar.gameObject.SetActive(false);
    91.             }
    92.         }
    93.         else
    94.         {
    95.             //If we dont have an item in the inventory
    96.             myImage.enabled = false;
    97.             myText.enabled = false;
    98.             durabilityBar.gameObject.SetActive(false);
    99.         }
    100.     }
    101.  
    102.     //--------------------------------------Drag and Drop stuff---------------------------
    103.  
    104.     public void OnBeginDrag(PointerEventData eventData)
    105.     {
    106.         if (myItem != null)
    107.         {
    108.             chestInventory.DoDrag(myItem, myAmount);
    109.             RemoveItem(myAmount);
    110.         }
    111.     }
    112.  
    113.     public void OnDrag(PointerEventData eventData)
    114.     {
    115.         chestInventory.draggingImage.transform.position = Input.mousePosition;
    116.     }
    117.  
    118.     public void OnDrop(PointerEventData eventData)
    119.     {
    120.         AddItem(chestInventory.draggingItem, chestInventory.draggingAmount);
    121.         chestInventory.EndDrag();
    122.     }
    123.  
    124.  
    125.     //---------------------------------Mouse Over Stuff------------------
    126.  
    127.     public void OnPointerEnter(PointerEventData eventData)
    128.     {
    129.         if (myItem != null)
    130.         {
    131.             chestInventory.ShowTooltip(myItem);
    132.         }
    133.     }
    134.  
    135.     public void OnPointerExit(PointerEventData eventData)
    136.     {
    137.         chestInventory.HideTooltip();
    138.     }
    139. }
    140.  
     
  2. jamiljawhar2001

    jamiljawhar2001

    Joined:
    Jan 1, 2020
    Posts:
    3
    Hey my friend, I would prefer to go through Brackys RPG inventory system. There you might get a waypoint.