Previously On IT Ninja . . .
Last time I got busy making some noise! That’s right, I added a system to my game to control and play sounds. Next up on the chopping block is a few related things, inventory, items, loot tables, and the like. I have already begun partially working on these things but I need to seriously dip in further. I’m sure I’ll have to make some extensive modifications to the stuff I’ve already got too.
And Now On IT Ninja . . .
I already have a custom class for items. I’ve got a list of items associated with the player’s inventory. But I think I need to change something about how that works. Right now I’m placing an Item directly into the list, so that thing holds all the information. Instead, I should convert that list to something like integers to just hold the ItemID, then use that to pull the item information out of an ItemDatabase. That way I can associate sprites directly with the item. From my experience you can’t include sprites in data you’re serializing, or something like that.
With that done it’s time to figure out how to create a database of items that exist in the game. I found this handy tutorial to work from. As I’m working through this tutorial I realize I need to do something a little different with the way I handle inventory (beyond the change I just made). I need a way to assign a quantity to the item in the inventory, maybe.
I also see that I might be doing things all wrong when it comes to things like items and quests. You know what, for now it work, so I’m going to leave it the way it is. It’s possible I should go back and watch the tutorials from this guy that lead up to this one so I have a better understanding of how he’s doing everything. So I went back and watched another tutorial to redo my item system properly.
Now that that’s fixed I can jump back into my item definitions. There is one small problem though, I use an enumerator in my item class, and I need to figure out how to properly address that in creating items. In the end, it really wasn’t all that hard to do. Although, now that I’m looking at it, I may just want to use a dictionary instead of an enumeration.
So, after all this I’ve run into a problem that I’m fighting. I need to be able to save the player’s inventory. Normally, that would be pretty easy, I could just have a list with the ItemID for each item the player holds, but I want a way to store a quantity associated with that number. Originally, I was going with a dictionary to do that, but then I found out you can’t serialize a dictionary in Unity. . .
I’ve been wracking my head to come up with a solution and I just can’t find anything. There is a custom solution or asset or something, I don’t know what exactly it is, that I can install to add a serializable dictionary but I don’t want to do that. Maybe I should just use a list and for each item add it again? Then count how many times the item is in the list? No, that’s a bad solution for removing items or adding more than one. Plus, I’ve read that large lists are slow.
In the end I created two new functions. One takes the dictionary I’m using to store the players inventory and splits it into two lists, which can then be saved. The other takes the two lists after loading and converts them into the player’s inventory dictionary. It took a bit of trial and error until I got it working right, but now it seems to be done.
As part of my inventory testing I also managed to create some collectible resources I can place in the world. It is a single prefab that I can drop in and modify as I see fit. It’s very customizable, uses the sound system, multiple sprites, and can even disappear. There is one small problem in they come back every time the scene is loaded. . .
Next Time On IT Ninja . . .
Now that I’ve got a working inventory system it’s time to move on to another important part of my game. I have to begin work on a minigame! You got that right, a minigame. . . I’m sure I’ll be adding a bit more to the inventory system (specifically adding more items to the database and a loot table system) but the next big hurdle will be something completely new. . .