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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Preserving list indexes when removing items from a list.

Discussion in 'Scripting' started by Ahmed-C, Dec 26, 2015.

  1. Ahmed-C

    Ahmed-C

    Joined:
    Dec 19, 2013
    Posts:
    10
    Hi,

    Suppose I'm creating an inventory system using a list<t>. suppose the inventory system has 6 slots so there are indexes from 0 - 5. suppose I have slots 1 to 4 filled. If I have a method called UseSlotItem(index); which uses an item from the index of that list and after the item has been used, I use RemoveAt(index) to remove that item from the list. Is there a way to keep the items indexs without making them move?
    so If I have 1 to 4 slots filled, I use the 3rd item, item of index 4 becomes item of index 3 is there a way to keep that indexes without making them move around.

    A good example would be Minecraft's inventory system. There are 9 slots, If i have slots 1 to 5 filled and I drop the item in slot 3, then clicking on slot 3 again wouldn't active what is on slot 4 because the items don't shift, rather they stay the same place, How can I achieve the same thing using lists or any other collection in Unity?
     
  2. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    Use an array.

    Though to make a GOOD inventory system would be to do something like so.

    Code (CSharp):
    1. public class Slot : System.Object {
    2.     protected Item m_item;
    3.     protected int m_quantity;
    4.  
    5.     public Slot(Item item) {
    6.         this.m_item = item;
    7.     }
    8.  
    9.     public void SetItem(Item item) {
    10.         if(this.m_item != null) {
    11.             if(this.m_item.GetType() == item.GetType()) {
    12.                 this.m_quantity++;
    13.             } else {
    14.                 Debug.Log("Item slot taken!");
    15.             }
    16.         } else {
    17.             this.m_item = item;
    18.         }
    19.     }
    20.  
    21.     public void Use() {
    22.         if(this.m_item != null) {
    23.             this.m_item.Consume();
    24.         } else {
    25.             Debug.Log("No item in slot!");
    26.         }
    27.     }
    28.  
    29.     public void Drop() {
    30.         if(this.m_item != null) {
    31.             this.m_quantity--;
    32.  
    33.             if(this.m_quantity <= 0) {
    34.                 this.m_item.Dropped();
    35.                 this.m_item = null;
    36.             }
    37.         } else {
    38.             Debug.Log("No Item to drop!");
    39.         }
    40.     }
    41. }
    42.  
    43. [System.Serializable]
    44. public abstract class Item : System.Object {
    45.     public abstract void Consume();
    46.     public abstract void Dropped();
    47. }
    48.  
    49. public class Demo : MonoBehaviour {
    50.     // Make an inventory with 3 slots empty.
    51.     [SerializeField] Slot[] m_inventory = new Slot[] {
    52.         new Slot(null),
    53.         new Slot(null),
    54.         new Slot(null)
    55.     };
    56. }
     
  3. Ahmed-C

    Ahmed-C

    Joined:
    Dec 19, 2013
    Posts:
    10
    Thanks for that, since I want to keep it simple, do you mind showing me a simpler example using Arrays? say I have 6 slots, how would I go about doing that?

     
  4. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    When initializing the array, initialize with 6 slots instead of 3 like I did.
     
  5. Ahmed-C

    Ahmed-C

    Joined:
    Dec 19, 2013
    Posts:
    10
    Awesome, let you know how it goes :)