Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (581)
games submitted by our members
Games in WIP (500)
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 1176 times)
0 Members and 1 Guest are viewing this topic.
Offline Hazzy

Senior Newbie





« Posted 2013-10-19 19: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?

Thanks!
Offline zngga
« Reply #1 - Posted 2013-10-19 19: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 opiop65

JGO Kernel


Medals: 124
Projects: 7
Exp: 3 years


Team Alluminum


« Reply #2 - Posted 2013-10-19 20: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 20: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

Check out my Devblog! Smiley
Offline Hazzy

Senior Newbie





« Reply #4 - Posted 2013-10-20 01: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 05: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 05: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 05: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  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (53 views)
2014-04-15 18:08:23

BurntPizza (51 views)
2014-04-15 03:46:01

UprightPath (65 views)
2014-04-14 17:39:50

UprightPath (48 views)
2014-04-14 17:35:47

Porlus (65 views)
2014-04-14 15:48:38

tom_mai78101 (89 views)
2014-04-10 04:04:31

BurntPizza (149 views)
2014-04-08 23:06:04

tom_mai78101 (245 views)
2014-04-05 13:34:39

trollwarrior1 (203 views)
2014-04-04 12:06:45

CJLetsGame (210 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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
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!