Best way to store Item data

Im making an RPG game which requires there to be lots of different items in the game. Each item has its own individual properties and i was wondering whats the best way to store them all.

Heres my idea at the moment


public class ItemStorage {
	public static Item nullItem = new Item(-1,"",""); //-1
	public static Item weaponTest = new ItemWeapon(0,"WeaponTest","Weapon"); //0
}

However that would require lots of code to put in all the items.

I thought about using a list or an array but that would be hard to reference in other areas of my code as i would need to knoew the index of each item. Is this the best way of doing it, or am i missing something?

-Many Thanks

You could have a database/file containing all the item information and load this in your game?

Not sure how you’d do this though.

I feel that would be a waste of time though as i could merely right out the elements of the database in the code instead of in a file

Well for starters, all items should be under an Item class like you have. You need to categorize different kinds of things. There should be things like weapons, potions, armor, etc. Each of these should have more attributes and characteristics to tell eachother apart, but if you need to go even more in depth, I think you’d have to create a class for each weapon, for example.

First you need to refine your requirements.
For example, what kind of items, are there item classes, what are common properties, who needs to access what, when and how frequently, identify dependencies, how should they be accessible, are they dynamically creatable, modifiable, should they be externally editable, what can you do with items, etc. etc.
Anyway, using global item variables is not a good solution.

I haven’t found that to be the case, personally. Although it takes some effort to set up the code for loading the properties file and injecting the values into your objects I find it to be worth it. Once it’s set up you can do something as simple as:



+Axe Weapon
name = Battle Axe
attack = 140
weight = 100
minStrength = 13

+MinHealth Potion
name = Minor Potion of Healing
healthIncr = 15

I have found it makes adding and changing things very easy.

Are these items essentially lists of properties or do they have behavior (e.g. methods) as well? And if they have methods, will they be the same for all items or will different types of items have different types of methods. That is will you be doing something like:



player.attack(monster,Player.getSword());

or

Sword sword = player.getSword();
sword.attack(monster);

I started to build up some various techniques over here: http://www.java-gaming.org/topics/archtypes-composition-and-code-is-data/26554/view.html