Search Unity

Inventory with List and Array???

Discussion in 'Scripting' started by Kashrlyyk, Aug 26, 2012.

  1. Kashrlyyk

    Kashrlyyk

    Joined:
    Aug 16, 2011
    Posts:
    17
    I like to have an inventory that is split up into a party inventory which needs to be dynamic and a character inventory which contains 10 spaces for each character, like it is done in "Wizardry 8".

    The party inventory would be a List and I like to make one big array for the character inventory and depending on what character is selected I display a different part of the array.
    If no item is in the array it should display a GUI.Label. But "Array = GUI.Label...." only lead to a "Can not convert " error.

    Using a list instead of an array no errors. But I couldn't display different parts of the list, because there are none. Using "Insert[cnt +10]" to move the item from the party inventory to the second player inventory instead of "Add", which just puts it at the next empty index, caused an "OutOfRange" error. "list.Capacity = 60" didn't help.

    Is the only way to do this to create a list for each character??

    Another problem is that I want the player to be able to arrange the items in the character inventory. Is that at all possible with Lists? I use C#.
     
  2. Games-Foundry

    Games-Foundry

    Joined:
    May 19, 2011
    Posts:
    632
    This should be enough to get you thinking in the right direction. I've just typed it directly so it might contain a few bugs. It's designed to work the way you want to work, and not how I personally would do it.

    Code (csharp):
    1.  
    2. public class Character
    3. {
    4.    public string name;
    5.    public int level;
    6.    public CharacterClass characterClass;
    7.  
    8.    public enum CharacterClass : byte
    9.    {
    10.       Warrior,
    11.       Rogue,
    12.       Mage
    13.    }
    14.  
    15.    // etc...
    16. }
    17.  
    18. public class InventoryItem
    19. {
    20.    public string sku; // unique identifier
    21.    public string name;
    22.    public int quantity;
    23.    public Character assignedTo;
    24.  
    25.    // etc...
    26. }
    27.  
    28. public class InventoryManager : Monobehaviour
    29. {
    30.    // Singleton Pattern
    31.    public static InventoryManager instance;
    32.    
    33.    private List<InventoryItem> items;
    34.  
    35.    public void Awake ()
    36.    {
    37.       instance = this;
    38.    }
    39.  
    40.    // Needs to be called from some other class first before you start using it
    41.    public void Initialize ()
    42.    {
    43.        items = new List<InventoryItem> ();
    44.    }
    45.  
    46.    public void AddItem ( InventoryItem item )
    47.    {
    48.       if ( !items.Contains ( item.sku ) )
    49.          items.Add ( item );
    50.    }
    51.  
    52.    public void RemoveItem ( InventoryItem item )
    53.    {
    54.        int count = items.Count;
    55.        for ( int i=0; i<count; i++ )
    56.        {
    57.           if ( items[i].sku == item.sku)
    58.           {
    59.              items.RemoveAt ( i );
    60.              return;
    61.           }
    62.        }
    63.    }
    64.  
    65.    // You may prefer to return the object or null rather than a boolean
    66.    public bool Contains ( InventoryItem item )
    67.    {
    68.        int count = items.Count;
    69.        for ( int i=0; i<count; i++ )
    70.        {
    71.           if ( items[i].sku == item.sku )
    72.               return true;
    73.        }
    74.  
    75.        return false;
    76.    }
    77.  
    78.    public InventoryItem Find ( string itemSKU )
    79.    {
    80.        int count = items.Count;
    81.        for ( int i=0; i<count; i++ )
    82.        {
    83.           if ( items[i].sku == itemSKU )
    84.                return items[i];
    85.        }
    86.  
    87.       return null;
    88.    }
    89.  
    90.    public bool AssignToCharacter ( InventoryItem item, Character character )
    91.    {
    92.         item = Find ( item.sku );
    93.         if ( item == null ) return false;
    94.         if ( item.assignedTo != null ) return false;
    95.  
    96.         item.assignedTo = character;
    97.         return true;
    98.    }
    99.  
    100.    public void Unassign ( Inventory item )
    101.    {
    102.           item = Find ( item.sku );
    103.           if ( item == null ) return;
    104.        
    105.           item.assignedTo = null;
    106.    }
    107.  
    108.    public List<InventoryItem> GetItemsForCharacter ( Character character )
    109.    {
    110.       // Note! You wouldn't really do it like this because you'd want to avoid the extra allocation, but since you don't seem experienced this will get you started
    111.      List<InventoryItem> result = new List<InventoryItem> ();
    112.  
    113.       int count = items.Count;
    114.       for ( int i=0; i<count; i++ )
    115.       {
    116.             if ( items[i].assignedTo == character )
    117.               result.Add ( items[i] );
    118.       }
    119.  
    120.      return result;
    121.    }
    122. }
    123.  
    124.  
    125.  
     
    Last edited: Aug 26, 2012