This is my first real attempt at an inventory system but I think I did a pretty decent job so far. I am creating this using the top down blueprint template with the starter stuff included.
I’m just going to give screen shots and a high level overview, I believe most people here should be able to figure out how to replicate my process if they want to. If anything needs expanding let me know and I can edit the post as needed.
- Create a basic mesh as a place holder for an item, set up collision, and enable physics
- Create a blueprint from this item (BaseItem)
Create the following variables:
- Name - Name
- Bool - Stackable
- Integer - Current Stack Size (1)
- Integer - Max Stack Size (1)
- Created as a child blueprint of BaseItem
- Set Stackable to true
- Set Max Stack Size to 10 for testing purposes
Create a new blueprint -> Actor and named BaseInventory
Added a sphere component and set radius to 100
Created the following variables:
Integer - Max Inventory Size
Array of Actors - BaseItem_C
Bool - Has Partial Stack
Integer - Item Slot Number
Created the following Macros and 1 Function:
- Event Graph
Whenever the sphere around the inventory, which is now attached to the pawn in the pawn BP, collides with BaseItem or a child, proceed to see if the item can be collected.
- Macro - Can it stack
This is very straight forward. If the item isn’t a stack able item it skips to checking for inventory space. This is designed to skip over items like equipment.
- Macro - Check for partial stacks part 1
This part looks complicated but I hope I can explain it well. This part searches the existing inventory array to see if the incoming item name matches the name of any of the array elements AND if that element isn’t a full stack. If it cannot find an item matching the above condition, it sets the bool (has partial) to false so that when the loop ends it can bail out and jump to the next macro. If the condition is met however, it sets the bool to true, and sets the Item Slot Number integer to the array index, and bails out of the loop since we don’t need to look for more items with this condition.
- Macro - Check for partial stacks part 2
Since we did find a partial stack, lets get the reference to that item by searching our inventory again for the array index that was previously set. Once we find said item, lets get its stack size and increment it +1 and destroy the incoming item since we don’t need to see if any additional spots are open in our inventory.
- Macro - Check for inventory space
This is pretty basic. If the number of items in our inventory is less than the total number of items specified in the Max Inventory Size integer, then move on to the following function. If it is greater however, bail out with a simple error message.
- Function - Add item to inventory
This simply adds the incoming item to our inventory array and destroys the mesh
Created a sphere component around the pawn with a scale of 3 for XYZ.
This event graph spawns an invisible reference to the inventory actor which, using the sphere generated above, is used to detect when the player runs over a collectible item.
Please be gentle
PS: please disregard the attachment file for the Add Item that includes the Last Item, Get, and Set functions. They are not needed.