I'm making a horror game #09
Inventory items need to stack!
I'M MAKING A HORROR GAME
Welcome to another I'm making a horror game post. During my last stream, I changed the way the inventory is set up in order to make items stack. Most items will stack up to a maximum instead of being unique items taking up a slot each.
The initial implementation of the Inventory was simple and straightforward. You pick up an item, it gets added, so on and so forth. Each item was unique and you could only hold a couple of them.
Of course, this meant that the player had to be more mindful of which items they picked up. However after asking a few people, I came to the conclusion that allowing certain items to stack would lead to a better experience.
With this new idea in mind, I changed the way the inventory worked and in the process, I broke everything! I spent a couple of days fixing everything, with extra time during stream improving it.
To introduce stacking in the inventory, we first have to check if the inventory has reached its capacity. If it hasn't, we then validate that item exists in the inventory. At that point, we need to search for the first item in the inventory that is the same type, but hasn't reached its Max Stack value.
Once that item has been identified, we then set the array element to basically the same thing, while increasing its Current Stack (and in my case Quantity) value(s). We then remove the actor from the world (since we picked it up.)
It took a while for it to work, but it works! Though there was one improvement I wanted to make after testing it out - making it so using an item from a stack would replenish the stack if the player had more items of the same type in their inventory.
Thanks for reading!
After all these changes to the inventory, I'm having to revisit a lot of my code. I've actually got a growing list of bugs to tackle! The quick wheel feature also broke quite a bit. Either way, I'm having a blast working through this!
I'll see you for part #10!
For what I call the auto-stacking functionality, I had to go through the Items array once more, but this time there were a lot more conditions before I could find the target item. First, I had to make sure the item I chose wasn't the current item I had just used/selected (otherwise the player could infinitely use a single item stack and we don't want that.)
Then, I needed to validate that the item was the same by comparing the data table row name while also making sure that the stack was further in the array as opposed to before. For the latter part, I found this out while testing. If I had 3 different stacks of the same item, using an item in stack #2 would take one from stack #1 and refill stack #2... this obviously made no sense, so I had to fix it.
Finally, we validate that the stack we're "stealing" from, has more than one. In the event that it doesn't, we remove the extra item from the array while increasing the current stack's count. If it has more than one, we just "steal" the item and add it to the current stack.
Of course, if no other stack is found, then all of this doesn't happen.