Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (711)
Games in Android Showcase (213)
games submitted by our members
Games in WIP (785)
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  
  Item management systems.  (Read 1974 times)
0 Members and 1 Guest are viewing this topic.
Offline DecafJava

Senior Newbie

« Posted 2007-11-30 01:58:47 »

I have a few questions concerning item management systems.  First of all, if a player has an inventory to store certain items, where would you actually store the items in the program? Would you use some kind of array? And how would you name multiples of the same item, with a number after their name? So would you just use for loop to make, for example, item1, item2, item3, etc.? Almost done. Smiley How do you deal with multiple item generators? If I have two different parts of my game generating items, and the items are all the same except for a number after the name, how will I generate multiples of the same item without looping from zero? (two items named item1 would create some problems...) Do you just have some kind of master variable that you use in every item loop? And finally, if you use a loop to generate multiple items and each item name has a number after it, won't the item names become extremely long?

Thanks a lot, any help is appreciated.  I apologize for writing so much, I just had to ask these burning questions. Grin Thanks again!
Offline fletchergames

Senior Devvie

« Reply #1 - Posted 2007-11-30 03:24:53 »

This is the sort of thing that you can resolve any way you want.

I would have one master ItemDefinitions class that contains definitions of all items.  These definitions should be accessible with a key, which would probably be the name of the item (probably using a HashMap or a Trie to store the items).  Alternatively, you could generate numerical ids when the game loads (probably using an array to store the items).  When using numerical ids, you would want to have a method for converting names to ids at any time because you would probably want to use the names in scripts and so forth.

Then I would have an Inventory class.  Every character that can have items will have an instance of it.  It would contain some kind of list of what items the character has along with how many of each item is owned.  If you're using the names of items as keys, it would probably be a HashMap or Trie with the names as keys and the quantities as values.  If you're using numerical ids, you might prefer to store the ids and quantities in some kind of IntPair class and then store them all in a sorted array or tree (sorted by the item ids).  You wouldn't want to allocate an array for all items for each character because there would be alot of zeros in the array.

You want to be able to find items by name or id number in both the ItemDefinitions class and Inventory class.  You will often have checks like inventory.hasItem(POTION_ID).

You can refer to multiple copies of the same item as "name X quantity" (e.g. Potion X 2).  You only have to worry about long item names if you allow incredibly large quantities of each item or if you actually have long item names.
Offline DecafJava

Senior Newbie

« Reply #2 - Posted 2007-11-30 03:57:29 »

So for my ItemDefinitions class, how would I define items, and how do I create the items if I don't have a class to instantiate? So if I wanted to store my items in a hash map, would I give them a name and a number? But how would I create multiple items with the same name?

Okay, let me rephrase all of my questions into one: How do MMORPGs define, create, and store items?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline fletchergames

Senior Devvie

« Reply #3 - Posted 2007-11-30 19:15:44 »

Okay, let me rephrase all of my questions into one: How do MMORPGs define, create, and store items?
However they want.  But it would probably be in some kind of a database.  In a single player RPG, databases are overkill.  In a MMORPG, some kind of database is probably essential, though the item definitions could probably be included in regular files if necessary.

For the Inventory class, you have either a name or id number as the key in the HashMap.  And then the quantity is the value.  That's how you store multiple items.  Equipped items might be specially stored in the Inventory class or possible stored by the class for the character itself.

The ItemDefinitions class has a different HashMap that maps the item name or id number to the item's definition.  There would generally be another class called ItemDefinition that actually defines one item definition.  I had envisioned it as an internal class only used by the ItemDefinitions class, but it might be better to make a public class to avoid checking the HashMap twice when you have to get the item's definition twice.

I was a little vague about that in the previous post.

So, basically, the ItemDefinitions class is like a database storing data about the objects.  The Inventory class stores the objects a particular character has.  It's like having 2 tables in the same database that are both indexed by the same key.  They're completely separate because how many of an item you have and what an item does are two independent concepts.

The ItemDefinition class itself would include the item name (if it wasn't the key for HashMap) and generally what statistics the item modifies when equipped.  It could also include the value of the item when sold and/or special effects the item has.

You don't have to do this way.  It's just the way that comes to mind.  This isn't the sort of thing where the design has to be perfect.
Offline purpleguitar

Junior Devvie

« Reply #4 - Posted 2007-12-01 13:46:30 »

Just my $0.02:

Why bother separating the "name" or "id" from the item itself?  We're in an OO environment.  Let the Item be the Item, and it can be a key into a map whose value is the number of such items carried.  Better yet, Item can be an interface, so you can add any new kinds of items later.  This works as long as all of your Items provide correct implementations of hashcode, which all of your Java classes should anyway. 
Offline fletchergames

Senior Devvie

« Reply #5 - Posted 2007-12-01 17:21:24 »

That can work.

They way I'm thinking about it is that the item will be referenced by name in a script, so you'll need some way to get the item from the name.  So the Inventory class should probably have a Hashmap with the Items as keys and the quantities as values.  (If you want to use a Trie for whatever reason, you would still have to use the item names as keys.)

The ItemDefinitions class would just map the item names to the Items so that you can determine what items your scripts (if any) are talking about.
Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

Archive (161 views)
2017-02-27 19:41:49

numerical (505 views)
2017-02-21 07:32:16

numerical (507 views)
2017-02-21 07:31:46

theagentd (620 views)
2017-02-18 13:42:33

theagentd (615 views)
2017-02-18 13:35:16

h.pernpeintner (1787 views)
2017-01-24 22:39:11

h.pernpeintner (1728 views)
2017-01-24 22:38:32

Galdo (2299 views)
2017-01-12 13:44:09

Archive (2190 views)
2017-01-02 05:31:41

0AndrewShepherd0 (2724 views)
2016-12-16 03:58:39
List of Learning Resources
by elect
2016-09-09 09:47:55

List of Learning Resources
by elect
2016-09-08 09:47:20

List of Learning Resources
by elect
2016-09-08 09:46:51

List of Learning Resources
by elect
2016-09-08 09:46:27

List of Learning Resources
by elect
2016-09-08 09:45:41

List of Learning Resources
by elect
2016-09-08 08:39:20

List of Learning Resources
by elect
2016-09-08 08:38:19

Rendering resources
by Roquen
2016-08-08 05:55:21 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!