Saving/Loading...
I don't know anything about how to do saving or loading in Cocoa, or do I know much about fread/fwrite... if somebody could give me some code or point me in the right direction it would be much appreciated 
basically I'm trying to save a 64 x 64 array of ints representing the tile map, and the locations of enemies and the player. I am trying to write it so when you load a level the standard OS X load/save panels appear. thanks again

basically I'm trying to save a 64 x 64 array of ints representing the tile map, and the locations of enemies and the player. I am trying to write it so when you load a level the standard OS X load/save panels appear. thanks again
-CarbonX
To show up the Save and Open panels use NSSavePanel and NSOpenPanel respectively. If your array was made as an NSArray you can save it by using the method writeToFile:atomically:, if not then you'll need to use the filemanager I think, or convert your array to an NSArray (that would be the easiest).
Read the documentation at the apple site: Cocoa Docs
Read the documentation at the apple site: Cocoa Docs
"When you dream, there are no rules..."
If you're going to do it the cocoa way, you'll have to get all those ints into an NSArray or NSDictionary. [NSNumber numberWithInt:myInt] will allow you to store an int in an NSNumber which can then be added to an NSArray. Once you have an array you have a number of options of how you store that data. Have a look at the docs for NSArchiever and NSCoder.
I like to use xml plists to store my data... see source code below.
For opening / saving, you just get the filename using NSOpenPanel and NSSavePanel.
David
I like to use xml plists to store my data... see source code below.
For opening / saving, you just get the filename using NSOpenPanel and NSSavePanel.
David
Code:
- (NSArray *) loadArrayFromXMLFile:(NSString *)fileName
{
NSData *plistData;
NSString *error = [[NSString alloc] initWithFormat:@"Error reading xml file: %s",fileName];
NSPropertyListFormat format;
NSArray *plist;
plistData = [NSData dataWithContentsOfFile:
[[NSBundle mainBundle] pathForResource:fileName
ofType:@"xml"]];
plist = [NSPropertyListSerialization propertyListFromData:plistData
mutabilityOption:NSPropertyListImmutable
format:&format
errorDescription:&error];
if(!plist)
{
NSLog(error);
[error release];
return NULL;
}
return plist;
}
- (void) writeArray:(NSArray *)plist toXMLFile:(NSString *)fileName
{
NSData *xmlData;
NSString *error = [[NSString alloc] initWithString:@"error writing to xml file"];
xmlData = [NSPropertyListSerialization dataFromPropertyList:plist
format:NSPropertyListXMLFormat_v1_0
errorDescription:&error];
if(xmlData)
{
NSLog(@"No error creating XML data.");
[xmlData writeToFile:fileName atomically:YES];
}
else
{
NSLog(error);
[error release];
}
}Chopper, iSight Screensavers, DuckDuckDuck: http://majicjungle.com
You should look up the NSArchiver and NSCoder types, those are made to store and restore binary data and freeze-dried objects. Going with a plist is only useful if you want a human-readable file format.
You could also serialize the numbers directly into an NSData and store it from there.
Did you ever wonder why we had to run for shelter when the promise of a brave new world unfurled beneath the clear blue sky?
you should just make a document-based cocoa application. truly, it makes your life a whole lot easier.
Yes, it does
Did you ever wonder why we had to run for shelter when the promise of a brave new world unfurled beneath the clear blue sky?
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| Saving data to file | _Event_Horizon | 2 | 2,625 |
Oct 28, 2005 06:53 AM Last Post: _Event_Horizon |
|
| Saving High Scores | Nick | 7 | 3,695 |
Jun 20, 2005 12:08 AM Last Post: Malarkey |
|

