Get items in inventory grid order

fukada

Member
This is hard to explain...
I'd like to retrieve items in the order that they are in the inventory grid.
(I want to set them on the hotbar in the order that they are in the inventory grid.)
Can I change the inventory order to be based on the inventory grid order?
Or can I get the inventory grid order?
 
Last edited:
I was able to get the items in the inventory grid with a simple script like the one below.

However, this requires me to open the inventory grid manually.

How can I make it so that it opens automatically once?

foreach (var item in indexedItems)
{
var itemStack = item.Key;
var index = item.Value.index;
var itemInfo = item.Value.itemInfo;

Debug.Log($"アイテム: {itemStack}, インデックス: {index}, アイテム情報: {itemInfo}");
}

Hmm. I can't actually get them.
Even if I rearrange the inventory grid, the order of the items I get remains the same as in my inventory.
This doesn't make sense.

How can I get items in the order they are in the inventory grid?
 
Last edited:
Hi, sorry for the delay

you have two options. They are slightly different so see which one suit best your needs:

Code:
var listSlice = new ListSlice<ItemInfo>();
listSlice = inventoryGrid.InventoryGridIndexer.GetOrderedItems(listSlice);
for (int i = 0; i < listSlice.Count; i++) {
    var item = listSlice[i];
    
    //Do stuff
}


//OR


var count = inventoryGrid.Grid.ElementCount;
for (int i = 0; i < count; i++) {
    var item = inventoryGrid.Grid.Elements[i];
    //Do stuff
}
 
Thank you. I was able to get it.
I think it's out of range, so please let me know.
I get an error.
IndexOutOfRangeException: Index was outside the bounds of the array.

public void GetInventoryGridItems()
{
if (m_InventoryGrid == null)
{
Debug.LogWarning("InventoryGridが設定されていません。");
return;
}

//if (_remainingItems.Count > 0)
//{
// _remainingItems.Clear(); // リストをクリアします。
//}

_remainingItems = new List<ItemInfo>();
int count = m_InventoryGrid.Grid.ElementCount;

for (int i = 0; i < count; i++)
{
var item = m_InventoryGrid.Grid.Elements;

Debug.Log(i +"/"+ count +" elements " + m_InventoryGrid.Grid.Elements);

if (item != null) // nullチェック
{
_remainingItems.Add(item);
}
}

Debug.Log("for End");

for (int i = 0; i < _remainingItems.Count; i++)
{
Debug.Log($"位置 {i}: {_remainingItems}");
}
}

Cannot be retrieved using "for".
"Debug.Log("for End");" is not executed.


This only works if there is an item at the last id in the inventory grid.
Maybe I need to initialize it or something?

Also, it is not possible to detect when there are no items in the grid.
Isn't it "null"?
if (item != null) // nullチェック

However, this requires me to open the inventory grid manually.
How can I make it so that it opens automatically once?
 
Last edited:
Sorry, in that case you might be better off using this function then

Code:
var orderedItems = inventoryGrid.FilterAndSortItemInfos(false);
for (int i = 0; i < orderedItems.Count; i++) {
var item = orderedItems[i];
    
 //Do stuff
}
 
Thank you for showing me the correct way to do it. I was able to get it.

if (item != null)
{
_remainingItems.Add(item);
}

I keep asking this question, but please tell me.
I want to check if the inventory grid is null, but I don't know how. It gets added to the list as [none iteninfo].

I would also be grateful if you could tell me how to display the inventory grid ID for each grid in the inventory grid.
 
in this case "item" is not really item it's an ItemInfo.
So it0s better to name it as such


Code:
var orderedItems = inventoryGrid.FilterAndSortItemInfos(false);
for (int i = 0; i < orderedItems.Count; i++) {
var itemInfo = orderedItems[i];
    
    if(itemInfo.Item == null || itemInfo.Amount == 0){ continue; }
 //Do stuff
}

I would also be grateful if you could tell me how to display the inventory grid ID for each grid in the inventory grid.

I'm not sure what you mean by this.
You could use "i" for this no?

Or do you mean you want to display item once the itemInfo is set in the ItemView perhaps. I'm afraid the ItemView has no context of the index it is at.

What you can do is check the index of the itemInfo in the list of items

Code:
var orderedItems = inventoryGrid.FilterAndSortItemInfos(false);
var index = orderedItems.IndexOf(itemInfo);
/CODE]
 
Thank you for your reply.
The inventory grid ID display is as shown in the image below.
It would be nice if each grid could obtain its own ID.

アートボード 1.png
 
Another approach you could take is using the
InventoryGrid.SlotIndexOffset

This will return the value for which you grid is offseted.

For example you grid has 6 slots that is shown, 3x3. Then as you scroll your inventoryGrid you SlotIndexOffset will grow.

You ItemViewSlots have an Index.

So bu adding the too InventorGrid.SlotIndexOffset + ItemViewSlot.Index then you'll get the id you are looking for (If I understand correctly)
 
Although it's not what I wanted, I was able to display the ID. Thank you very much.

InventoryGrid m_InventoryGrid = GameObject.Find("Storage - Client Inventory").GetComponent<InventoryGrid>();
if (m_InventoryGrid == null) { Debug.Log("Inven Null"); }
ItemViewSlot m_ItemViewSlot = transform.parent.GetComponent<ItemViewSlot>();
if (m_ItemViewSlot == null) { Debug.Log("ItemViewSlot Null"); }
SlotId = m_InventoryGrid.SlotIndexOffset + m_ItemViewSlot.Index;
m_ID.text = SlotId.ToString();

スクリーンショット 2024-11-02 135624.png
 
Ah, you want to skip counting for slots that are empty...

That a bit more complicated since it requires you to know what slots are empty...

It's not pretty but I have an idea in mind. Create a dictionary of item -> index when you check the ordered list. and when you draw them you can check the dictionary.


Pseeudo code, not tested:

Code:
YourDictionary = new Dictionary<ItemStack, int>();
var orderedItems = inventoryGrid.FilterAndSortItemInfos(false);
var count = 0;
for (int i = 0; i < orderedItems.Count; i++) {
var itemInfo = orderedItems[i];
    
    if(itemInfo.Item == null || itemInfo.Amount == 0){ continue; }
    count ++;
    YourDictionary.Add(itemInfo.ItemStack, count);
 //Do stuff
}






//Then inside the ItemView
var dictionary = InventoryGrid.GetComponent<TheComponentWitYourDictionary>().YourDicrionary;
m_ID.text = dictionary[itemInfo.ItemStack];
 
Back
Top