Game Development Community

how do i save variables to a file and load them on startup ?[solved]

by Jeff Yaskus · in Torque 3D Beginner · 02/25/2011 (1:53 am) · 3 replies

I'm trying to make a persistent score board ...

How would I (in script) load / assign variables from a text file.

And how would I save/update the text file when a new score is added ?

Here is a list of the variables in question
$ScoreBoard::Count = 5; // 0 to 4
$ScoreBoard::Names[0] = "Visitor1 ";
$ScoreBoard::Names[1] = "Visitor2";
$ScoreBoard::Names[2] = "Visitor3";
$ScoreBoard::Names[3] = "Visitor4";
$ScoreBoard::Names[4] = "Visitor5";

$ScoreBoard::Scores[0] = "0.50";
$ScoreBoard::Scores[1] = "0.55";
$ScoreBoard::Scores[2] = "0.60";
$ScoreBoard::Scores[3] = "0.65";
$ScoreBoard::Scores[4] = "0.70";

$ScoreBoard::Mission[0] = "crash site";
$ScoreBoard::Mission[1] = "desert";
$ScoreBoard::Mission[2] = "zone2";
$ScoreBoard::Mission[3] = "desert";
$ScoreBoard::Mission[4] = "blank room";

#1
02/25/2011 (2:13 am)
This seems to work very well ...

$ScoreBoard::isLoaded = false;
function LoadScores()
{
   %loadFile = new FileObject();
   %dataFile = expandFilename("scripts/gui/scoreboard.txt");
   
   %loadFile.openForAppend(%dataFile);
   %loadFile.close();
   
   %loadFile.openForRead(%dataFile);

   // read in 5 scores
   for (%count=0; %count < $ScoreBoard::Count; %count++)
   {     
      $ScoreBoard::Scores[%count] = %loadFile.readLine();
      $ScoreBoard::Names[%count] = %loadFile.readLine();
      $ScoreBoard::Mission[%count] = %loadFile.readLine();      
   }   
   
   %loadFile.close();
   %loadFile.delete(); // object not file it self
   
   $ScoreBoard::isLoaded = true;
}

function SaveScores()
{
	%saveFile = new FileObject();

	%dataFile = expandFilename("scripts/gui/scoreboard.txt");	
	%saveFile.openForWrite(%dataFile);

   for (%count=0; %count < $ScoreBoard::Count; %count++)
   {     
      %saveFile.writeLine( $ScoreBoard::Scores[%count] );
      %saveFile.writeLine( $ScoreBoard::Names[%count] );
      %saveFile.writeLine( $ScoreBoard::Mission[%count] );      
   }   
	
	%saveFile.close();
	%saveFile.delete();  // delete %saveFile object, not file itself      
}

// Use this to add a new Top Score ... it will drop the last one
// and replace it with this score
function AddScoreBoardList( %score, %name, %mission)
{
   SortScoreBoardList();
   $ScoreBoard::Scores[4] = %score;
   $ScoreBoard::Names[4] = %name;
   $ScoreBoard::Mission[4] = %mission;
   
   echo(" New score added " @ %score SPC %name SPC %mission); 
   
   // save it to file, so its there later // 
   SaveScores(); 
}

//This function updates the StatsListView
function UpdateScoreBoardListView()
{
   if ( $ScoreBoard::isLoaded != true)
      LoadScores();    
...

It loads the scores from file, into memory when the screen is updated ... but only once. Then it saves the list each time a new score is added.

#2
11/15/2012 (6:17 pm)
maybe this will save some code space:

function SaveScores()
{

%dataFile = expandFilename("scripts/gui/scoreboard.txt");

export( "$save::*", %dataFile );
}


function LoadScores()
{

%dataFile = expandFilename("scripts/gui/scoreboard.txt");
exec(%dataFile);


// $ScoreBoard::isLoaded = true;
}
#3
11/16/2012 (12:41 pm)
Additionally, if you make all of your score information fields on a ScriptObject you can call ScriptObject.save(%filename) and then call ScriptObject.load(%filename) to load them.

More than one way to skin a cat....