Get item from ItemSlotView

Szymon Z.

Member
Hello !

Another case :)
I prepared my own ScrollView. I have ItemContainerSlots, GridEventSystem, ItemSlots -> everything working fine. Then i created another panel, for example "statistic". And i want to have another Item View inside this panel. So I prepared another one. Now when i'm navigating through my item slots in ScrollView i want to send event which item has been selected to update data in this another Another Slot. For example if i have 20 items on my list and i will click on Sword, my another item view will update with data of Sword Item. I think it's clear what i want to achieve :) I see the event OnGridElementSelectedE(int) by this one has parameter int. How can i use this parameter to determine which item should be drawn in my another item view slots ? Or what is the correct workflow to achieve that ?
1625042852065.png


2. How to switch sprite for currently selected slot in slots container ?

3. How to bind an Item to a slot ?

For example i have 2 Slots Container. One is auto-filled based on player Inventory (A). Second one is empty at the start (B). Now i want that behaviour:
- I want to click on o slot in container A
- Then i want to click on a slot in container B
- Now the slot in container B should contain selected item
- Also list of items in container B slots should be saved. Including empty slots.
 
Last edited:
1)
I would recommend you use the events on the ItemViewSlotContainer.

1625054316347.png

To use that event you can listen to it in an intialize function or in Start/Awake like this
Code:
m_ItemViewSlotContainer.OnItemViewSlotClicked += OnItemClicked;

Where OnItemClicked is a function you create like this:
Code:
private void OnItemClicked(ItemViewSlotEventData slotEventData)
{
    //Do Stuff here when the slot is clicked. slotEventData has all the values you need about the ItemViewSlot
}

2)
You can change the image when a slot is currently selected on the ItemView prefab. We use a component called "SelectImageView" or "EquippedSelectItemView". you can add that on the Item View prefab, most stuff in the demo scene uses that:
1625054797105.png
If you need something more custom have a look at the "SelectImageView" and "EquippedSelectItemView" source code and inspire yourself from it. The important thing is the "IViewModuleSelectable" interface to know when the view is selected.

3) That's quite advanced but you can do it with some custom code.

You can use or inspire yourself from the Hotbar component to make Container B. It saves the ItemInfo that was assinged to a slot and checks to make sure the Inventory still has the item whenever there is a change in the inventory.

The more advanced part is assingnig the slot in Container B with the item in Container A. For that I would recommend a custom Item Action that can get a custom component in your scene. Then you can use custom component to listen to what slot was clicked in container A and them in container B.

Have a look at the "MoveItemAction" and "OpenOtherItemViewSlotContainerItemAction". MoveItemAction moves an item within the same slot container, OpeneOtherItemViewSlotContainerItemAction moves an Item to another SlotContainer.
There is avideo tutorial on MoveItemAction here: https://opsive.com/videos/?pid=22330&video=24159

There are no tutorials on "OpenOtherItemViewSlotContainerItemAction". But you can try to make it work with your setup and then make a copy and customize it to your liking.

I hope that helps.
 
okay thank you for advices :)


Additional to case 1, I created my own scroll-system and i can correctly "recycle" my slots in scroll-view. Just how can i now change the content of slot with next item in inventory ?
For example i have 5 slots and 6 items. I'm scrolling down, my first slot is going down and now should be filled with data of item 6. Can i get those data somewhere from Grid Inventory or something like that ?
 
Yes, If you are using an Inventory Grid, you can change the start index.

On the ItemInfoGrid that sits next to the Inventory Grid you can set the start index using the "SetIndex" function:
Code:
/// <summary>
/// Set the index.
/// </summary>
/// <param name="start">The start index.</param>
public virtual void SetIndex(int start)
{
    m_StartIndex = start;
    m_EndIndex = start + GridSizeCount;
}

This will set both the start and end index. Then the next time you call "Draw" on the InventoryGrid, it will try to draw the items between those indexes.

Since you created your custom scroll solution. I'd recommend you have a look at the "GridNavigatorBase" and "GridNavigatorWithScrollView" source code.
The Grid Navigators are specifically designed to change the start index. You'll have a lot of useful functions that you can take advantage off.
All you need is to inherit the GridNavigatorBase script and you'll be able to hook up your custom grid navigator the the ItemInfoGrid.

I hope that helps
 
Top