Hotbar operations

fukada

New member
Thank you for always helping me.

1, Clear hotbar
2, Newly obtained items are automatically set
3, Set the specified items

We are prototyping three movements.
There are some aspects of "iteminfo" that I don't fully understand.

I can set specified items, but new items cannot be set automatically.
Would you please tell me?
Is it necessary to specify itemCollection?

public void ItemHotBarAutoSet()
{
GameObject m_Player = GameObject.FindWithTag("Player");
Inventory myinventory = m_Player.GetComponent<Inventory>(); // Player
ItemCollection itemCollection = myinventory.GetItemCollection("Recovery"); // 回復フォルダから検索

var result = itemCollection.GetItemInfo(InventorySystemManager.GetItemDefinition("ハート"));

if (result.HasValue == false) { return; }
var ItemInfo = result.Value;

var Recovery = InventorySystemManager.GetItemCategory("回復アイテム");
var isTool = Recovery.InherentlyContains(ItemInfo.Item);

Debug.Log("isTool "+isTool+"/"+ ItemInfo.Item);

if (isTool)
{
// Add to Hotbar.
uint panelManagerID = 1;
var panelName = "Item Hotbar";
var hotbar = InventorySystemManager.GetItemViewSlotContainer<ItemHotbar>(panelManagerID, panelName);

Debug.Log("hotbar " + hotbar);

for (int i = 0; i < hotbar.SlotCount; i++)
{
if (hotbar.GetItemAt(i).Item != null) { continue; }
hotbar.AddItem(ItemInfo, i);
break;
}
}
}
 
Hi
Your scripts looks good to me. You can try to automate this by listening to the c_Inventory_OnAdd_ItemInfo_ItemStack event.
Check out the code on this page:

When you listen to the event you can check if the added item is a "Tool", or if it is part of the "Recovery" collection. If it is then try to add it to the hotbar.

I hope that helps :)
 
Sorry, my writing was bad. I want to know how to add to the hotbar automatically, not listen for events. Also, I want to know how to clear hotbar items.
 
No that's correct.
You automatically add items to the hotbar by listening the Add item event.
There is no way of doing this by default, you have to write a function that listens to the add event to add it to the hotbar.

You may also be interested by the ItemTranstioncionCollection: https://opsive.com/support/document...item-collections/item-transaction-collection/
this allows you to automatically move items to a specific ItemCollection when added to the transaction collection.



As for clearing the hotbar.
I believe there is a RemoveAll function. if not you can loop over the items in the hotbar and remove them one by one
 
What was set in the Hotbar was an item with a quantity of 0 and another fictitious item with the same name and quantity as the item in the inventory.
How should I obtain the items?

private void HandleOnAddItem(ItemInfo orignItemInfo, ItemStack addedItemStack)
{
if (addedItemStack == null) { return; }
// orignItemInfo と addedItemStack を使用して、項目の出所、追加された場所、追加量などを確認します...
//use the orignItemInfo and addedItemStack to know where the item came from, where it was added, how much was added, etc...

ItemCollection itemCollection = addedItemStack.ItemCollection;
var result = itemCollection.GetItemInfo(InventorySystemManager.GetItemDefinition(orignItemInfo.Item.name));
var ItemInfo = result.Value;

Debug.Log("ItemInfo/" + ItemInfo + "/name " + orignItemInfo.Item.name);

for (int i = 0; i < itemHotbar.SlotCount; i++)
{
if (itemHotbar.GetItemAt(i).Item != null) { continue; }
itemHotbar.AddItem(ItemInfo, i);
break;
}
}
 
You should add the "addedItemStack" directly, you don't need to search for the item. That's the item that was added.

So something like this should work (Not tested so I could be wrong, let me know if that's the case):
Code:
private void HandleOnAddItem(ItemInfo orignItemInfo, ItemStack addedItemStack)
{
    if (addedItemStack == null) { return; }

    for (int i = 0; i < itemHotbar.SlotCount; i++)
    {
        if (itemHotbar.GetItemAt(i).Item != null) { continue; }
        itemHotbar.AddItem(new ItemInfo(addedItemStack), i);
        break;
    }
}
 
Top