Move Item to Collection Using API does not Update Inventory in Inspector

pako

Member
Hi,

First, I'll give some context:

I have the Player's Inventory with a Main and an Equipped Collection, which works well for Equipping/Unequipping Items.

I also have a Shop Inventory with a "Shop" Main ItemCollection (for all Items available for purchase), and a "Locked" Secondary ItemCollection, which contains all Items that are not yet available for Purchase (depending on the "UnlockLevel" int Attribute). I'm thinking that I should probably configure the Main Collection as an ItemSlotCollection, since these Items are displayed in a Grid List, but I don't want to make any changes at this point, before consulting you.

An UnlockController Component on the same GameObject as the Shop, checks the current game level and compares it to the "UnlockLevel" Attribute of each Item in the "Locked" Collection. If the UnlockLevel is smaller than or equal to the current game Level, it "moves" the Item from the Locked Collection to the Shop Collection, i.e., removes it from the first and adds it to the second. I've tried this while iterating either through "lockedItemStacks" or "lockedItemInfos", using the following:

Get reference to the Inventory collections:
ItemCollection lockedItemCollection = shopInventory.GetItemCollection(lockedCollectionID);
ItemCollection shopItemCollection = shopInventory.GetItemCollection(shopCollectionID);

Method A - Direct Add/Remove.

shopItemCollection.AddItem(lockedItemStacks.Item, quantityToMove)
and
lockedItemCollection.RemoveItem(lockedItemStacks.Item, quantityToMove)

Method B - Using an ItemActionSet and getting a reference to a MoveToCollectionItemAction (after correctly configuring it).

moveToCollectionItemAction.Initialize(false);
moveToCollectionItemAction.InvokeAction
(
lockedItemInfos,
shopInventory.ItemUser
);

As I step through code and inspect the contents of the collections, the correct Items get moved from the Locked to the Shop Collection, with both the above methods.

However, the Unity Inspector shows these collections unaltered, and the Items are still displayed as locked in the Shop Grid List, probably as a consequence.

I still can't figure out why this is happening.

I'd really appreciate your help.

Thanks in advance!
 
Hello :)

Rather than having two ItemCollections and adding/remove/moving items around. Why not have an EmptyItemCollection and an ItemDefinitionAmounts.

You'd define all your items for your shop in ItemDefinitionAmounts, and each time, just before you open your shop UI you clear the shopItemCollection and add newly created items by looping over all the ItemDefinitionAmounts and checking their ItemDefinitionAttribute.


As for why the add/remove doesn't work for you, no idea. Are you sure "quanittyToMove" is not 0? Are you sure your checking the correct Inventory, maybe you have multiple shops in the scene? I can't think of any reason it wouldn't work
 
Hi (again) :)

Thank you for your answer.

About the methods I use:
I set int quantityToMove = lockedItemInfos. Amount, and have checked multiple times that the quantity is 1.

I'm sure I'm checking the correct Inventory, since I set it in the Inspector, and also verified its value at runtime by stepping through code. Also, there are only 2 inventories in the scene. One is on the Player GameObject, and the other (the one I'm checking) is on the "Shop Menu" GameObject. So, I can see while debugging that the inventory is on the "Shop Menu" GameObject, which is the same as the one whose Collections I check in the Inspector. While the fields in the code contain the correct Items, the UI collections of the same inventory are not updated...

And there's just 1 shop in the game.

About my settings: I'm not sure which inventory to set in the "Shop Menu" component. Initially, I had set it to the Player's inventory, but then, during my tests, I set it to the Shop inventory, and got a warning that there's no currency owner. So, I added a currency owner component (empty), the warning went away, but it doesn't make any difference for the unlocked items. Could you please tell me which inventory I should set in the "Shop Menu" component?

I've really run out of ideas of why this is happening, so I'd like to try your method, but I'm not quite clear what you mean.

EDIT: Forget I said these:
---------------------

You say, "have an EmptyItemCollection and an ItemDefinitionAmounts". I'm not sure what you mean by that, and what ItemDefinitionAmounts would represent. Also, you say "You'd define all your items for your shop in ItemDefinitionAmounts". But I don't understand how to do that.

Could you please be a bit more specific?
-------------------------------

So, on second thoughts...
I found the struct definition ItemDefinitionAmount, which I hadn't used before. So, now I think I get what you mean. The shopItemCollection must be the EmptyItemCollection that you refer to, after removing all items from it, and then I should add the correct items to it after that shop UI starts up.

Well, since in my use case the amount of each definition will always be 1, I used an ItemDefinition List to hold all definitions, and iterate through, instead of an ItemDefinitionAmount List.

I tried it, and the result is the same as before 😀 : The shopItemCollection gets cleared and Items are added to correctly, but the same Collection in the Inspector shows the initial items, no change!

So, I have to dig deeper to see what's going on. But in any case I think your method is simpler.


Thank you in advance.
 
Last edited:
I found the problem!

The Inventory Saver LoadOnStart was enabled, and it apparently overwrites the changes made by the UnlockController. I don't know why this is happening, since I have set the ExecutionOrder of the UnlockController to run after the Save System, and both code runs on Start().

Anyway, I use your method for unlocking Items now, so I don't need an Inventory Saver for the Shop Inventory.

Thanks again!
 
Back
Top