Equip on Pickup

@Brian_Trotter
@sampattuzzi

I’ve gone over the videos, and I’m still not grasping how to weald this code.

I’m trying to equip a weapon on pickup if we are unarmed. Or any equipment slot for that matter, auto equip empty equipment slots.

I’ve narrowed the process down to this function with the pseudo code that I want to do… this is in the inventory.cs… I have TODO in the summaries as well…

/// <summary>  //TODO this is where it first looks for an inv slot, but if equip slot is null, don't bother
        /// Attempt to add the items to the first available slot.
        /// </summary>  // TODO this is where we check if it is quippable and if so is that slot empty.. equip it
        /// <param name="item">The item to add.</param>
        /// <param name="number">The number to add.</param>
        /// <returns>Whether or not the item could be added.</returns>
        public bool AddToFirstEmptySlot(InventoryItem item, int number)
        {
            // TODO
            // if this is an equippable item
            // check if that equipment slot is null
            // if so equip item
            int i = FindSlot(item);

            if (i < 0)
            {
                return false;
            }

            slots[i].item = item;
            slots[i].number += number;
            if (inventoryUpdated != null)
            {
                inventoryUpdated();
            }
            return true;
        }

I’m fairly certain this is where I want to do this, but I’m open to suggestions if there is a better way…

I don’t know what to actually check to see if this is an equippable item… or how to gather if it’s a weapon or helm.

but I think I’ve also narrowed down where to do the checking and equipping from the Equipment.cs script…TODOs in the summaries

/// <summary>
        /// Return the item in the given equip location.  // TODO this is where we can check to equip on pickup
        /// </summary>
        public EquipableItem GetItemInSlot(EquipLocation equipLocation)
        {
            if (!equippedItems.ContainsKey(equipLocation))
            {
                return null;
            }

            return equippedItems[equipLocation];
        }

        /// <summary>  // TODO this is where we can eqiup it on pickup if check is empty slot
        /// Add an item to the given equip location. Do not attempt to equip to
        /// an incompatible slot.
        /// </summary>
        public void AddItem(EquipLocation slot, EquipableItem item)
        {
            Debug.Assert(item.GetAllowedEquipLocation() == slot);

            equippedItems[slot] = item;

            if (equipmentUpdated != null)
            {
                equipmentUpdated();
            }
        }

How do I check if an inventory item is equippable? I need to start there…

@sampattuzzi
@Brian_Trotter

any hint as to what my first if statement would be? Or if I’m starting in the right place?

For checking if an inventory item is equipable, you need to cast it
EquipableItem item = (EquipableItem)inventoryItem;
if(item)
{
it’s equipable, do your thing
}

1 Like

THANK YOU!.. casing using “as” skips the error (saw that on some googling rabbit hole, so I applied it here) , therefore if the cast of the inventory item is invalid it leaves the var null, and so the following code makes healthy hat print donkey… health pickups on the other hand don’t… I’m fairly certain I can achieve equip on pickup from here, thanks again for the help! I would never had known to try to cast… that was beyond me.

EquipableItem itemToTest = item as EquipableItem;
            if (itemToTest)
            {
                print("donkey");
            }

With that one hint, I was able to code a system that looks to see if it’s equippable, if there isn’t anything in that slot, equip it automatically, unless that slot is occupied, then attempt to put it in inventory, OR if it isn’t equippable, attempt to put in inventory, and prep an inventory is full method for the endpoint of the system (which I’ll wire up with an event to trigger some feedback, will have to practice my Dwarven Scottish accent)… thanks again!

… hmm maybe now I’ll wire up actions to go right to the action bar if it isn’t full… new challenge!

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

Privacy & Terms