- Home /
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.
This question could use a little more clarification. Easy-to-edit through code? What do you mean with "calling them by name"? Do you mean you'd want to do like inventory.AddNew("$$anonymous$$ushroom");
?
Easy to edit, so I can just add another item somewhere inside the database without the need of editing other items already there.
inventory.AddNew("$$anonymous$$ushroom"); sounds good
Answer by Statement · Aug 10, 2011 at 01:54 PM
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
Ok, after some reading it's clear to me (except for the cloning part - why can't I use the preset?). But still, I don't know how to create the database - either hardcoded or from a file. I'm new to coding.
Oh you can use the preset if it doesn't contain any instance specific properties (if they are read only). Let me update the code with an example of how to populate the presets...
So yeah, you don't need to clone/copy the presets into new instances if they have no individual data (that is, no two apples are different from each other, ever).
I'm curious is it possible to encrypt this data as you write it to the file and still maintain the simple spreadsheet like formatting?
Answer by Waz · Aug 10, 2011 at 01:46 PM
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.
First of all I'm a C# guy, so I don't understand everything you said, but I think we're not talking about the same thing. I don't have prefabs for all the objects yet, also I don't want the items to hold my Item info. GameObject's name may be used to call inventory.AddNew("GO's Name Here") though.
I'd rather like to have a script or a text file, where in one place I can write all the variables name etc down by myself
Answer by AaronG · Aug 10, 2011 at 03:21 PM
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
}
The List is characters current equipment. I needed a library which could hold all items that might be added to the list like a crossbow or a carrot, with all it's stats