Reset Index Question

I was wondering if it was possible to reset the indexes on an Item Collection/Inventory. The basic idea of what I am doing is creating random inventories in the world that display a menu for when you search them. Here is my basic logic:

C#:
        protected override void Start()
        {
            base.Start();
            if (m_randomAmounts)
            {
                m_ItemAmountProbabilityTable = new ItemAmountProbabilityTable((m_StorageInventory.MainItemCollection.GetAllItemStacks(), 0));
                var randomItemAmounts = m_ItemAmountProbabilityTable.GetRandomItemAmounts(m_MinAmount, m_MaxAmount);
                m_StorageInventory.MainItemCollection.RemoveAll();
                for (int i = 0; i < randomItemAmounts.Count; i++)
                {
                    if (randomItemAmounts[i].Amount > 0)
                    {
                        m_StorageInventory.MainItemCollection.AddItems(randomItemAmounts[i]);
                    }
                }
            }
        }

The only problem I have is that when the display menu pops open for the first time, the item grid that is drawn uses the original indexes before generating the random inventory. This will leave some blank gaps in the display menu between items. Is there some code I can run that resets the Indexes after I add the random items to the list? I know on the Inventory Grid there I can turn off UseGridIndex, but everytime it opens it resets indexes.
 
Yes UseGridIndex allow you to keep track of the indexes, toggling it off will draw the inventory items as they appear in the collection.

Probably the easiest way to keep the useGridIndex but redrawing without leaving empty spaces is by calling the inventoryGrid.SortItemIndexes(...) function. This will automatically reset the GridIndexer indexes. You can pass in whatever sort index you want, by name, id, etc... You can have some code that keeps track of when the item was added and sort in that order, etc...
 
Thanks for the response! That all makes sense to me. While I did get it sort of working, I noticed that it only retained the index of the last searched inventory. This wasn't exactly desired, as I wanted the slot position to be always remembered. I simply changed all of the Searchable Boxes to use an ItemSlotCollection, and this did exactly what I was hoping for!
 
Top