I am in the process of making a level editor for my game. I have used cocos2d to make the game. I have a level node that is a subclass of CCNode and contains all the level objects in the game. The level then gets added to a CCLayer. I want to save all the children of the level (an NSMutableArray) to a file in the resources folder. I want to load this NSMutableArray, loop through it, and add all the saved level objects to the level. I can’t save these level objects on the local device because the levels in the final game will be based off of this file and other devices need to have it ready along with all the other game resources. Is there any way to do this or do I have to just save strings containing all the properties of the level objects and search through those strings for the information necessary to load the level? If so, is there a way of reading and writing a c structure to a file in combination with an NSString? Here is what I have so far:
NSString * filePath = @"LevelSaves.txt";
NSString * fileRoot = [[NSBundle mainBundle]pathForResource:filePath ofType:@"txt"];
NSString * fileContents = [NSString stringWithContentsOfFile:fileRoot encoding:NSUTF8StringEncoding error:nil];
NSArray * allLinedStrings = [fileContents componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
ObjectFactory * objFactory = ObjectFactory::sharedObjectFactory();
for (NSString * curString in allLinedStrings) {
NSArray * seperatedString = [curString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@";"]];
NSString * objectID = (NSString*)[seperatedString objectAtIndex:0];
NSArray * properties = (NSArray*)[seperatedString objectAtIndex:1]; //not really sure about this part. Haven't tried saving anything there yet.
objFactory->createObject(objectID,properties);
You cannot save data in the application bundle (which I believe is what you meant by “resources” folder). You can save them in the app’s
NSDocumentDirectory, and if your objects conform toNSCoding, you can serialize them usingNSKeyedArchiver.Do the above for debug, and when you’re done editing the level, bundle it for release and use
NSKeyedUnarchiverto deserialize.