Forum begins after the advertisement:


[Part 9] Moving the inventory item bug

Home Forums Video Game Tutorial Series Creating a Farming RPG in Unity [Part 9] Moving the inventory item bug

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #12069
    Sand Man
    Former Patron

    Hello, i’m doing part 9 of the farming RPG and i stumble upon this bug. When i tried to equip tools and item, the tools and item didn’t equip in the hand.

    here are my InventoryManager

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    
    public class InventoryManager : MonoBehaviour
    {
        public static InventoryManager Instance { get; private set; }
    
        private void Awake()
        {
            if (Instance != null && Instance != this)
            {
                Destroy(this);
            }
            else
            {
                Instance = this;
            }
        }
    
        [Header("Tools")]
        [SerializeField]
        private ItemSlotData[] toolSlots = new ItemSlotData[10];
        [SerializeField]
        private ItemSlotData toolEquiptSlot = null;
    
        [Header("Items")]
        [SerializeField]
        private ItemSlotData[] itemSlots = new ItemSlotData[10];
        [SerializeField]
        private ItemSlotData itemEquiptSlot = null;
    
        public Transform handPoint;
    
        public void InventoryToHand(int indexSlot, InvSlots.TipeInventory tipeInventory)
        {
            ItemSlotData handEquip = toolEquiptSlot;
            ItemSlotData[] inventoryAlter = toolSlots;
    
            if(tipeInventory == InvSlots.TipeInventory.Item)
            {
                handEquip = itemEquiptSlot;
                inventoryAlter = itemSlots;
    
            }
    
            if (handEquip.Stackable(inventoryAlter[indexSlot]))
            {
    
                ItemSlotData slotAlter = inventoryAlter[indexSlot];
    
                handEquip.AddQuantity(slotAlter.quantity);
    
                slotAlter.Empty();
            }
            else
            {
                ItemSlotData slotToEquipt = new ItemSlotData(inventoryAlter[indexSlot]);
    
                inventoryAlter[indexSlot] = new ItemSlotData(handEquip);
    
                EquipHandSlot(slotToEquipt);
            }
    
            if(tipeInventory == InvSlots.TipeInventory.Item)
            {
                RenderHand();
            }
    
            UIManager.Instance.RenderInventory();
        }
    
        public void HandToInventory(InvSlots.TipeInventory tipeInventory)
        {
    
            ItemSlotData handSlot = toolEquiptSlot;
            ItemSlotData[] inventoryAlter = toolSlots;
    
            if(tipeInventory == InvSlots.TipeInventory.Item)
            {
                handSlot = itemEquiptSlot;
                inventoryAlter = itemSlots;
            }
    
            if(!StackItemToInv(handSlot, inventoryAlter))
            {
                for (int i = 0; i < inventoryAlter.Length; i++)
                {
                    if (inventoryAlter[i].IsEmpty())
                    {
    
                        inventoryAlter[i] = new ItemSlotData(handSlot);
    
                        handSlot.Empty();
                        break;
                    }
                }
            }
    
            if(tipeInventory == InvSlots.TipeInventory.Item)
            {
                RenderHand();
            }
            UIManager.Instance.RenderInventory();
        }
    
        public bool StackItemToInv(ItemSlotData itemSlot, ItemSlotData[] invArray)
        {
            for(int i = 0; i < invArray.Length; i++)
            {
                if (invArray[i].Stackable(itemSlot))
                {
                    invArray[i].AddQuantity(itemSlot.quantity);
    
                    itemSlot.Empty();
                    return true;
                }
            }
            return false;
        }
    
        public void RenderHand()
        {
            if (handPoint.childCount > 0)
            {
                Destroy(handPoint.GetChild(0).gameObject);
            }
    
            if (SlotEquipped(InvSlots.TipeInventory.Item))
            {
                Instantiate(GetEquippedSlotItem(InvSlots.TipeInventory.Item).gameModel, handPoint);
            }
    
        }
    
        #region Gets and Checks
        public DataItem GetEquippedSlotItem(InvSlots.TipeInventory tipeInventory)
        {
            if (tipeInventory == InvSlots.TipeInventory.Item)
            {
                return itemEquiptSlot.itemData;
            }
            return toolEquiptSlot.itemData;
        }
    
        public ItemSlotData GetEquippedSlot(InvSlots.TipeInventory tipeInventory)
        {
            if (tipeInventory == InvSlots.TipeInventory.Item)
            {
                return itemEquiptSlot;
            }
            return toolEquiptSlot;
        }
    
        public ItemSlotData[] GetInventorySlot(InvSlots.TipeInventory tipeInventory)
        {
            if (tipeInventory == InvSlots.TipeInventory.Item)
            {
                return itemSlots;
            }
            return toolSlots;
        }
    
        public bool SlotEquipped(InvSlots.TipeInventory tipeInventory)
        {
            if (tipeInventory == InvSlots.TipeInventory.Item)
            {
                return !itemEquiptSlot.IsEmpty();
            }
            return !toolEquiptSlot.IsEmpty();
        }
    
        public bool IsTool(DataItem item)
        {
            EquipmentData equipment = item as EquipmentData;
            if (equipment != null)
            {
                return true;
            }
    
            DataSeed seed = item as DataSeed;
            return seed != null;
        }
        #endregion
    
        public void EquipHandSlot(DataItem item)
        {
            if (IsTool(item))
            {
                toolEquiptSlot = new ItemSlotData(item);
            }
            else
            {
                itemEquiptSlot = new ItemSlotData(item);
            }
        }
    
        public void EquipHandSlot(ItemSlotData itemSlot)
        {
            DataItem item = itemSlot.itemData;
            if (IsTool(item))
            {
                toolEquiptSlot = new ItemSlotData(itemSlot);
            }
            else
            {
                itemEquiptSlot = new ItemSlotData(itemSlot);
            }
        }
    
        #region Validating Inventory
        private void OnValidate()
        {
            //validasi slot tangan
            ValidateInventorySlot(toolEquiptSlot);
            ValidateInventorySlot(itemEquiptSlot);
    
            //validasi slot inventaris
            ValidateInventorySlots(itemSlots);
            ValidateInventorySlots(toolSlots);
        }
    
        void ValidateInventorySlot(ItemSlotData slot)
        {
            if (slot.itemData != null && slot.quantity == 0)
            {
                slot.quantity = 1;
            }
        }
    
        void ValidateInventorySlots(ItemSlotData[] array)
        {
            foreach (ItemSlotData slot in array)
            {
                ValidateInventorySlot(slot);
            }
        }
        #endregion
    
        // Start is called before the first frame update
        void Start()
        {
            
        }
    
        // Update is called once per frame
        void Update()
        {
            
        }
    }
    
    #12075
    Jonathan Teo
    Moderator

    Hi Sand Man,

    Your code looks correct, so the issue may lie in InvSlots, UIManager or with the configuration of the components in the Inspector.

    1. Ensure that the SlotIndexes are assigned correctly in UIManager
    2. Ensure that the Hand Slots are configured correctly
    3. InvSlots Display function is working as intended

    #12076
    Sand Man
    Former Patron

    Hi Jonathan,

    thanks for replying. I found out the bug was my stackable bool only has one equal and not two on my ItemSlotData script.

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: