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?

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.

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.

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.

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.

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].

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org