- Home /
Static Dictionary not initializing properly
For a mining game I have a public static dictionary that I was hoping to use to sort for calling by id, but I can't get it to call values from the dictionary. If I call a debug.log(dictionary.count.tostring()) it shows me that it is in fact 16 long, but all the values are null. I don't understand, and am currently calling item specifically, but this will make it very hard for me to call random items from a pool down the road.
public static class Resources {
public static Dictionary<int, Item> ResourceDictionary = new Dictionary<int, Item> {
{0, null},
{1, Resources.RawOre},
{2, Resources.Gem},
//etc
};
}
PlayerScript.AddItem(Resources.RawOre, 10);
//^^^^this works fine, calling directly
for (int i = 0; i < newGems; i++){
int gemType = Random.Range (10,15);
PlayerScript.AddItem(Resources.ResourceDictionary[gemType], 1);
//calling through the dict returns null reference exception
}
Answer by Habitablaba · Oct 30, 2014 at 01:20 AM
Oh man, are Resources.Gem and the others stored as objects in the Resources class?
If so, you may be going about this the wrong way (or a less than ideal way, because who is to say what is right or wrong).
You want to make sure you are instantiating those objects as well, in order to add them to the dictionary (otherwise you'll end up with 16 valid keys with 'null' as the value since that is the value of those objects before they are instantiated).
Really all you want to store in that dictionary is a type, as far as I can tell. Or some way to know which type of resource to add to another list later. Instead of keeping them as objects, perhaps you could create an enum
public enum Item
{
None = 0,
Rock,
Gem,
Etc...
}
You'd have to drop the 'null' for Item.None, but for the most part your code could stay.
yes it has 16, 10-15 were the predeter$$anonymous$$ed gem types i made, ruby, sapphire etc. It showed the count of the Dictionary as properly 16, as if it were initizliaed, yet all key wuld only return null. I setup a checker to post if for i<16 dict[i] == null, debug.log(i.tostring() + " is null"); and it showed them all empty.
I don't understand how it would get a proper count if all values were null.
Here's a question: What is Item? and what are Resources.RawOre and Resources.Gem.
I had thought they were an enum, but I'm starting to question that.
ya i was pretty general with everything sorry.
Item is a static class with int _id, string _name, string _description.
Resources are a static hard coded Item.
I was thinking, rather than setting up a Dictionary to try and access items by a preset number I could just hard code a public static Item FindItemByID (int id) { if (id == 1) return Resources.RawOre; else if (id == 2) return Resources.RawGem;
etc....
but that doesn't sound very performance smart... an indexed object sounded much better but I couldn't get lists or dictionaries to work... frankly an array would work fine as I could just set the length as a variable and add to it whenever I add new items.... ?
still don't understand why I can't use a Dict like that though.
for example, I understand I could make a script separate and attach it to my camera and have it populate the static dictionary when the camera script loads, but is there no way to have the values just inherently static?
I was able to successfully build and int based return system, which is pretty much the same things, I just can't use the dictionary functions.