ItemID (RPG Inventory course)

Hi there, I was just going through the RPG course and am up to the bit where you play around with UUIDs and adding your own custom item to the inventory.

I actually saw the code to add items to the inventory earlier and tried adding my own and it didn’t work. Now - it still doesn’t work and commenting out the previous two entries (For the dagger and hat) also didn’t change anything.

Any help?

My inventory.cs is as follows

using System;
using UnityEngine;
using GameDevTV.Saving;

namespace GameDevTV.Inventories
{
    /// <summary>
    /// Provides storage for the player inventory. A configurable number of
    /// slots are available.
    ///
    /// This component should be placed on the GameObject tagged "Player".
    /// </summary>
    public class Inventory : MonoBehaviour, ISaveable
    {
        // CONFIG DATA
        [Tooltip("Allowed size")]
        [SerializeField] int inventorySize = 16;

        // STATE
        InventoryItem[] slots;

        // PUBLIC

        /// <summary>
        /// Broadcasts when the items in the slots are added/removed.
        /// </summary>
        public event Action inventoryUpdated;

        /// <summary>
        /// Convenience for getting the player's inventory.
        /// </summary>
        public static Inventory GetPlayerInventory()
        {
            var player = GameObject.FindWithTag("Player");
            return player.GetComponent<Inventory>();
        }

        /// <summary>
        /// Could this item fit anywhere in the inventory?
        /// </summary>
        public bool HasSpaceFor(InventoryItem item)
        {
            return FindSlot(item) >= 0;
        }

        /// <summary>
        /// How many slots are in the inventory?
        /// </summary>
        public int GetSize()
        {
            return slots.Length;
        }

        /// <summary>
        /// Attempt to add the items to the first available slot.
        /// </summary>
        /// <param name="item">The item to add.</param>
        /// <returns>Whether or not the item could be added.</returns>
        public bool AddToFirstEmptySlot(InventoryItem item)
        {
            int i = FindSlot(item);

            if (i < 0)
            {
                return false;
            }

            slots[i] = item;
            if (inventoryUpdated != null)
            {
                inventoryUpdated();
            }
            return true;
        }

        /// <summary>
        /// Is there an instance of the item in the inventory?
        /// </summary>
        public bool HasItem(InventoryItem item)
        {
            for (int i = 0; i < slots.Length; i++)
            {
                if (object.ReferenceEquals(slots[i], item))
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// Return the item type in the given slot.
        /// </summary>
        public InventoryItem GetItemInSlot(int slot)
        {
            return slots[slot];
        }

        /// <summary>
        /// Remove the item from the given slot.
        /// </summary>
        public void RemoveFromSlot(int slot)
        {
            slots[slot] = null;
            if (inventoryUpdated != null)
            {
                inventoryUpdated();
            }
        }

        /// <summary>
        /// Will add an item to the given slot if possible. If there is already
        /// a stack of this type, it will add to the existing stack. Otherwise,
        /// it will be added to the first empty slot.
        /// </summary>
        /// <param name="slot">The slot to attempt to add to.</param>
        /// <param name="item">The item type to add.</param>
        /// <returns>True if the item was added anywhere in the inventory.</returns>
        public bool AddItemToSlot(int slot, InventoryItem item)
        {
            if (slots[slot] != null)
            {
                return AddToFirstEmptySlot(item);
            }

            slots[slot] = item;
            if (inventoryUpdated != null)
            {
                inventoryUpdated();
            }
            return true;
        }

        // PRIVATE

        private void Awake()
        {
            slots = new InventoryItem[inventorySize];
           slots[0] = InventoryItem.GetFromID("71e73607-4bac-4e42-b7d6-5e6f91e92dc4");
           slots[1] = InventoryItem.GetFromID("0aa7c8b8-4796-42aa-89d0-9d100ea67d7b");
           slots[11] = InventoryItem.GetFromID("0aa7c8b8-4796-42aa-89d0-9d100ea67d7b");
           slots[10] = InventoryItem.GetFromID("0aa7c8b8-4796-42aa-89d0-9d100ea67d7b");
        }

        /// <summary>
        /// Find a slot that can accomodate the given item.
        /// </summary>
        /// <returns>-1 if no slot is found.</returns>
        private int FindSlot(InventoryItem item)
        {
            return FindEmptySlot();
        }

        /// <summary>
        /// Find an empty slot.
        /// </summary>
        /// <returns>-1 if all slots are full.</returns>
        private int FindEmptySlot()
        {
            for (int i = 0; i < slots.Length; i++)
            {
                if (slots[i] == null)
                {
                    return i;
                }
            }
            return -1;
        }

        object ISaveable.CaptureState()
        {
            var slotStrings = new string[inventorySize];
            for (int i = 0; i < inventorySize; i++)
            {
                if (slots[i] != null)
                {
                    slotStrings[i] = slots[i].GetItemID();
                }
            }
            return slotStrings;
        }

        void ISaveable.RestoreState(object state)
        {
            var slotStrings = (string[])state;
            for (int i = 0; i < inventorySize; i++)
            {
                slots[i] = InventoryItem.GetFromID(slotStrings[i]);
            }
            if (inventoryUpdated != null)
            {
                inventoryUpdated();
            }
        }
    }
}

As you can probably see, there’s no changes made other then attempting to play around with the UUIDs

Pulling by ItemID in the code is actually inefficient at this point, when you can make a reference to the items directly. Your itemID will not match the itemID from the course, and for feeding the Inventory from Awake(), it’s unneeded when you can instead create a pre-load serializedField. ItemIDs make much more sense when it’s time to Save and Restore the items.

[SerializeField] InventoryItem[] preLoadedItems;

void Awake()
{
    foreach(InventoryItem item in preLoadedItems)
    {
         AddToFirstEmptySlot(item);
    }
}

Bear in mind that the version of the Inventory script you are using is not the final version in which we will be dealing with InventorySlots

struct InventorySlot
{
    InventoryItem item;
    int number;
}

instead of simply InventoryItems (because it might be handy to know how many are in the slot for stackables).

I appreciate the reply!

I understand it’s inefficient + whatever else, but why does it not work? Is the inventory hardcoded somewhere? No changes to the awake code reflect in-game.

I’m not sure what you mean by hard coded. Each Inventory Item is stored in a ScriptableObject.

My assumption is that the ItemIds are incorrect because they won’t be the same in my project as they are in yours. Let’s do a test to see if we can see what’s going on.

private void Awake()
{
    slots = new InventoryItem[inventorySize];
    TryAddInventoryItem(0, "71e73607-4bac-4e42-b7d6-5e6f91e92dc4");
}

private void TryAddInventoryItem(int index, string itemId)
{
     InventoryItem item = InventoryItem.GetFromID(itemId);
     if(item==null)
     {
           Debug.Log($"Unable to find {itemID} in Inventory.GetFromID()");
     } else
     {
           Debug.Log($"Adding {item} to slot {i}");
     }
}

One further thing to check: Even if the itemID is correct, if an InventoryItem is not in a folder named Resources, it simply won’t be found by the system.

Sorry - I meant if it was possible the inventory items were being added to inventory from elsewhere but i’ve not found anything. I’ll go ahead with your test now, but as said previously, even deleting the item ID’s from the awake method would not change the inventory.

I added everything to the Resource folder per the video/Unity docs.

OH!

You have a save file which is overwriting the inventory.

That’s what I thought the issue was!

Am I good to go and just delete the save file to see the changes?

Yep!

Thank you very much for your help! Truly appreciated.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms