Tuesday, December 21, 2010

Unity 3D Inventory Management Project

Many people, myself included, are struggling with building a nice inventory system for their RPG. I'd like to start this next set of tutorials exploring how we can set up an inventory system. Laying out how we want to store items, what we can do with them, and how to interact with them. I'm going to try and make it as generic as possible so that it doesn't require alot of work to put into any game and will have configurable options.

So I'm going to begin by defining the various things that make up an inventory system. First it needs to be able to be applied to anything. This means that any object in the game can be a container. Normally you want to limit it to backpacks, chests, dead critters, or even the ground, and lastly the player. Next we need to decide if we can stack things together into groups. Do we want to have a set inventory size, like Dungeon Siege where objects take up space and space is limited. Do we want to allow weight where an object can only carry so much, like Everquest and Asheron's Call. Do we want to limit the number of types of items per container, like WoW's and Asheron's Call's backpack setup. With that in mind we are going to create a config that will accommodate as much as possible and give the developer a way to choose which version he wants.

Given this list of requirements we will need a couple of classes:

  • Item
  • Inventory
  • GameItem
  • GameItemInventory
  • ItemManagementConfig
Now you may be thinking that we are duplicating our classes by having an Item and a GameItem and same with Inventory and GameItemInventory. I am separating them because we want one for a data structure that can be easily passed to us from a server and one for scripts which will go on the game objects. 

You could easily argue that you could combine them, however, because we are dealing with data from the server I prefer to use separate objects and let the scripts have a reference to the class that contains their data.

So lets begin by laying out our ItemManagementConfig class. This class will define what options we will use. The enumeration does a good job for picking which of these options we will allow. I have chosen Weight, Slots, and Stackable items. We will be writing our code generically enough in the inventory and item systems that will make this work well together.


public class ItemManagementConfig {

public enum BagType { WEIGHT, SIZE, SLOTS, WEIGHT_AND_SLOTS };

public static BagType bagType = BatType.WEIGHT_AND_SLOTS;
public static bool usesStacks = true;

}


Pretty simple, but it's only our config class. Next time we will be defining our Item class and using these configurations to set up some basic stats that our generic items will have. And once we get further along we will expand our list of item types to include armor, weapons, consumables, and anything else we may need.

No comments: