I had so much trouble with this, it's not even funny

Hi, and it’s not your fault, it’s mine!

I didn’t have any clue about what I was doing and how I was supposed to look stuff up in the dictionary. I tried doing nested foreach-loops inside nested foreach-loops, within nested foreach-loops and it just goes on and on, and I didn’t manage to get anything useful out of our lookupTable variable.

I’m having such a hard time wrapping my head around Dictionarys, I had the same problem with arrays when I was starting out, I just couldn’t do a simple for-loop to look up stuff within an array… Now unfortunately that stupidity has carried over to my “knowledge” of dictionarys :frowning:

I should’ve known how to search for data in the dictionary, but I tried all kinds of "TryGetValue"s and all sorts of hacks… Alas, I didn’t, I just hope some day I’ll be able to understand things… Thanks for the solution and sorry for failing, I tried for about 15 minutes until giving up and looking for your solution. It was so simple I feel like an idiot! Awesome video though!

1 Like

I disagree on the fault. Let’s be clear, we’re not just throwing a Dictionary at you (think of a Dictionary as an array with unique keys instead of an integer for an index). We threw a NESTED pair of Dictionaries at you. But wait, there’s more, we threw an array into the nested dictionaries!!! I’ve been coding since the Celebrity in the White House said “Mr. Gorbachov, Tear Down This Wall”, and nested Dictionaries even gave me pause.

So let’s start by getting you up to speed on Dictionaries. Since you’ve mentioned that you had trouble with arrays (emphasis mine), it would appear that you have since gotten over this roadblock and understand the concepts behind an array… So let’s start there… with a reminder of how Arrays work.

An array is, at it’s most fundamental level is a pile of values, all easily indexed by their position in the array… Like a bookshelf that has room for exactly one book in each shelf.

string[] books;

Now if I want to get the name of the book on the first shelf, I’ll use books[0] (because indexes start at 0 in arrays).
This makes accessing individual array elements fairly easy, because our minds are used to orderly things organized in order, either by numbers or alphabet.

Dictionaries are actually very much like Arrays. At the core, a Dictionary is a pile of values. Only this time, instead of organizing by the shelve’s number, we’re giving the box another identifier… I’ll actually use a different analogy for this one… licence plates. Within any given state, a licence plate is something that is guaranteed to be unique. If my license plate is “UNITY”, yours cannot also be “UNITY”, it’s rejected by the system because there is already a license plate called Unity…

Dictionary<Plate, Car> registeredVehicles;

So if you want to know what car is registered with the license plate “UNITY”, you would say

Car car = registeredVehicles[UNITY];

and just like that, you have my car.
But… wait… look at that car over there… it has a plate from a different state, but it’s plate reads “UNITY”… This is possible because that state has it’s own Dictionary<Plate, Car>
So if I want to find out the car, I also need to know what State the car is from… Mine’s in California, that other guy’s state is Arizona…
It turns out, we can do that with another dictionary, this one being a dictionary of our <string, car>

Dictionary<State, Dictionary<Plate, Car>> registeredVehiclesByState;

So the key for the Dictionary registeredVehicles is State… if I want to get to the registrations for California, I can access this with

Dictionary<Plate, Car> registrations = registeredVehiclesbyState[California];

I can now look up the car by saying

Car car = registeredVehiclesByState[California][UNITY];

So that’s a Dictionary within a Dictionary. It’s our Turkey stuffed with a Duck… but we still need one more element to get our Turducken, that annoying array within the Dictionary within the Dictionary.
That’s right, we’re making a Turducken! This is next level stuff, and not for the faint of heart, but we’re going to get you through it, or my name isn’t John Jacob Jingleheimer Smith… Oh, wait, it’s not, but we’re still going to get you through it…

At this point, we’re going to set aside the cars and states and license plates, and get down to the brass tacks… because what we’re really dealing with is an array of stat values, a stat, and a class.

What do we want to know? What the given value of a stat is at a particular level, for a particular class… so let’s start with the levels… We know we need a value for each level, and a level is an integer… so that says “Array”…

float[] levels;

So we know how to access a value from the levels…

float value = levels[level];

Now we want to know this information keyed off of a specific stat… you’ll have a different set of levels for Stat.Health for example, than you would for Stat.Attack. A Stat isn’t really an Int, so a Dictionary is a better way to keep the levels organized…

Dictionary<Stat, float[]> stats;

So made a dictionary where the key is the Stat, and the contents are an array of floats, whose key is an integer (level).
If I want to find out what the value of a particular stat is for a particular level, I would use:

float value = stats[stat][level];  

The first index reaches into the Dictionary<Stat part of the statement, the level reaches into the float array that is what is stored within the stat.
Now for the coup de gras, the Turkey we’re going to stuff our Duck which we’ve already stuffed with a chicken, we’re about to make our Turducken!

So we’re going to take that Dictionary<Stat, float[]> and put it in another Dictionary, much like we took the Dictionary<Plate, Car> and put it inside the Dictionary<State

Dictionary<CharacterClass, Dictionary<Stat, float[]> characterClasses;

The first part of the Dictionary, the Key is CharacterClass, the value is a Dicitionary<Stat, float[]>.

Now, we want to find out what a particular stat value is at a particular level on a particular class

float value = characterClasses[characterClass][stat][level];

Whew! We did it, we stuffed that array of levels into a stat, then jammed that stat right into a Characterclass.
I hope this helped make things a bit clearer, even if not perfectly clear. At the very least, I hope you chuckled once or twice through it all.

1 Like

Wow, this is a monster of a post! Thank you so much for explaining this to me, I feel like I learned a lot! Your examples were useful and also funny, which is a nice combination! (Although we don’t have thanksgiving here in little ol’ Finland, but I got the point anyways!)

So maybe from now on I can kinda say that I had trouble with Dictionaries before, but not so much anymore, you know? :smiley:

Sorry it took me a while to write an answer, but your post was so awesome that I just didn’t know how to do it justice… Still don’t actually… So I’m just gonna say THANK YOU in all caps, so you can know that I’m shouting at you! :smiley:

I’m glad I could help! I try to infuse humor into explanations when I can. Sometimes, you just can’t, but when you can, be funny! Besudes, you sort of set the tone by saying it wasn’t funny. I had to prove you wrong.
For the record, you don’t need it to be Thanksgiving to enjoy a Turducken… you just need a chicken, a duck, and a turkey, and a lot of patience.