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.