0:07
In this lecture, we'll see how
ConfigurationData is implemented in the Feed the Teddies game.
So let's go take a look.
Here's my ConfigurationData CSV file.
You'll notice this is a different format than what we've used so far where we had
the first row was all the value names and the second row was all the values.
I find this vertical organization easier to read here in the CSV file.
It is actually harder to process in the code but I was willing to pay that price.
So you get to see another way that we can use CSV files with ConfigurationData.
In the code itself,
I have added an enumeration for the different ConfigurationDataValueName.
So, there are 35 different enumeration names
here in the ConfigurationDataValueName enumeration.
My ConfigurationData class is also different from what we've seen before.
I have a constant for the ConfigurationData file name
but I'm storing all of my ConfigurationData in a dictionary,
and the key data type is that enumeration,
the ConfigurationDataValueName, and the value will be afloat.
Now, some of my ConfigurationData is more
appropriately an integer but I will store it as a float,
and then within the game,
I will use it as an integer based on the way
I expose things in the properties here from ConfigurationData.
Speaking of that, the total game seconds is one of my ConfigurationData values,
and it's really an int,
even though we could have partial seconds, I don't.
So what I do is I retrieve
the floating point value from that dictionary using TotalGameSeconds as the key,
and then I just cast it to an int.
So, even though I'm storing everything as a float,
I'm converting them to int as
external consumers of the ConfigurationData class consume those values.
Information hiding again because we're hiding the implementation details
inside the property and exposing an appropriate interface to that information.
The Constructor is also significantly
different from what we've seen for handling ConfigurationData.
We still have a stream reader and we still open a text file,
but we have a loop because I don't just have two lines.
Now, I have a value name and a value on each line,
and so I use a while loop here.
When input.ReadLine returns null,
that means I've reached the end of the file.
So, while it's not null,
I have another line to read.
I read this one first, of course,
so at first split that line into tokens and we've seen that before,
except this time, instead of a bunch of values split by commas,
I have the name and then the value.
This next line of code probably looks really crazy to you.
Here's the bottom line.
I'm taking tokens[0], which is the name of this data value,
and I am parsing it into an enumeration.
So in other words, I'm converting the string into an enumeration value,
and then I am typecasting it to be that data type,
and sticking it into value name.
And you can, of course,
go read the documentation for Enum.Parse to understand this better, but really,
I am converting the string that I read for
the value name on this line in the ConfigurationData file,
and I'm converting it to one of the enumeration values.
Then, I will add to my dictionary using
that name and parsing into a float the second string, which was the value.
And then I moved down to the next line in the file.
Now, you might wonder why I'm just doing this instead of just using the string.
I could ignore the enumeration and just use strings instead.
Unfortunately, using strings, if you get anything wrong and I use this enumeration a lot,
when I'm returning those 35 properties,
you could easily get a string wrong and it will compile fine.
It just won't work when you run.
And using this enumeration means if it doesn't compile,
it won't run, and if it does compile, it'll run fine.
So, that's why I have an enumeration as
the key into my dictionary instead of just a string.
If in fact there's an exception,
either I couldn't open the file or somewhere along the way,
something was corrupt in the file so I couldn't parse it properly,
then I SetDefaultValues, and we'll look at that method,
too, and I always close the input file.
Now, this is more robust than the constructor in
the ConfigurationData classes we've looked at before because you'll notice,
I don't have to know in what order the values appear in the file.
As long as they're all valid value names and values,
they can be in any order in that file
and everything will work fine using this dictionary approach.
And that's a little more robust than having to know what order they're in.
Down here is the big long SetDefaultValues method.
First, I cleared the dictionary just to make sure because
if I crashed up here reading the 20th value,
my dictionary has 19 values in it,
and then when I come down here and try to add all 35,
I'll get a duplicate key and I'll get an exception thrown.
So, I clear out the dictionary and I walk through and I add
a default value for each of those 35 ConfigurationData values.
Finally, the ConfigurationUtils class works
exactly like our ConfigurationUtils classes have worked in the past.
I have a field for a ConfigurationData object and each of my properties exposed by
ConfigurationUtils simply wraps the corresponding property inside that object.
The Initialize method does exactly what it's always done.
It just creates a new ConfigurationData object in
the Constructor for the ConfigurationData class goes and does all that file reading,
and setting default values as necessary, and so on.
So, this is more complex than we've seen before for ConfigurationData,
and we are using a different format for our CSV file,
but it actually works really well doing it this way.
To recap, we saw another great use of dictionaries as
we implemented our ConfigurationData work in the Feed the Teddies game.
We also saw a different format for
a CSV file where we had a value and a name in the opposite order,
a name and a value on each line of the file.
And although that was a little more complicated to process,
it led to a much more robust way to implement that file so that we didn't
have to know in what order
those values appeared in the file when we were reading them in.
And finally, we saw that the number of
ConfigurationData values can increase as our game complexity increases.
So, we had 35 here and we actually have 52 in our Battle Paddles game,
and my Balloons Extreme game is going to have many more than that.