Multiple Inventories

Updated 6 days ago in [A] Brackeys Courses
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
4 on November 8, 2018

Hi there! I followed Brackey’s tutorials on creating an RPG, specifically looking at how he creates his inventory system. The way he does it is fantastic and I have followed the code to the letter. For a project I’m working on I need to create multiple inventories independent from each other. Is there a way to do this without duplicating code and keep it clean? I’ve been looking everywhere and can’t find anything applicable to this situation.

Thanks in advance!

  • Liked by
Reply
0 on November 9, 2018

Can you provide your code?

  • Liked by
Reply
Cancel
0 on November 9, 2018

Of course. I followed the tutorials to the letter (with some additional code to allow me to access FMOD) and there are six scripts that eventually influence the inventory as follows:

(Inventory)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Inventory : MonoBehaviour {

#region Singleton
    public static Inventory instance;

    void Awake()
    {
        if(instance != null){

            Debug.LogWarning(“More than one instance of inventory found”);
            return;
        }

        instance = this;
    }

    #endregion

    public delegate void OnItemChanged();
    public OnItemChanged onItemChangedCallBack;

    public int space = 100;

    public List<Item> items = new List<Item>();

    public bool Add (Item item)
    {
        if (!item.isDefaultItem)
        {
            if(items.Count >= space){

                Debug.Log(“Not Enough Room”);
                return false; 
            }
              
            items.Add(item);
            if(onItemChangedCallBack != null)
            onItemChangedCallBack.Invoke();
        }

        return true;
    }

    public void Remove (Item item)
    {
        items.Remove(item);

        if (onItemChangedCallBack != null)
            onItemChangedCallBack.Invoke();
    }
}

…………….

(Inventory Slot)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using FMODUnity;

public class InventorySlot : MonoBehaviour
{

    Item item;
    public Image icon;
    public Button removeButton;

    [FMODUnity.EventRef]
    public string FMOD_Event;
    public FMOD.Studio.EventInstance FMOD_EventInst;

  
    void Update(){}
    public void AddItem(Item newItem)
    {

        item = newItem;

        icon.sprite = item.icon;
        icon.enabled = true;
        removeButton.interactable = true;
        FMOD_Event = item.FMOD_Event;
        FMOD_EventInst = FMODUnity.RuntimeManager.CreateInstance(FMOD_Event);

    }

    public void ClearSlot()
    {
        item = null;

        icon.sprite = null;
        icon.enabled = false;
        removeButton.interactable = false;
    }
    public void OnRemoveButton()
    {
        Inventory.instance.Remove(item);
    }

    public void UseItem()
    {
        if (item != null)
        {
            item.Use();
            FMOD_EventInst.start();

        }
    }
}
………….

(InventoryUI)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class InventoryUI : MonoBehaviour
{

    public Transform itemsParent;
    public GameObject inventoryUI;
    Inventory inventory;

    InventorySlot[] slots;

    // Use this for initialization
    void Start()
    {

        inventory.onItemChangedCallBack += UpdateUI;
        slots = itemsParent.GetComponentsInChildren<InventorySlot>();
        inventory = Inventory.instance;

    }

    public void SlotChange()
    {

    }

    // Update is called once per frame
    void Update()
    {

        if (Input.GetButtonDown(“Inventory”))
        {
            inventoryUI.SetActive(!inventoryUI.activeSelf);

        }
    }

    void UpdateUI()
    {
        Debug.Log(“Updating UI”);
        for (int i = 0; 1 < slots.Length; i++)
        {
            if (i < inventory.items.Count)
            {
                slots[i].AddItem(inventory.items[i]);
            }
            else
            {
                slots[i].ClearSlot();
            }
        }

    }
}

…………….

(Item)

using UnityEngine;
using FMODUnity;
using System.Collections;
using System.Collections.Generic;

[CreateAssetMenu(fileName = “New Item”, menuName = “Inventory/ Item”)]

public class Item : ScriptableObject {

    public ItemSlot itemSlot;
    //Item
    new public string name = “New Item”;
    public Sprite icon = null;
    public bool isDefaultItem = false;
    //Fmod
    [FMODUnity.EventRef]
    public string FMOD_Event;

   
    public virtual void Use ()

    {
        // Use the item
        Debug.Log(“Using” + name);

    }

    public void RemoveFromInventory()
    {
        Inventory.instance.Remove(this);
    }

}

………………….

(Interactable)

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Interactable : MonoBehaviour
{

    public virtual void Interact()
    {

        Debug.Log(“Interacting with” + transform.name);

    }

}

………………..

(ItemPickUp)

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ItemPickup : Interactable {

    public Item item; 

    public override void Interact(){

        base.Interact();

        PickUp();
       }

    void PickUp()
    {

        Debug.Log(“Picking up” + item.name);
        bool wasPickedUp = Inventory.instance.Add(item);

    }
}

 

…………..

 

I would like multiple inventories to use in the context that if inventory one isn’t active, the picked up item will automatically assign to inventory two. Alternatively, is there a way to let items know what inventory they should assign to (perhaps through an enum) on different occasions?

 

Thank you for any help you can give (sorry for the long post)

  • Liked by
Reply
Cancel

did you solve this problem?

  • Liked by
Reply
Cancel
0 6 days ago

No hasn’t been solved, still can’t think of how to do it:/

  • Liked by
Reply
Cancel