Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Inventory system questions.

Discussion in 'Scripting' started by Yourking77, Jul 2, 2016.

  1. Yourking77

    Yourking77

    Joined:
    Jan 14, 2016
    Posts:
    303
    I have created a main menu and a basic ui during the game with a cross hair and made it so you can now pause my game, I think I have got the basics of the ui elements down so I am ready to move into inventory systems.I would like an inventory system like Skyrims for example, how would I accomplish something like that? is that mostly script, with like a ton of arrays to keep track of the hundreds of different items? Can anyone point me to a great tutorial because I could not find one that really looked like it would work good for my type of game.
     
  2. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,921
    You can just have one array/list.

    You would filter the list based on item behaviour/type (weapon, armor, potions, quest items). The behaviour of the items would be decided by implementing Interfaces.

    So you could have a base Item class, then subclasses that implement a certain interface, like so:

    Code (csharp):
    1. public abstract class Item {}
    2.  
    3. public class Sword : Item, IWeapon {}
    4.  
    5. // Shield is both armor and a weapon because you can do a shield bash attack.
    6. public class Shield : Item, IArmor, IWeapon {}
    7.  
    8. public class Arrow : Item, IAmmo {}
    9.  
    10. public class DoorKey : Item, IUseable {}
    Then in your inventory GUI you would have a button for "Weapons", then just look through your list of items, check if they have implemented IWeapon and display them if they have.
     
    eses likes this.
  3. Yourking77

    Yourking77

    Joined:
    Jan 14, 2016
    Posts:
    303
    That makes sense, so to filter the different weapon types could I use different tags? I still think it is slightly beyond my experience and I need to find a good tutorial still but what you said makes sense and I think it made it seem easier now too.
     
  4. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,921
    It's kind of like tagging the items. Your inventory could look like this:

    Code (csharp):
    1. public class Inventory : MonoBehaviour {
    2.  
    3.     //===== VARS =====
    4.  
    5.     public List<Item> _inventory; // List of all your items
    6.  
    7.     //===== FUNCTIONS =====
    8.  
    9.     List<Item> GetAllWeapons()
    10.     {
    11.         // Create empty list
    12.         List<Item> foundWeapons;
    13.  
    14.         // Look through all inventory items
    15.         foreach(Item item in _inventory)
    16.         {
    17.             // Get IWeapon interface on current item
    18.             IWeapon weapon = (IWeapon)item.GetComponent(typeof(IWeapon));
    19.  
    20.             // If IWeapon was found, add item to foundWeapons list
    21.             if (weapon != null)
    22.             {
    23.                 foundWeapons.Add(item);
    24.             }
    25.         }
    26.  
    27.         // Deliver list of weapons. A GUI could use this and only display this list.
    28.         return foundWeapons;
    29.     }
    30.  
    31. }
    There may be better ways to do it instead of 100 GetAllType functions, but you get the idea.
     
  5. recon0303

    recon0303

    Joined:
    Apr 20, 2014
    Posts:
    1,634
    There are a ton of tuts out there for stuff like this, but as Star Dog as shown using LIST is the best way to go.. If I where you since you are new at this, just rip through some tuts on inventory and then learn how the code works and understand, if you just copy a tut you won't learn anything..

    If you go further into this for a database manager, check into Scriptable Objects.
    https://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/scriptable-objects


    This is one of the better ones, there is an asset made by this one..Then the dev made it free.



    If you need more help ask, but remember no one will hold your hand, if we did it is not helping you..I actually i'm a UI programmer for a living, so I would be happy to answers any questions as long as your willing to learn :)


    This one uses a very common Grid style, but most inventory's are all made in some similair fashion if games like The Forest, Skyrim and more they just are not using Slot/ grid . But some of the concept is the same if you can do these styles you can do any.