Ability "Inventory"

Hey I Was wondering if anyone has conquered making a separate “inventory” for abilities. It’s a little silly to me to have awesome spells and abilities just be regular inventory items (unless they are legit potions or scrolls or something that get used up. Maybe a rare totem or something makes sense with lore).

It’s rattling around in my brain how to maybe achieve this by making a bunch of new classes that inherit from the existing ones like Inventory and InventoryUI maybe. It’s just every time I sit down to start my brain locks up. I get stuck on thinking that there must be an easier way than the rabbit hole my mind goes down trying to play out how to handle this.

The general idea is going to be that abilities will be a scroll. When that scroll is used it will add the actual ability to a secondary panel that I suppose will just have to be a special inventory to keep the functionality going with dragging around and into the action bar. I can pretty that up later. But once the ability lands there it can’t be dragged to the regular inventory. Only to and from the action bar. I’d like that to be even more automated if I am being honest (right click from ability panel adds to first empty action bar slot and vice versa) but that can also come later.

Like I said. I have flashes of ways to accomplish this but something keeps sending up a red flag like I am overthinking the implementation and I’ll get stuck in the weeds.


Type up your notes here with what you have already.

People are usually more likely to help fix something you’ve worked on, rather than do your work for you.

I wasn’t asking anyone to do the work for me dude. I was more asking about general theory/feedback on how I explained I am planning to go about it.

But sure. I’ll be back in a few days and just dump hundreds of lines of code and gesture erratically.

I would start by subclassing Inventory with something like an AbilityInventory… This AbilityInventory should only accept ActionItems/AbilityItems (AbilityItems make more sense in the final course, Shops and Abilities).
We can do a bit of magic when picking up Inventory Items with an interface and some clever coding, to ensure that any Actionitem that is picked up automagically goes to the AbilityInventory

Here’s the interface, which we introduce and explain fully in Shops and Abilities:

namespace GameDevTV.Inventories
    public interface IItemStore
        int AddItems(InventoryItem item, int number);

This is simple: Any type of container you want to have on the character that can take in an InventoryItem should implement this interface.

Now in Inventory.AddToFirstEmptySlot(), add this at the beginning of the method:

            foreach (var store in GetComponents<IItemStore>())
                number -= store.AddItems(item, number);
            if (number <= 0) return true;

What this does is that whenever an item is added via AddToFirstEmptySlot(), it is first farmed out to see if any other container can use it. In Shops and Abilities, we use it so that any coin objects are automagically funneled to the purse…

Now in our AbilityInventory, we need to make this class implement the IItemStore interface, and add this method:

public int AddItems(InventoryItem item, int amount)
     if(item is ActionItem actionItem)
          int i = FindSlot(item);

            if (i < 0)
                return 0;

            slots[i].item = item;
            slots[i].number += amount;
            if (inventoryUpdated != null)
            return amount;  
       return 0;

Next, you’ll need to make a UI that reads from this AbilityInventory instead of the standard Inventory. You might make a new InventoryItemUI type class that rejects items that are not ActionItems.

Awesome Brian, thanks. This is pretty much what I had in mind. I forgot about the interface for coins, though. However I think I may even get to skip over it if “finding” an ability is just a scroll that is a normal consumable item that has an Effect to add the real ability to that secondary AbilittyInventory.

A quick update. I have implemented this pretty well. Probably not how others would, but I am happy with how it turned out. It really does a lot in the DragItem script that I had already played with for intercepting clicks to auto-equip items for example. Now it just adds to first empty slot of the new AbilityInventory. I’ve also hacked into the ActionStore/ActionSlotUI to allow right-clicking to return abilities to the AbilityInventory and regular items to the regular inventory.

My Ability Inventory will be static sized set to the maximum abilities that will be in my game so I don’t need to worry about sending something there with no room available. The right click to send to regular inventory does do a check to see if there is room and if not will spawn it on the ground. Although, I am trying to decide if I want to spend the time to figure out how to do a dynamic “inventory size” for the Ability Inventory. That way there are no empty slots showing on that screen – only the exact amount for the current amount of “learned” abilities.

All in all, I stared blankly for a couple hours trying to figure this out and take into consideration @Brian_Trotter 's suggestions and it wasn’t gelling. Then it just hit me like lightning and I was possessed for 90 minutes getting this implemented in a vastly different way.

The second part of this sentence is the important part!

This is the way

Good job getting these things into your game!

1 Like

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

Privacy & Terms