Saving context

Member
Posts: 117
Joined: 2010.09
Post: #1
Hi all,

2 questions regarding this; one technical the other just too see what people think.

Firstly with no Cocoa stuff (NSString yuck! ;-) ); how do I save my games context?. By that I mean where do I write a file to; what are it's space limits etc. etc.

Secondly. My game is an rpg and on a mobile platform I think players need to be able to save regularly and easily. Rather than saving a heap of state data I was just going to have fixed save points (like in Castlevania); where a player can save.
What do we generally think about this?. My reason for that question is that potentially your waiting 10 minutes for the bus; just about to get to the save point; bus turns up so end your game.
Next time have to do it all again.
I appreciate 'casual' gamers like simple games (Angry birds anyone?) because they are simple (the games, not the players! ;-) tho...)
So in that context not completing a level (due to the bus being on time!) isn't such an issue.
Just wondered other people's views on when/how to save so the user isn't continually frustrated with redoing tasks. Equally, don't want too much saving and the player completes the game without any replay!

Cheers
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #2
Avoiding Cocoa to get the file path to save may not be possible, and at the very least won't be futureproof. You can keep the Cocoa code well isolated, though; here's what I use (trimmed down a bit to be fully generic):

Code:
#import <Foundation/Foundation.h>

void getPreferencesFilePath(const char * prefsFileName, char * outFilePath) {
    NSAutoreleasePool * pool;
    NSArray * paths;
    const char * libraryDir;
    
    pool = [[NSAutoreleasePool alloc] init];
    paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    libraryDir = [[paths objectAtIndex: 0] UTF8String];
    snprintf(outFilePath, PATH_MAX, "%s/Preferences/%s", libraryDir, prefsFileName);
    [pool release];
}

outFilePath is an array of at least PATH_MAX characters; prefsFileName is the name of the file to save.

As to the second question, I rather like the approach taken by games like Mario & Luigi: Superstar Saga and Cave Story, where there are fixed save points but they're sprinkled VERY liberally through the game world. That way you can control when saves are possible, but there are enough of them that the player is never more than a minute or two from one.

In addition to this, on the iPhone, it would probably be a good idea to save the player's state whenever he quits/backgrounds your game, so that he has the option to resume exactly where he left off. This wouldn't be a full save that could be reloaded multiple times; just a one-time thing so that he doesn't have to think "do I want to lose my progress?" before pressing the home button.
Quote this message in a reply
Member
Posts: 117
Joined: 2010.09
Post: #3
Hi,

Thanks for that.
So can I have as many 'save' files as I wish?
I don't suppose you have the equivalent Cocoa for loading also - as you can tell I stay clear of Cocoa and obj-C :-))

thanks
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #4
Yep, I'm pretty sure there are no specific restrictions on size or number of files, so you should be able to fill up the device's entire storage space with files if you want to.

The function I posted just gives you a file path, so you can use it the same way for both loading and saving. Example:

Code:
#include <limits.h>
#include <stdio.h>

void loadPreferences() {
    char filePath[PATH_MAX];
    FILE * file;
    
    getPreferencesFilePath("MyPreferences.txt", filePath);
    file = fopen(filePath, "rb");
    // call fread, etc. on file
    fclose(file);
}

void savePreferences() {
    char filePath[PATH_MAX];
    FILE * file;
    
    getPreferencesFilePath("MyPreferences.txt", filePath);
    file = fopen(filePath, "wb");
    // call fwrite, etc. on file
    fclose(file);
}
Quote this message in a reply
Member
Posts: 117
Joined: 2010.09
Post: #5
Excellent!, thanks so much!!
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  saving/reading level data using plists aerospaceman 6 4,565 Nov 29, 2010 02:44 PM
Last Post: aerospaceman