Item Database

I’ve got an item class (with few children for various item types) which holds all the variables such as name, price, weight etc. For my characters’ inventory I use a List<Item>. Whenever I let’s say find a mushroom inside the forest and pick it up, the mushroom object is destroyed and new Item is added to the list.

My question is - what is the easiest way to create an easy-to-edit database of all the items I have in-game, so I can just call them by name, whenever I need to add new thing to my inventory. I want it all in one place.

Maintain a Dictionary of your prefabs or prototypes.

class Inventory 
{
    private List<Item> items = new List<Item>();
    private Dictionary<string, Item> itemPresets = new Dictionary<string, Item>();

    // Presets (Prototypes) are meant to be predefined instances that are later 
    // copied to actual instances for game use. Set these in your init,
    // like hardcoded, or from a file...
    public void SetPreset(string name, Item preset)
    {
        itemPresets[name] = preset;
    }

    public Item AddNew(string name)
    {
        Item preset = itemPresets[name]; // Get the preset

        Item newItem = new Item(preset); // Create a clone, 
                                         // I opted for a copy constructor...

        items.Add(newItem);              // Register to database

        return newItem;                  // And return it since you probably
                                         // want to use it immediately.
    }
}

Hardcoded

Inventory inv = new Inventory();

Item preset;

preset = new Item();
preset.name = "Apple";
preset.cost = 25;
preset.weight = 1;
inv.SetPreset("Apple", preset);

preset = new Item();
preset.name = "Sword";
preset.cost = 100;
preset.weight = 5;
inv.SetPreset("Sword", preset);

preset = new Item();
preset.name = "Shield";
preset.cost = 75;
preset.weight = 8;
inv.SetPreset("Shield", preset);

From a text file

Inventory inv = new Inventory();
string[] lines = System.File.IO.ReadAllLines("Presets.txt");
for (int i = 0; i < lines.Length;)
{
    Item preset = new Item();
    preset.name = lines[i++];
    preset.cost = int.Parse(lines[i++]);
    preset.weight = int.Parse(lines[i++]);
    inv.SetPreset(preset.name, preset);        
}

Assumed text format (Presets.txt)

Apple
25
1
Sword
100
5
Shield
75
8

Have an array to which you drag a prefab of each item type, and add all the helper functions you need to that class. Use the singleton pattern to access it:

// Items.js
var items : Item[]; // Fill out in Inspector
private var instance : Items;
function Awake() { instance = this; }
function Get(name : String) : Item { ... }

Used like this:

Instantiate(Items.Get("toothbrush"));

Note that if you have quite a few, you’ll want to build a Dictionary.<String,Item> at startup.

I’m not entirely sure what the issue is. You already have a list containing your items so that IS your database. If you need to find something specifically within it you can either look up the MSDN documentation on Lists or you can do something like this…

foreach (Item_Class foundItem in Inventory) {
if (foundItem.ItemName == "Mushroom")
//DoStuff

}