akuritsu
akuritsu

Reputation: 440

Text file to use for data

I've been wondering what the best way to store data would be in my Cocos2d game.

When my game initiates, it selects a random type of enemy. Depending on the level, the amount of enemies changes. What I want to do is have a file that has all the amounts of enemy to spawn on it. I'd like to have this in a separate file if possible, so that it is easy to tweak and change.

EXAMPLE

Say for Level 3, Enemy_1 is chosen. It then goes to the Enemy_1 data file, and then finds the amount of sprites to create.

When searching for a way to do this, i've notcied that there are a couple of files built in to xCdoe that I could use. These include: Strings File, Property List and Rich Text File. My best guess is that I'd have to use a property list, but I am not sure how they work, and how it could be implemented into my game. Preferably could you give me the code for how to do this too?

Thanks in advance

EDIT: I forgot to say that I can't just have one properties file, but I need to have one for each type of enemy. This is because some enemies are harder to kill than others.

Upvotes: 0

Views: 131

Answers (2)

rob mayoff
rob mayoff

Reputation: 385500

You probably can use one property list file.

First, make sure you understand what a property list is. A property list is defined recursively as one of these things:

  • a string
  • a Boolean value
  • an integer
  • a floating-point number
  • a date (and time)
  • a blob of binary data
  • an array of property lists
  • a dictionary with strings for keys and property lists for values

A property list can have arrays and dictionaries nested to any level. That's why you can probably just use one property list. You can have your top-level object be a dictionary with two keys: “Enemies” and “Levels”. The value for the “Enemies” key is a dictionary with one entry for each type of enemy. The value for the “Levels” key is an array with one entry per level. Here's an example:

game property list

You can use Xcode's property list editor to create and modify the property list (like I did for that example). You can load it using +[NSDictionary dictionaryWithContentsOfFile:], or using NSPropertyListSerialization if you need more error checking. The Xcode plist editor isn't that great, but even if you end up needing to write your own level editor, a property list is still a convenient way to store your game data.

Property List Programming Guide

Upvotes: 1

Inder Kumar Rathore
Inder Kumar Rathore

Reputation: 39978

Property list are the best suitable to design level based game.
Property list say Levels.plist can be converted into NSDictionary or NSArray easily depending upon what kind of data it holds.
These are the two methods that is used to convert plist into NSDictionary or NSArray

NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Levels" ofType:@"plist"]];

NSArray *array = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Levels" ofType:@"plist"]];

Upvotes: 2

Related Questions