Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (741)
Games in Android Showcase (225)
games submitted by our members
Games in WIP (823)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Handling RPG Inventory  (Read 4882 times)
0 Members and 1 Guest are viewing this topic.
Offline Hazzy

Senior Newbie

« Posted 2013-10-19 17:09:57 »

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?

Offline zngga
« Reply #1 - Posted 2013-10-19 17:58:14 »

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.

My code never has bugs... it just develops unexpected features!
Offline Opiop
« Reply #2 - Posted 2013-10-19 18:15:53 »

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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Mac70
« Reply #3 - Posted 2013-10-19 18:21:52 »

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
Offline Hazzy

Senior Newbie

« Reply #4 - Posted 2013-10-19 23:10:45 »

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!
Offline UprightPath
« Reply #5 - Posted 2013-10-20 03:24:11 »

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.

Offline tyeeeee1
« Reply #6 - Posted 2013-10-20 03:44:43 »

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

JGO Coder

Medals: 17

« Reply #7 - Posted 2013-10-20 03:54:08 »

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].
Pages: [1]
  ignore  |  Print  

xxMrPHDxx (21 views)
2017-11-21 16:21:00

xxMrPHDxx (14 views)
2017-11-21 16:14:31

xxMrPHDxx (16 views)
2017-11-21 16:10:57

Ecumene (114 views)
2017-09-30 02:57:34

theagentd (150 views)
2017-09-26 18:23:31

cybrmynd (260 views)
2017-08-02 12:28:51

cybrmynd (251 views)
2017-08-02 12:19:43

cybrmynd (247 views)
2017-08-02 12:18:09

Sralse (260 views)
2017-07-25 17:13:48

Archive (881 views)
2017-04-27 17:45:51
List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51 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‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!