Handling RPG Inventory

I’m working on a RPG engine and I’m considering on how to build the item classes. So far I have an abstract superclass called Item and I am creating new classes for each item that extends that class. I don’t think it’s a good idea though so I’m considering keeping the same Item class and saving items as XML files which can then be loaded when needed. XML would also allow for the items to be implemented outside of the source code and during run time.

I’m using XML as an example here but it’s most likely going to be my choice unless someone can suggest a better method?

Thanks!

I think that this is a great way to handle items.
For RPGs you typically have lots of items in the game, some times approaching the hundreds to thousands of unique items the player can interact with. Having that many individual classes would be ridiculous and un-maintainable, so I think you have made a good choice.

As far as using XML or other methods, such as JSON, well, that is entirely up to you. As long as your program can read and write to it, and you understand it, you could write bytes to a text file for all the difference it would make.

An added bonus of implementing items this way is the ability to add items later without to much work. However, doing items this way introduces a ton of work up front in the form of decoding, encoding, and determining the functionality of the item from what you decoded.

Keep going, and I would love to see what you come up with.

Yeah, create a parser for your files, and just store the type of item, the stats, maybe a location to an image for the item etc… and then just create the files and load them into the game every time you start it up. You’ll need to code the item classes, though. So say you have a weapon. Well, you’ll need to make a weapon class. If its a potion, you’ll need a potion. Just specify in the file what type of item it is, then create a new instance of it. Its really very simple after you finish your parser.

Consider using Kryo - very easy to use, fast serialization library. Using CompatibleFieldSerializer you can even make any changes except variable type changes in your class.

By the way, don’t forget about making variables you don’t want to serialize transient - this helps a lot when you don’t want to save the whole data and try to not write boilerplate code. :wink:

Making separate files rather than classes seems like a much better idea and I’m glad I took a moment to consider it. I’ve pretty much decided on going for an XML structure for this now.

Thanks for the input!

XML or JSON, it doesn’t matter too much. For what you’re describing, I’d try to do something around the idea of the ‘Decorator Pattern’ with the decorations being item effects. By making a set of generic effects that an item can have: Return X HP, Return X MP, Remove X HP, Remove X MP, Increases Y Stat by X, etc… You can use the XML (Or JSON) files to build your items.

Basically, you’d end up with an Item object which is a container for several ‘Effect’ decorations then each Item type (Equipment, Consumable, Weapon, etc.) would have their own rules for how these Effects are applied. Such as Equipment causing a persist set of effects, or an on activation effect. That sort of thing.

I just create an enum for each item type, then load everything into the game from XML files when the program is launched.

The Item itself is pretty straight forward. Yes I think you’ve made the correct decision by having a single class because generally items don’t have different behavior. Instead they usually differ by type and stats. Item stats can be as simple as a Map of Stat -> Int (e.g. STR -> 5)

Regarding inventory is where it gets a bit more complicated. Items can be carried, equipped, put in bags, can be a bag, and exist independently e.g. they’re lying on the ground.

Inventory itself can be as simple as a list. However, if you have an interactive GUI like Diablo then you’ll need to deal with inventory slots and items occupying them.

Equipment is by its nature a bit more complicated. Items can be equipped in certain slots e.g. worn on head, around wrists, carried, wielded by one or two hands. To do this I used a map of Slot -> List[Item].