As I started working on my second will-go-to-market iPhone game,
one of the most important improvements I made was moving configuration
and level details to XML files from the very beginning. One of the
differences between a great game and a mediocre game is flexibility,
and storing details within XML files gives the gift of manifold
flexibility (best of all, you can design a level or content format
in XML, and then you can have non-programmers design content, and
it's very easy to add content later on).
Fortunately, loading XML files on the iPhone is quite easy, once
you pick up a couple of tricks.
The first step is to create a plist file:
File and then select
Scroll to the bottom of the list of templates, and select
From the templates, select
Name the properly list
settings.plist, and save it in your project.
Add whatever data you're interested in. For this example
let's say that you leave Root as a dictionary, and
give it three keys:
- An array with the key levels.
- A string with the key title.
- A number with the key version.
Now wherever you want to load the code, you just need to
write a few simple lines that look like this:
NSString * fileName = @"settings.plist";
NSString * path = [[NSBundle mainBundle] bundlePath];
NSString * fullPath = [path stringByAppendingPathComponent:fileName];
NSDictionary * root = [[NSDictionary alloc] initWithContentsOfFile:fullPath];
NSArray levels = [root objectForKey:@"levels"];
NSString title = [root objectForKey:@"title"];
int version - [[root objectForKey:@"version"] intValue];
One gotcha to watch out for: even if you place all your XML files
in a directory in your project folder named
xml, that doesn't
mean the file will be in a directory named
xml once you
build you application.