It's a dictionary which holds two enumerated types & a List<>
Dictionary<BiomeType, Dictionary<LocationType, List<string>>> myDictionary;
So when I want to use it, I do something like this:
//Add "myString" to the List<string>
myDictionary[BiomeType.Jungle][LocationType.City1].Add("myString"));
When I try to add “myString” to myList, it throws an obvious & foreseeable error: “KeyNotFoundException: The given key was not present in the dictionary.”
Is there any way in C# to automatically have the Dictionary add the Key if it isn’t already there? I have a lot of BiomeTypes & even more LocationTypes. It would be a PITA to have to create each List, then create each locationtype dictionary, and then to add it for every BiomeType. All that work just to initialize this complex dictionary. Is there no easy way to do this?
There’s no way to add have a dictionary automatically add entries for keys. It’s not functionality built into it.
You’d have to manually add them all.
…
Though I have to say, this is the most terrifying use of a Dictionary I’ve ever seen!
…
First and foremost I’d create my own class type that was independent of Dictionary, with a simplified interface. Something like:
public class LookupTable
{
public string this[BiomeType biome, LocationType loc]
{
get { return this.GetEntry(BiomeType biome, LocationType loc); }
}
public string GetList(BiomeType biome, LocationType loc);
public void AddEntry(BiomeType, LocationType loc, string entry);
}
I have to stop here though because you seem to have a ‘List’ inside the nested dictionary… but you use it in your example class as if there isn’t a List:
This passed a list of strings to LoadLevel, is this what is supposed to be done?
I don’t know… I’m going to just go with string for now in my example because we’re jsut getting more complicated otherwise. When you come back we can expand on this more.
Then in your implementation of the class you can store them in whatever container type you want (like Dictionary of Dictionaries… if you oh so desire…). But you can also handle unfound keys and the sort appropriately without throwing exceptions. Leaving a clean interface for calling code to use.
Yea - I know I’m doing it wrong, but for whatever reason what usually comes easy for me is really difficult the last few days. Just structuring my asset bundles in a way that I can load them by location has caused me two full days of headaches. Like a mental barrier is preventing me from reaching obvious conclusions of how I should approach the problem.
I immediately thought of this, and began trying it as a solution. But that mental barrier is making it hard to contemplate how I should get this done.
Ah, sorry. I left out the foreach loop because I assumed people would understand. My apologies.
Alright, let me chew on this for a bit & get back to you. I’ve done stuff like this a hundred times before - but for whatever reason implementing assetbundles & creating all these functions to load/unload them is giving me an unusual amount of trouble. Maybe I’m just rusty.
Also I’ll apologize again about the original question & my trouble with comprehension right now. I copy/pasted it after I typed it up (changing things a bit) for StackOverflow where I just asked it. I was going to format it better here, but I am feeling like I’m on edge.
When I posted on StackOverflow, within seconds, some entitled troll began making insane demands & insulting implications in the comments, and since it appeared so quickly after I posted my question - It really bothered me that the community would be so unfriendly & aggressive to me. Implying I didn’t try any solution at all before asking - demanding I provide him with evidence (like I needed to write up a 10 page term paper to prove I’m not trying to get him to do my homework). The whole incident really surprised me & caught me off guard. Then I just replied to him “Sorry for asking. Wow. Didn’t realize this place was so unfriendly. I’m gone.” and the mod/poweruser deleted that. I reported him, but got lectured by a mod about how I should be more open to my critics. The guy didn’t criticize me & I didn’t argue with him. I just got spooked by his aggressive demand for “evidence” that I tried to solve the problem myself. Like I needed to prove it in the first place after admitting I know of a (bad) way to initialize the dictionary.
I normally don’t let assholes on the internet bother me at all - I’m used to that, lol, but I guess since I expected StackOverflow to be a really friendly, helpful, & understanding place - it gave me system shock when I got an instant reply with the opposite.
Idk why it’s bothering me so much. People are really mean online - and StackExchange isn’t always the nicest place, but man - it bothered me so much I can’t think straight & all I want to do is see that guy exiled from society for being such a toxic jerk. Or maybe it bothers me because I know he will be praised by that community for beating up a new user & scaring a newbie off so they wouldn’t come back to annoy them. Those damn newbies! That makes me upset because it’s the opposite of justice perhaps?
Either way, I appreciate your kind (normal/decent) reply
Thanks @lordofduct for helping me think through this.
I’m actually not storing List, but a custom class called “LocationAssetBundleSets”. I thought changing what I’m storing to be a List would simplify it so people don’t get bogged down in needless details.
Next time I’ll try to be more specific. Then again, next time I won’t have to edit my code to try to appeal to StackOverflow’s troll community.
I think I’ll just do that - it’s really the simplest way to go. Your solution looked great, but I honestly don’t really understand it & this way seems to be the quickest. On top of simplifying the dictionary to no longer be multi-dimensional.
Instead of BiomeType & LocationType, I’ll just sort by LocationType and name them based on biome, like:
Store the LocationAssetBundleSets in a single dictionary, with the LocationType key.
Idk why I tried to do this the harder way. That 2D dictionary was so difficult to even read, let alone use. Getting some clarity in my mind, I can just create a new LocationAssetBundleSet & add it to the dictionary after storing all the assetbundle references. Normal coding. Nothing complicated with a simpler one dimension Dictionary<>.
public Dictionary<LocationType, LocationAssetBundleSet> allLocal_AssetBundleSets = new Dictionary<LocationType, LocationAssetBundleSet>();
public void ReadyAssets()
{
//FARMLAND - UNIVERSAL
LocationAssetBundleSet farmlandUniversalSet = new LocationAssetBundleSet(); //Create new Set
//Tiles
farmlandUniversalSet.assetBundles.Add(LoadAssetBundleManifest("spookyfarmland_tiles_assets"));
farmlandUniversalSet.PrefabBundle_Tiles = LoadAssetBundleManifest("spookyfarmland_tiles_prefabs");
//GameWorldObjects
farmlandUniversalSet.assetBundles.Add(LoadAssetBundleManifest("spookyfarmland_worldobjects_assets"));
farmlandUniversalSet.PrefabBundles_GameWorldObject.Add(LoadAssetBundleManifest("spookyfarmland_worldobjects_prefabs"));
allLocal_AssetBundleSets.Add(LocationType.Farmland_Universal, farmlandUniversalSet); //Add set based on LocationType
}