How To Create An Item View Listener?

Hello and good evening,

I am trying to put together an event listener that can filter out items based on the Item view slot they have been added and ofcourse their Collection, per example lets pick "c_Inventory_OnAdd_ItemInfo_ItemStack".

The issue with the aforementioned event is the fact that lets say i had the desired Item View Slot as a reference, i would still hit a wall cause the Item View Slot only holds its past value at the moment of the addition of the Item to the Collection.

A possible suggestion to this would be to add to the event an Item View Slot parameter that merely indicates where this Item will be added, if its null we can assume this Item will not be added to a U.I (like a monster per example or something that holds some Items but are not gonna get displayed).

Cause the only other alternative to this would be to listen to the containers event for on draw, but this seems convoluted and hard to wrap your head around when you go with a logical flow for this.

An example script:

C#:
    [SerializeField]
    private ItemViewSlot TargetSlot;

    [SerializeField]
    private ItemCollectionID targetCollection;

    private Inventory inventory; //Its referened in start, this is only an example

    public void ItemViewSlotListener(ItemInfo itemInfo, ItemStack stack)
    { 
        if (inventory.GetItemCollection(targetCollection) != stack.ItemCollection)
        {
            return;
        }

        //The following is not possible to work
        if (TargetSlot.ItemInfo == itemInfo)
        {
            return;
        }

        //Do stuff
    }


What other alternatives exist to handle a listener like the one described?
 
Last edited:
The Way the Item View Slot Containers work is that they listen to the Inventory update event. When they do they set a boolean to updated the UI. During LateUpdate I check if that boolean is true, if it is I draw/update the UI. We do this to prevent the UI from being drawn multiple times in a frame (for example when a drag & drop an item on another you do at least 4 inventory updates, remove A, remove B, Add A, Add B).

The part I do not understand is why you need to know about the UI slot when the item is added. In a sense the UI is seperated from the Inventory, so your logic should normally be able to work even if the UI does not exist. Perhaps instead of explaining what you are trying to achieve you could explain why you are trying to implement this?
From what you said in Discord you mentioned it was to prevent having to write the same function twice for ItemActions and DropActions. There is a DropAciton that lets you use an ItemAction: "ItemViewDropActionToItemAction".

But in many cases you wouldn't want to do that because the Drop Action has a lot more information about the origin of the item and its destination.

With Item Actions you know the origin but you do not know the destination, you can only predict it in some cases. For example if you are using an ItemSlotCollection you can use this function:
Code:
//Get the index where the item could potentially fit if it was added.
var potentialSlotIndex = equipmentItemCollection.GetTargetSlotIndex(myItem);

In your case though you say you would like to know the slot after the item was added, so if you are using an ItemSlotCollection you can do:
Code:
//Gets the slot where the item actually is.
var realSlotIndex = equipmentItemCollection.GetItemSlotIndex(myItem);

These functions are shown in the documentation:

Of course this only gives you the slot within the Item Slot Collection (since the other item collections do not work with slots it cannot be done in all use cases). It doesn't give you the slot within the UI. But from the slot index you should be able to get the ItemViewSlot where the item will be set in late update (remember the UI is drawn once a frame, so if an item is equipped/unequipped multiple times a frame it does not matter.)

If what you want is to show something visually in the UI when an item is set you can use ItemViewModules.

I hope that makes sense. Let me know if it does not.
 
Top