Reduce the item count and remove / unequip if 0

kendogar

New member
Hey there, me again!

So I'm kind of confused here on the workflow.

I do have an equippable item that represents a seedbag and is stackable.

Let's say I do have 4x tomato seed and have the item equipped in hand, ready to place some plants:

1628088054312.png

The seed animation contains an ExecuteEvent that the item listens to, in order to validate if the player is able to place the plant wherever he tried to. That works and I am able to spawn tomatos where I want to.

Not upon that spawning I want to reduce the tomato seed count by one, ultimately unequipping and removing it from my inventory when there are no seeds left.

The things I tried:

1. Access the Item Component on the gameobject and call the Remove() function in order to remove one from the stack => did not work
2. Access the ItemObject Component on the gameobject and call SetAmount() to reduce the amount. This kind of did work, but did have no real effect in the scene.
3. Access the Inventory from the above ItemObject and try to remove one of the ItemIdentifier from the Inventory... Did not work.

I don't think my code-based approach is bringing me anywhere... Please send me off into the right direction once again! :)

Kind regards
 

Sangemdoko

Moderator
Staff member
Removing the item from the Inventory should be the way to go. I'm assuming Tomato in this case is a Common item which is Equippable through the Integration, correct? So it's a similar use case as the Grenades in the Demo scene.

In the Grenade Item script we do:
Code:
m_Inventory.AdjustItemIdentifierAmount(m_Item.ItemIdentifier, -1);
In that case the Inventory is the UCC Inventory.

In the bridge I override that Adjust function to remove the item from the UIS Inventory.

Perhaps you could try the same approach?
 

kendogar

New member
You were right with your assumption and thank you, I've managed to get the effect I was looking for!

Altough I don't know if this is the way I was supposed to do it :D It was more a trial and error kind of thing:

Code:
var itemObject = GetComponent<ItemObject>();
var playerInventory = GameManager.Instance.PlayerTransform.GetComponent<Inventory>();
playerInventory.RemoveItem(itemObject.Item.ItemDefinition, 1);

Does that look somewhat like the correct way to you?
 

Sangemdoko

Moderator
Staff member
Yes that can work.
Just out of curiosity did you try this first?

Code:
playerInventory.RemoveItem(itemObject.Item, 1);
 

Sangemdoko

Moderator
Staff member
Ah, I see... my bad, I might add an overloaded function in the next update.

The correct way to do it would have been this:
Code:
playerInventory.RemoveItem( (1, itemObject.ItemInfo) );

the inner brackets "(1, itemObject.ItemInfo)" makes a new ItemInfo with an amount of one. It keeps the reference to that exact Item with the correct ID, ItemCollection, ItemStack, etc...
Whenever you want to specify a very precise item within your Inventory (when it is unique or when you have multiple stacks) it is important to use ItemInfos to point to the exact one you want. I hope that makes sense
 

kendogar

New member
Interesting! I did not even know you could do that kind of implicit conversion.

However, trying to use that way does result in the item stack being reduced to zero, but does not unequip the item upon reaching zero.
Instead it gives this warning: Item was removed from an Item Collection which was not set on the item.

And it keeps the item equipped, preventing me from changing the equipped item altogether.

It may be very possible that this happens due to me not setting up the systems/integration perfectly, altough following tutorials.
 

Sangemdoko

Moderator
Staff member
I will need to double check, it is possible that the ItemInfo on the ItemObject doesn't match what's actually inside the Inventory. I might be setting the ItemObject ItemInfo before the item is actually moved to the Equippable ItemCollection. In the mean time you can use the ItemDefinition which worked before
 
Top