Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm quite new to cocos2d and games programming, and am currently I am writing a game that is currently in Prototype stage. Everything is going okay, but I've realized a potentially big problem and I am not sure how to solve it.

I am using a singelton to store a bunch of arrays for everything, a global list of planets, a global list of troops, a global list of products, etc. And only now I'm realizing that all of this will be in memory and this is the wrong way to do it.

I am not storing files or anything on the disk just yet, with exception to a save/load state, which is a capture of everything.

My game makes use of a map which allows you to select a planet, then it will give you a breakdown of that planets troops and resources,

Lets use this scenario:

My game has 20 planets. On which you can have 20 troops.

Straight away that's an array of 400!

This does not add the NPC, which is another 10.

So, 20x10 = 200

So, now we have 600 all in arrays inside a Singelton.

This is obviously very bad, and very wrong. Especially as the game scales in the amount of data.

But I need to expose pretty much everything, especially on the map page, and I am not sure how else to do it.

I've been told that I can use a controller for the map page which has the information I need for each planet, and other controllers for other items I require global display for.

I've also thought about storing each planet's data in a save file, using initWithCoder however there could be a boatload of files on the user's device?

I really don't want to use a database, mainly because I would need to translate NSObjects and non-NSObjects like CGRects and CGPoints and Colors into/from SQL.

I am open to other ideas on how to store and read game data to prevent using a singelton to store everything, everywhere.

Thanks for your time.

share|improve this question

3 Answers 3

up vote 3 down vote accepted

You seem to be worried about how to store your data when you should be concerned with the design of both your data, and how you expose it. Storing data in files or using a database is not a solution to design.

Instead of a singleton, pass the data by reference into functions. It may make sense to use a hierarchy of data types, such as a planet type that contains the troops, and a universe that contains the planets.

share|improve this answer
    
Ah, sounds good. Upvoted. Thanks –  zardon Apr 16 '12 at 14:17
    
I think iPhone games kit uses a similar setup to what you refer to and I will certainly look it up. –  zardon Apr 16 '12 at 14:22
    
I will accept your answer for the moment, and come back to it later on –  zardon Apr 17 '12 at 8:49

According to the description of your game i think you should use core data which is not so hard once to start developing your game with core data to manage all of your data.

For thinks like CGRect or CGpoint you can create some functions in the class of NSManagedObjects that will fetch and store these values at run time so that you dont have to do it every time you need them, something like this

- (void)setRect:(CGRect)rect
{
    self.x = [NSNumber numberWithFloat:rect.origin.x];
    // And so on.....
}

- (CGRect)getRect
{
    CGRect rect;
    rect.origin.x = [self.x floatValue];
    // And so on......
    return rect;
}
share|improve this answer
    
I guess I could do core data, but I always find it a ballache adding to a new project, it always crashes and its not simple/straight forward to add to an existing project. I avoided core data because of this. Although I guess I could start a core data project then add Cocos2d on top of it? –  zardon Apr 16 '12 at 14:20
    
Or you can create a new core data project and just copy the code from the app delegate and the xcoremodel or something into your project, I think thats the easiest way to go about it for you. –  Robin Apr 17 '12 at 1:46
    
I'll give it a shot! I do wish Apple had made Core data more easier to implement though. –  zardon Apr 17 '12 at 8:49

An approach that I used was to create property lists that defined the initial state of each planet, and loaded them into dictionaries as I needed them. Assuming the player could be at one planet at a time, you would only have one dictionary to contend with at any given time, which could be accessed via your singleton, if you wanted to use a singleton, or just have the dictionary as part of your games main controller. If the status of the planet is changed and needs to be stored, then you just save the dictionary back to a plist, outside your bundle of course, and reload it if the player returns to that planet.

I don’t know how much data you are referring to, but this approach could certainly work and is very easy to implement.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.