Oof. It's been a while since I've written a devlog thanks to 2018 being so... busy so far. I got some stuff done before taking a break from Space Farm that I hadn't written about yet, so I'll go ahead and cover it here alongside the stuff I got done this weekend.
This post will give a brief overview of the new inventory system, how I'm going to start managing prefabs, and my work-in-progress crafting system.
Inventory! This one is usually the stumbling block that derails my projects, but I managed to power through it this time. I put together a relatively simple inventory manager class that I can just drop on any game object that needs to hold stuff. As a companion to the manager, I've added an inventory item class that I'll be attaching to any object that can exist in the game and also in the player's inventory. This means my manager can hold an array of "inventory objects" that have data about their quantities, quality level, stack size, and so on while also holding all the info I need to place that item back in the world. Doing it this way required some elbow grease initially, as I had to go in and update all my prefabs to have both a world object class and an inventory item class, but it's been pretty straight-forward to use since that point. It's helped standardize what's going into the inventory and reduced the "cast to this object" kinda code I had floating around everywhere.
My main gripe with my inventory manager at this point is that it has a lot of functions already. I have functions for adding new items, removing items from a specific index, checking if there's already a stack of a specific item forming, checking if there's even room to add more items, and sorting the mess of items once they've been added. And the weirdest part is when I realized I had all these functions but didn't have a means of taking a specified number of an item out of a stack, like it hadn't occurred to me while writing these other dozen functions. I'm wondering how many more there will be-- I might have to come back and refactor this class before too much longer.
Prefabs! I like prefabs now that I've figured out how to use them. Unfortunately, it's easy to find yourself buried in prefabs when you're working on a game that will have a lot of different items, tools, and buildings. As per my inventory changes, everything that appears in my game world is now a prefab with a world object class and an inventory item class attached, plus the other fun stuff game objects need to exist. And any time I need to instantiate a new game object via code, I've been having to expose a variable in the inspector pane so I can drag-n-drop a specific prefab into a script. This didn't seem too bad until I had more than one tool and realized I had to attach several prefabs to my player just to give myself the items I was trying to test. I kinda pushed this problem under the rug until I got started on crafting and realized it'd be impossible to implement my solution with drag-n-drop prefabs everywhere, so I took to Google. Luckily I found a relatively simple-sounding solution-- Unity has a Resources.Load function that can be used to load up a prefab by name. It sounds like there may be some concerns about this method's efficiency, but it's an easy solution that I'll go ahead and use until I find something better. It will take another round of changes to many of my scripts and prefabs, but the pain should be worth it if I can get crafting up and running as a result.
Crafting! Yes, I decided to bite the bullet and start on the crafting system. Like the inventory system, this was uncharted waters for me, and I'm pleasantly surprised I didn't run head-first into an iceberg. I decided to use JSON despite being completely unfamiliar with JSON beyond knowing people use it for lists of stuff in Unity. Luckily there's a Unity-sponsored tutorial on the topic that helped me get it up and running, so I created a JSON file to hold the crafting recipes. Each recipe has an array of required inputs and their quantities, an array of outputs and their quantities, and a base crafting speed. I'll be using the tutorial's nifty editor window class to add new recipes to the file over time, though I may need a more robust tool in the future.
To use the crafting recipes, I have a game object set up to load in all the JSON data at the start of the game so my buildings can simply request recipes by name as needed. At the moment, I have buildings set up to check if they can make one of their recipes any time they receive an item, assuming they're set to automatically craft stuff. If so, the code will check the building's inventory for all the items the recipe needs as input and make the new item if everything's there. When I write it out like that it doesn't sound very complicated, but putting it together was a bit stressful-- I had to make some changes across a number of scripts before I could even really start testing the code I was writing. Luckily there doesn't seem to be any goofy bugs... yet. The next step will be to add more recipes and see if I can get a building to check more than one when an item comes in, so we'll see how that goes!