Auto equipping weapons and action bar items

Finished the money pickups lecture and Sam mentioned using it for weapons and consumables and I just had to do it.

I found the weapon equipping pretty straightforward as compared to the actionbar stuff which I had to puzzle out but it’s working ok so far. I’m definitely missing something where you pick up several of the same item that aren’t stackable on the action bar but I’m guessing you can just design the game drops to not work like that so that wouldn’t be an issue. Can’t think of anything off the top of my head that wouldn’t be stackable that you’d want multiples of, in that slot at least.

I’m not sure how to have the item you bring in change types other than casting it as a new item so that’s what I did.

Implementation in Equipment.cs for auto equip gear

 public int AddItems(InventoryItem item, int number)
        {
            if (item is EquipableItem)
            {
                EquipableItem newItem = (EquipableItem)item;
                EquipLocation location = newItem.GetAllowedEquipLocation();
                
                if (GetItemInSlot(location) == null)
                {
                    AddItem(location, newItem);
                    return 1;
                }                
            }
            return 0;
        }

Implementation in ActionStore.cs for auto adding to action bar

public int AddItems(InventoryItem item, int number)
        {
            if (item is ActionItem)
            {
                ActionItem newItem = (ActionItem)item;
                
                foreach (var dockedItem in dockedItems)
                {
                    if (dockedItem.Value.item == newItem && newItem.IsStackable())
                    {
                        AddAction(newItem, dockedItem.Key, number);
                        return number;
                    }
                    else if (dockedItem.Value.item == newItem && !newItem.IsStackable())
                    {
                        return 0;
                    }
                }

                for (int i = 0; i < 5; i++)
                {
                    if (!dockedItems.ContainsKey(i))
                    {
                        AddAction(newItem, i, number);
                        return number;
                    }                                       
                }
            }            
            return 0;
        }

I’m still just a beginner but this is the way I figured it out. Foreach the dictionary to see if it contains the item and if it’s stackable, then add the item to the stack. If it’s not stackable but on the bar already, add the extra to the inventory.

Then if it hasn’t found the item, go through each slot looking and add it to the first empty slot. I don’t like having to hardcode the slot limit but I don’t think that limit actually exists other than by the final actionslot item in the UI setting it as 5. So it is what it is I guess.

Anyways, take a look and tell me what you think.

So far, this looks good. I’m out of time for the evening, but I’ll take a look at it over the next few days for anything I might have missed.

1 Like

Thanks. I’d love to know what can be improved on it.

Just finished the lecture on Conditions for Equipment so you can imagine my groan when I had to immediately go make that work otherwise you could cheese the system pretty hard :sweat_smile:. Thankfully, its an easy fix.

public int AddItems(InventoryItem item, int number)
        {
            if (item is EquipableItem)
            {
                EquipableItem newItem = (EquipableItem)item;
                EquipLocation location = newItem.GetAllowedEquipLocation();                
                
                if (GetItemInSlot(location) == null && newItem.CanEquip(location,this))
                {
                    AddItem(location, newItem);
                    return 1;
                }                
            }
            return 0;
        }

Just need to check CanEquip right after you check to see if the slot is open. Ezpz.

I ended up doing this challenge sometime ago, but I did refactor it out a little bit on my last post. You can use this as a reference if you need too

IActionStore: int AddItemToActionSlots(InventoryItem item, int number);
ActionStore implementation:

        public int AddItemToActionSlots(InventoryItem item, int number)
        {
            if (item.GetSlotPlacement() == EItemSlotPlacement.AbilitiesSlot)
            {
                int currentIndex = 0;
                int maxIndex = 6;
                return EquippingActionItem(item, currentIndex, maxIndex, number);
            }

            else if (item.GetSlotPlacement() == EItemSlotPlacement.ConsumablesSlot)
            {
                int currentIndex = 6;
                int maxIndex = 9;
                return EquippingActionItem(item, currentIndex, maxIndex, number);
            }
            return 0;
        }

        // PRIVATE

        int EquippingActionItem(InventoryItem item, int currentIndex, int maxIndex, int number)
        {
            for (int i = currentIndex; i < maxIndex; i++)
            {
                if (!dockedItems.ContainsKey(i))
                {
                    AddAction(item, i, number);
                    return number;
                }
                else if (dockedItems.ContainsKey(i) && dockedItems[i].item == item && item.IsStackable())
                {
                    AddAction(item, i, number);
                    return number;
                }
                else if (!dockedItems.ContainsKey(i) && !item.IsStackable())
                {
                    AddAction(item, i = +1, number);
                    return number;
                }
            }

            return 0;
        }

Autoequip equipment

IEquipStore: void AddEquipmentItem(InventoryItem item, EquipLocation location);
Equipment Implementation

        public void AddEquipmentItem(InventoryItem item, EquipLocation location)
        {
            EquipableItem equipItem = item as EquipableItem;

            if (baseStats.GetLevel() < equipItem.GetLevel())
            {
                baseStats.GetComponent<Inventory>().AddItemToFirstEmptySlot(equipItem, 1);
                return;
            }

            if (equipItem.CanEquip(location, this))
            {
                AddItem(location, equipItem);
            }
        }

Inside Inventory.cs

public bool AddToFirstEmptySlot(InventoryItem item, int number)
        {
            SetupItemPlacement(item);

            foreach (IItemStore store in GetComponents<IItemStore>())
            {
                number -= store.AddItems(item, number);
            }

            if (number <= 0) return true;

            equipment = GetComponent<Equipment>();

            if (item.GetSlotPlacement() == EItemSlotPlacement.StatsEquipableSlot)
            {
                Debug.Log($"Placement: {item.GetSlotPlacement()} / Item: {item.name}");
                foreach (IEquipStore store in GetComponents<IEquipStore>())
                {
                    EquipableItem equipableItem = item as EquipableItem;
                    EquipLocation equipLocation = equipableItem.GetAllowedEquipLocation();

                    if (equipableItem.CanEquip(equipLocation, equipment))
                    {
                        store.AddEquipmentItem(item, equipLocation);
                        return true;
                    }
                }
            }

            actionStore = GetComponent<ActionStore>();

            if (item.GetSlotPlacement() == EItemSlotPlacement.AbilitiesSlot ||
                item.GetSlotPlacement() == EItemSlotPlacement.ConsumablesSlot)
            {
                Debug.Log($"Placement: {item.GetSlotPlacement()} / Item: {item.name}");
                foreach (IActionStore store in GetComponents<IActionStore>())
                {
                    number -= store.AddItemToActionSlots(item, number);
                }

                if (number <= 0) return true;
            }

            int i = FindSlot(item);

            if (i < 0) return false;
            

            slots[i].item = item;
            slots[i].number += number;
            inventoryUpdated?.Invoke();
            abilityStore.OnEvaluate();

            return true;
        }

Privacy & Terms