Equiping a left hand and right hand weapon.

Shade

New member
TLDR: I want to drop an item in either the left hand slot or the right hand slot, get the name of the slot i dropped the item in, then pass the value to another system. Below is the code, which is from a characterEquipper script derived from Equipper, like in the demo character equipper.

Code:
public override bool Equip(Item item, int index)
    {     
        var result = base.Equip(item, index);       
        var Slotsx = m_Slots[index];
        vWeapon = item.GetItemObjectAt(index).GetComponentInChildren<vMeleeWeapon>();
        if (Slotsx.Name.Contains("Right Hand"))
        {
            Debug.Log("EQUIP RIGHT HAND");
            meleeManager.SetRightWeapon(vWeapon);
        }
        if (Slotsx.Name.Contains("Left Hand"))
        {
            Debug.Log("EQUIP LEFT HAND");
            meleeManager.SetLeftWeapon(vWeapon);
        }
    }

The code is treating whatever hand i drop the item in as the "Right hand", and the second item i drop in the remaining hand as "Left Hand". It doesn't matter if i drop it in the left hand slot, or the right hand slot. I am about 2 days into using the system, so it may be something stupid, or i could be doing this the wrong way. But i need some way to get the proper hand to build the logic around. I want to get the slot currently used, and the weapon to be equipped (not just index 0's slot).
 
If I understand correctly you are drag & dropping the item from your main inventory UI to your equipment UI to equip it correct?

And for some reason whether you drop it in slot 0 or 1 it equips in slot 0 first.

My guess is that the drop action isn't telling the collection in which slot the item should be added. It just tells it to add it to the equipped collection.

In theory in should work correctly if you are using the Contain Smart Exchange action.


EDIT: I've been looking at the code and something does seem off in the Equipper script.
It seems like the ItemSlotCollection and the Equipper slots and not synced...


Here are the changes I made, let me know if it fixes your issue

In the Item Slot Collection script add this function:
Code:
/// <summary>
/// Returns the slot where the item is set.
/// </summary>
/// <param name="itemStack">The item that is in the collection.</param>
/// <returns>The slot where the item is equipped. -1 indicates no slot.</returns>
public int GetItemSlotIndex(ItemStack itemStack)
{
    for (int i = 0; i < m_ItemsBySlot.Length; i++) {
        if (m_ItemsBySlot[i] == itemStack) { return i; }
    }

    return -1;
}

In the Equipper script make the m_EquippedItemCollection an ItemSlotCollection instead of a simple ItemCollection
You can cast the ItemCollection in the start funtion
Code:
/// <summary>
        /// Initialize the Equiper.
        /// </summary>
        protected virtual void Start()
        {
            if (m_Inventory == null) { m_Inventory = GetComponent<Inventory>(); }
            m_EquipmentItemCollection = m_Inventory.GetItemCollection(m_EquipmentItemCollectionID) as ItemSlotCollection;
            
            ...


Then replace the following function:
Code:
/// <summary>
/// Equip item that was added to the equipment collection.
/// </summary>
/// <param name="originItemInfo">The origin Item info.</param>
/// /// <param name="addedItemStack">The added item stack.</param>
private void OnAddedItemToInventory(ItemInfo originItemInfo, ItemStack addedItemStack)
{
    if (addedItemStack == null) { return; }
    
    if (addedItemStack.ItemCollection == m_EquipmentItemCollection) {
        var index = m_EquipmentItemCollection.GetItemSlotIndex(addedItemStack);
        Equip(addedItemStack.Item, index);
    }

}

With those changes the index should be specified and the system won't just guess where the item should be equipped
 
Thanks for the reply. Copy and pasted your code and got the following error, even with casting m_EquipmentItemCollection as an ItemSlotCollection, it's not able to access anything in item slot collection. I double checked, i'm using InventoryCollections Directive. Not entirely sure why this isn't working.

Code:
Error    CS1061    'ItemCollection' does not contain a definition for 'GetItemSlotIndex' and no accessible extension method 'GetItemSlotIndex' accepting a first argument of type 'ItemCollection' could be found (are you missing a using directive or an assembly reference?)
 
Sorry I wasn't clear.

Of course you need to define the ItemCollection as an ItemSlotCollection.

So replace this (on line 40 more or less)
Code:
protected ItemSlotCollection m_EquipmentItemCollection;
 
Sorry I wasn't clear.

Of course you need to define the ItemCollection as an ItemSlotCollection.

So replace this (on line 40 more or less)
Code:
protected ItemSlotCollection m_EquipmentItemCollection;
I totally missed that. It's working now, and i'm getting the slots index and name. Thanks again!
 
Top