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. Dismiss Notice

Questions about Inventory System

Discussion in 'Editor & General Support' started by Vandarthul, May 3, 2014.

  1. Vandarthul

    Vandarthul

    Joined:
    Dec 23, 2012
    Posts:
    20
    Hello there, I've been thinking to create an Inventory system and I would like to get your suggestions about (1)if this is an "okay" implementation (2)are there any "better" or similar implementations that you know of, and if you can give source of the idea behind that.

    So basically:
    ITEMS
    1. A base Item Class that has basic information that every Items should have.
    2. Child classes that inherits from Item Class, such as Weapon, Armor Class.

    INVENTORY
    1. My inventory consists of array of buttons.
    2. I have array of Item Class assigned to my array of buttons.

    PROCESS
    1. Player kills an enemy.
    2. Enemy drops a loot from Weapon, Armor, Material Classes. Lets say a weapon, every information of the weapon are assigned on their default constructors(for instance; Type, Damage, Attributes of weapon).
    3. Player picks the weapon, Inventory Class assigns the weapon to it's Item Class array, so the weapon is now in our Inventory.
    4. Player wants to equip the weapon, thus the information of weapon is needed. Weapon from Item Class array is converted to Weapon Class explicitly(with typecasting) so we can use the information needed now.
    5. Item is removed from the Item Class array and other minor stuff being made...

    For better understanding, please examine the following example;
    Code (csharp):
    1.     Item item = new Weapon(); //Weapon gets random values on it's default constructor
    2.    
    3.     //Whenever I want the information of the weapon, lets say it's damage from Weapon Class:
    4.    
    5.     Weapon weapon = (Weapon)item;
    6.     print(weapon.GetAverDamage());
    Note that I'm not asking for code, just some other implementation ideas that seems more convenient. For example, I didn't like the idea of generating an array of "buttons" for my inventory. If there are any other systems that you know of, it would be appreciated.
    Also, how should I approach to following systems according to your opinion:
    • Dragging Dropping items in inventory
    • Popup information on mouse over about item in inventory
    • Stacking items and showing the amount

    I really would like to do something smooth and usable, so I'm into any advanced options that you might suggest. Thank you for your time.
     
    Last edited: May 4, 2014
  2. Vandarthul

    Vandarthul

    Joined:
    Dec 23, 2012
    Posts:
    20
  3. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    I've recently done an inventory system, and i thought about two ways of doing it:

    Making just one item class and put every possible attribute in it and handle the further treatment (usage, stackable etc) by an itemType, thus every item would have every attribute even though it doesn't make any sense. In the end i didn't like that one.

    So, the second way which i finally decided for is similar to yours. I created a base item class which has all common attributes such as name, type, id etc. and necessary functions which will work for each derived item class individually based on polymorphism. I inherit from that class for any type of item, such as weapon, armor, food, material and implement the functionalities.

    As a kind of database, i made a List and added all items in my game to it.

    The inventory item's array can now be filled with any derived type.
    I personally think that's a proper way of doing that, so unless noone comes and tells you to stop, just go for it.


    ___________

    Stacking can be done with a single boolean attribute. In my system, i check that attribute first and if it it stackable, i search for the same item type in my inventory and add it there in case the max amount hasn't been reached, otherwise i'll try to find another stack and so on. If there's no other stack which can hold more items, i put it into the next empty slot. If it's not stackable, i immediately go for the next empty slot.

    Popup information have driven me a bit crazy since my GUI elements have always been drawn behind my inventory window, so i finally decided to make another window for the popup so that i can be drawn in front of the inventory. There might be more efficient solutions, but i had to go for it since i have to finish the project for school. Just play around with it and you'll surely find a way that you're satisfied with. ;)

    Drag Drop: I haven't tried to do that yet because it's not important for my project but spontaneously i'd do it that way:

    Whenever the mouse is hold down, i'd check if one of my inventory slots (the rect of the elements) contains that button and snap the item's icon to the mouse and set a boolean which i can work with, and set a temp reference to the item in that slot. While the mouse is still down, move the icon with the mouse and on mouse button up i'd check if the position is contained by another slot's rectangle + if the boolean was set. If so, replace the item it was taken from with an empty one (or null, but that might cause exceptions) and put add the item you copied to your new slot. Mouse up on a slot + non-set boolean will do nothing then.

    Can surely be done way cleaner, but just a quick idea.
     
  4. Vandarthul

    Vandarthul

    Joined:
    Dec 23, 2012
    Posts:
    20
    Thank you for your reply Suddoha. But I couldn't get how you did the stacking system. Shouldn't there be an amount of the item that is going to be controlled? I though of adding an "amount" variable to my Item class so that I can check it and act accordingly.
     
  5. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Well ye, you might wanna limit the amount of items per stack. As for the stacking system, i implemented a boolean which is true if the items can be stacked (for example life potions, materials) and falls for items such as weapons, armors etc which usually do not get stacked in most games.

    So, whenever you try to loot an item you will check if it can be stacked or not, that's what i meant.