Monday 8 October 2018

Creating the Inventory System

Last week I started work on implementing the inventory system that will be used in my game. Here I'm going to document how I managed to simplify it into simple steps that I could implement one at a time to get to a system that functions as you would expect from a classic RPG inventory system.

The first step I took towards creating the inventory was implementing the equipment for the character. I had a good idea of how I wanted it displayed and what equipment to be displayed, so I got to work on creating the base visual elements I needed and got to work building an example tooltip that would be displayed when hovering over an item.


I managed to make a good system for the tool tip to be displayed by using various elements of unity. The ContentSizeFitter element allows the children of the tooltip to expand it, something you'd expect from a tooltip. 

After creating the base template I went to work creating an equipment class that could hold the various stats of that specific equipment. To hold the various equipment the player had equipped I decided to have them implemented as an array of equipment in another class (you are probably better of using JSON)

I went to work making the tooltip pull data from the right point in the array that corresponds with the item that's hovered. I managed this by having each equipment slot having a public variable that I can change in the editor, allowing me to set each equipment slot to a integer that points to that number in the array. The script attached to each inventory slot allows the tooltip to be shown when hovered over and changes the tooltip to be visible and it's content to be changed, pulling it's stats from the ones stored in the array.


From here I decided to develop the inventory side which would hold all the other items and equipment that the player did not have equipped. To start, I made it look more like an inventory with some additional assets and then made the inventory button open and close it, as you'd expect. Some various fixes went in to this step as well, such as not moving the map when the inventory is open.

The ContentSizeFitter and GridLayout helped me get the slots looking nice and organized!

So, between the next step I implemented the first version of the Item class, storing similar variables to the equipment class but without the stats. Most of the stuff here was behind the scenes so not much to show, but here is where I made the tooltip switch from displaying left of the mouse to right of the mouse depending on which side of the screen the mouse is hovered over (again one of those simple things that you come to expect).

The next big step of the process was creating drag and drop features! This was fun. I started by making a smaller image of the item that the player is dragging under the mouse. This quickly evolved to being able to drag items and equipment to other slots although it definitely wasn't perfect. A lot of polish was needed in the next steps!

Yes, you could equip apples.

I had a lot to get sorted in the next step. The way items and equipment interacted when moving with each other wasn't very logical, the inventory slots stored item variables and the equipment slots stored equipment variables... Causing a lot of unwanted behaviour (you can see when the helmet is moved in the video it's stats stay in the slot!)

My solution seemed quite obvious after awhile. I needed to use polymorphism between the item and equipment classes. The Equipment class would inherit from the Item class and various item functions needed to be changed to virtual functions to allow the Equipment functions to override them if needed. For example the tooltip for item needed to use it's name and item type, whereas the tooltip for equipment needed all the stats added in too.

Item classes moving function
 
There was a very long day of figuring out logic due to the interactions between the Item class and the Equipment class during switching positions with each other. When moving an item with a piece of equipment there is logic required that I didn't expect. 

There has to be many constraints; you can't move an item in to an equipment slot, equipment of a specific type could only be equipped on their assigned slot (helmets to helmet slot, rings to ring slots etc) and empty slots need to be able to switch places with anything unless in the equipment slots (this stumped me for a while). This required several checks when moving an item and several more when moving equipment.

 
 SOME of the spaghetti that is the equipment move checks

I think I'd have to write a whole book to document it here, but solving the problems required a lot of debugging and scuffed MS Paint drawings to understand the logic. Maybe I'll write more about it some day. But ultimately after I solved these logic problems it was mostly finished.

As the finishing touch, I made it so that the stats gained from equipment were updated on equipment change and added a few more placeholder assets. It kind of even feels like a proper inventory now! Here is the final product.



I still have many features to implement, saving and loading equipment/inventory most notably. But right now it's at stage where I'm able to work on the next step - the battle system. Thanks for reading!

No comments:

Post a Comment