Game Development Community

Save Settings

by Phillip O'Shea · in iTorque 2D · 03/17/2010 (3:54 am) · 9 replies

I was fumbling around with saving/loading settings and I found a few threads which hinted at doing it manually. I didn't like that approach so I opted for something a little more general:
function LoadSettings()
{
    // Load Game Defaults.
    exec( "./gameDefaults.cs" );

    // Have User Data?
    %userDataFile = expandFilename( "./gameData.dat" );
    if ( !isFile( %userDataFile ) )
    {
        // Nope, quit here.
        return;
    }

    // Read the File.
    %loadFile = new FileObject();
    %loadFile.openForRead( %userDataFile );
    
    while ( !%loadFile.isEOF() )
    {
        // Eval Contents.
        eval( %loadFile.readLine() );
    }

    // Close & Delete Reference.
    %loadFile.close();
    %loadFile.delete();
}

function SaveSettings()
{
    // Export $Game::* variables.
    export( "$Game::*", expandFilename( "./gameData.dat" ), false );
}
This method saves out all of the variables stored as $Game::*. It exports them in a format like this:
$Game::MyStringVariable = "Hello World!";
$Game::MyIntVariable = 20;
When the file is read, each line is evaluated using the "eval" method which basically just assigns a value to the given variable.

It is a pretty simple method, but I thought I'd share it for those who didn't know how.

About the author

Head of Violent Tulip, a small independent software development company working in Wollongong, Australia. Go to http://www.violent-tulip.com/ to see our latest offerings.


#1
03/17/2010 (8:42 am)
Phillip

I need to save preferences in my game and haven't gotten around to doing it. You saved me some time and thanks for sharing.
#2
03/17/2010 (12:45 pm)
The export() function is one I've never really noticed - thanks :)

But does this actually save in the writable space on the device?
#3
03/17/2010 (12:47 pm)
Ronny, yeah I've tried it out. Export explicitly uses the prefs path, and so does openForRead apparently.
#4
04/04/2011 (1:49 am)
This is just awesome!!! 10 minutes to make it run. All my prefs saved, high-scores, etc...

Thank u very much.
#5
04/04/2011 (4:56 am)
Phillip, thanks very much for sharing this!
#6
04/04/2011 (12:37 pm)
Could you use:
export( "$*", expandFilename( "./gameData.dat" ), false );

to save EVERY global script variable?
#7
04/26/2011 (1:02 am)
function LoadSettings(){
	// Load Game Defaults.  
	exec("./gameDefaults.cs");

	// Have User Data?
	%userDataFile = expandFilename("./gameData.dat");
	if(!isFile(%userDataFile)){  
		// Nope, quit here.  
		return;  
	}

	// Read the File.  
	%loadFile = new FileObject();
	%loadFile.openForRead(%userDataFile);

	while(!%loadFile.isEOF()){
		// Eval Contents.
		eval(%loadFile.readLine());
	}

	// Close & Delete Reference.
	%loadFile.close();
	%loadFile.delete();
}
  
function SaveSettings(){
	// Export $* variables.  
	export("$*", expandFilename("./gameData.dat"), false); 
}

I have to use it in this way (pseudo-code)?
function exitGame(){
        quit();
        SaveSettings();
}

function loadGameButton(){
        LoadSettings();
        if($level == 10){
                changeLevel();
        }
}
#8
04/06/2012 (12:25 am)
What is the:
if($level == 10){
changeLevel();
}
???
#9
04/09/2012 (12:19 pm)
Apparantly the Code in this thread doesn't save to NSCaches and wouldn't result in an approvable app.