Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (121)
games submitted by our members
Games in WIP (577)
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 1153 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 Duke





« 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 Duke





« 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 Duke





« 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 Duke





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

 

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

The first screenshot will be displayed as a thumbnail.

theagentd (20 views)
2014-10-25 15:46:29

Longarmx (53 views)
2014-10-17 03:59:02

Norakomi (47 views)
2014-10-16 15:22:06

Norakomi (35 views)
2014-10-16 15:20:20

lcass (39 views)
2014-10-15 16:18:58

TehJavaDev (69 views)
2014-10-14 00:39:48

TehJavaDev (69 views)
2014-10-14 00:35:47

TehJavaDev (61 views)
2014-10-14 00:32:37

BurntPizza (74 views)
2014-10-11 23:24:42

BurntPizza (47 views)
2014-10-11 23:10:45
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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!