Right clicking an item in the inventory - Trait issue

Hello,

I’ve seen the post between Brian and Hearbearr about right clicking from the inventory (see here: Right clicking an item in the inventory).

The solution provided works, but introduces a bug. When the item has a trait requirement (like 4 strength) I get a debug.assert notification message. The item also gets equipped when you don’t want that, since I lack the necessary trait minimum (condition is not met).
How would one check if the trait condition is met before equipping it?

I’m going to change the tag on this to Shops and Abilities, since this is where Traits are introduced, and this will make no sense to anybody taking the inventory course without Shops and Abilities

Some context for the question for the TLDR crowd:

The referenced post involves finding a way to click on an item in inventory and if it’s an EquipableItem, to automagically equip the item.

It relies on the core functionality of this method:

        public void ItemClicked()
        {
            if (GetItem() == null || GetNumber() <1) return;
            if (GetItem() is EquipableItem equipableItem)
            {
                Equipment equipment = inventory.GetComponent<Equipment>();
                EquipableItem equippedItem = equipment.GetItemInSlot(equipableItem.GetAllowedEquipLocation());
                equipment.AddItem(equipableItem.GetAllowedEquipLocation(), equipableItem);
                RemoveItems(1);
                if(equippedItem!=null) AddItems(equippedItem, 1);
            }
            
        }

The idea here is that the item is checked to see if it is an EquipableItem, and if it is, to equip it and put any item in that slot into the inventory.

What you’re looking for is a way to enforce conditions, which we introduce to EquipableItems in the Shops and Abilities course. Fortunately, in that course, we have a function added to EquipableItem to determine if an item can be equipped:

        public bool CanEquip(EquipLocation equipLocation, Equipment equipment)
        {
            if (equipLocation != allowedEquipLocation) return false;

            return equipCondition.Check(equipment.GetComponents<IPredicateEvaluator>());
        }

So all we need to do is after checking to see that the item is an EquipableItem, and getting the player’s Equipment component, we just check EquipableItem.CanEquip.

Something like this:

        public void ItemClicked()
        {
            if (GetItem() == null || GetNumber() <1) return;
            if (GetItem() is EquipableItem equipableItem)
            {
                Equipment equipment = inventory.GetComponent<Equipment>();
                if (equipableItem.CanEquip(equipableItem.GetAllowedEquipLocation, equipment)
                {
                     EquipableItem equippedItem = 
                     equipment.GetItemInSlot(equipableItem.GetAllowedEquipLocation());
                     equipment.AddItem(equipableItem.GetAllowedEquipLocation(), equipableItem);
                     RemoveItems(1);
                     if(equippedItem!=null) AddItems(equippedItem, 1);
                }
            }
            
        }

This will test any conditiosn attached to the item before allowing it to be equipped.

1 Like

Thank you Brian,

I was messing around with IPredicate on the ItemClicked and completely forgot about using CanEquip. Works like a charm! Cheers.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms