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.