Removing equipped item from inventory when durability is depleted.

sovereign

New member
First of all I would like to say I am still very confused trying to understand how many concepts of the UIS work so the more detailed you are the better. I managed to set up equipping/unequipping weapons from the inventory(drag and drop to equip slots) through the Equipper.cs. My weapons logic is a monobehaviour on the weapon prefab and its unrelated with UIS (also i would like to add that I am not using any opsive controller but a custom one i made). The thing is, that weapons have durability that goes down with each shot, and when durability gets depleted(<1) the weapon gets destroyed so does its gameobject.
My issue now is when the weapon gets destroyed, It is still equipped as an item on the equipment slot and therefore also still exists inside the main inventory, which means that if i unequip the weapon and then equip it again there it is again a new instance of that weapon as a gameobject with full durability.
I want to be able to unequip the weapon item but also completely remove it from the main inventory.

What i attempted with failed results is adding an ItemObject MonoBehaviour on the weapon game object, setting the weapon item definition as its item, and then through code accessing the ItemInfo and trying to remove it from the main inventory. (See code below for a more clear view of what i am saying here).

C#:
   public virtual void HandleWeaponDurability()
        {
            if ((durability -= durabilityLossPerShot) <= 0)
            {
                Debug.Log("Durability Depleted");
                DestroyWeapon();
            }
        }

        private void DestroyWeapon()
        {
            ItemInfo equippedItemInfo = GetComponent<ItemObject>().ItemInfo;
            playerCharacter.Inventory.RemoveItem(equippedItemInfo);

            Destroy(this.gameObject, 0.3f);
        }

P.S. In general I would like to handle something like an OnWeaponEquip and OnWeaponUnequip event that i can subscribe to because Armor and Weapon will also have stats that i want to manipulate. I think this goes hand in hand with the above, so if the solution for the main issue can cover this also all the better. I tried looking at the demo but I am lost i could not understand what happens where I prefer to simplify the logic to my understanding
 

Sangemdoko

Moderator
Staff member
My guess is that the item is not being removed because the inventory does not know where the item is.
Try removing the item from the equipped item collection instead of removing it from the inventory.
Code:
//Untested code
playerCharacter.Inventory.GetItemCollection("Equipped").RemoveItem(equippedItemInfo);

Let me know if that works.

For knowing when an item is equipped unequipped you may use the events to know when it is added or removed from the equipped item collection.

Also feel free to make your own Equipper or build on top of the existing one. You will have a lot more freedom to choose exactly how your items are equipped.
 

sovereign

New member
My guess is that the item is not being removed because the inventory does not know where the item is.
Try removing the item from the equipped item collection instead of removing it from the inventory.
Code:
//Untested code
playerCharacter.Inventory.GetItemCollection("Equipped").RemoveItem(equippedItemInfo);

Let me know if that works.

For knowing when an item is equipped unequipped you may use the events to know when it is added or removed from the equipped item collection.

Also feel free to make your own Equipper or build on top of the existing one. You will have a lot more freedom to choose exactly how your items are equipped.
1) Wow it was that simple I was indeed close. Yes this worked perfectly thank you (using the purpose since i might change inventory names in the future):

playerCharacter.Inventory.GetItemCollection(ItemCollectionPurpose.Equipped).RemoveItem(equippedItemInfo);

Is it really the correct approach i took the one with the ItemObject component on weapons prefab? Or would you suggest another approach I could explore?

2) The thing with the equipper is i tried extending it to customize it but truth is it is too complicated for me both Equipper.cs and DemoCharacterEquipper.cs . If you or another user have a more simplified version laying around please feel free to share it.

3)As for the stats can I use this EventHandler.RegisterEvent<ItemInfo, ItemStack>(m_Inventory, EventNames.c_Inventory_OnAdd_ItemInfo_ItemStack, OnAddedItemToInventory); to subscribe to that event in another class? if i did that how can i get the weapon instance of that ItemStack or ItemInfo(still dont understand their difference) and have access to the properties i set in the weapon code?

4) What is the difference between ItemStack and ItemInfo?
 

Sangemdoko

Moderator
Staff member
1) I'm glad that worked

2) The Equipper script is very bear bones right now. It is something I plan to improve on in the future. If it's not convenient for you I'd recommend you create your own from scratch. All you need to do is create a component that listens to the Inventory On Add/Remove events, to know when to equip and unequip items. The way you equip items is completely up to you.

3)If you create your own Equipper script you could incorporate stats directly in the same component. That's one of the advantages of creating you own. Plus you'll have access to your Item gameobject that way.
otherwise you could use a function like
Code:
//If your Item is Unique you may get the Item Object from the Item
var itemObject = newItem.GetLastItemObject();
But if you have multiple itemObjects for a single Item that might cause you some issues
Check more examples on getting ItemObjectsfrom an item Here:

4)If you are confused between ItemStack, ItemInfo, etc... make sure to read the Terminologies in the documentation

In the On Add Event, the ItemInfo is that original item that was supposed to be added, and the Item Stack is the actually added item. Meaning you can get where the Item came from before it was equipped by checking the ItemCollection or Inventory of the ItemInfo.
 
Top