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

Question Help with creating inventory system that supports item amount

Discussion in 'Scripting' started by Daedalus_0, Jul 16, 2021.

  1. Daedalus_0

    Daedalus_0

    Joined:
    May 31, 2020
    Posts:
    3
    Hello. As the title says, I'm in need of creating an RPG inventory system that supports storing several of the same item with an item count (think Bethesda titles on the Gamebryo/Creation engines). Searching for tutorials on this only result in ARPG-esque or single-item-per-slot approaches, which doesn't provide all the solutions I'm looking for.

    Currently, this is what I've set up as a proof of concept to iterate upon later:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerManager : MonoBehaviour
    6. {
    7.     public List<Item> playerInventory = new List<Item> (50);
    8.     public float playerWeight;
    9.  
    10.     public void AddItemToInventory(Item itemToAdd)
    11.     {
    12.         if (itemToAdd.itemID != null)
    13.         {
    14.             playerInventory.Add(itemToAdd);
    15.         }
    16.         else
    17.         {
    18.             Debug.Log($"{itemToAdd} is an invalid item.");
    19.         }
    20.     }
    21.  
    22.     public void RemoveItemFromInventory(Item itemToRemove)
    23.     {
    24.         if (playerInventory.Contains(itemToRemove))
    25.         {
    26.             playerInventory.Remove(itemToRemove);
    27.         }
    28.         else
    29.         {
    30.             Debug.Log($"{itemToRemove} does not exist in the player's inventory.");
    31.         }
    32.     }
    33. }
     
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,596
    So uhh... what do you need help with? It seems like you have the right idea.
     
    Daedalus_0 likes this.
  3. coder091

    coder091

    Joined:
    Feb 16, 2021
    Posts:
    23
    ScriptableObjects are the solution for you. They are the best to keep values.

     
    Daedalus_0 likes this.
  4. Daedalus_0

    Daedalus_0

    Joined:
    May 31, 2020
    Posts:
    3
    Well, specifically I need help coming up with a solution where I not only can store items in a list, but also can show these items as a stack in the inventory, and this is what I'm clueless about.
     
  5. Daedalus_0

    Daedalus_0

    Joined:
    May 31, 2020
    Posts:
    3
    I know ScriptableObjects are a good solution, but not something I'd consider for the inventory management itself. Items themselves in order to simplify item making later, sure, but I'm talking having an inventory system that takes into account how many of the same item you have in your inventory.
    EDIT: Oof, didn't have my cookies settings in order, so I didn't have the ability to open up the video and see what the video described. Thanks for the suggestion.
     
  6. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,596
    You could create an object which derives from Item which is a stack, that might work.
     
  7. wileyjerkins

    wileyjerkins

    Joined:
    Oct 13, 2017
    Posts:
    77
    depends on how many different types of items you have, but i would just have one player inventory list, and when the player opens his inventory, just have a script scan the list to count/create stacks. It could count stackable items as one item if < than max stack size.