Game Development Community

FileWriting...

by University of Gotland (#0009) · in Technical Issues · 12/22/2007 (7:05 pm) · 6 replies

Hello!
I got some big issues with filewriting..
1st off i was using TGB 1.5.x and tried several approaches to write to a file but none of them worked.

So i upgraded to TGB 1.6. Ok! so it works now!!
So heres what i noticed. If i use writeline i can only write 1 line of data.
If i use appendtofile i will save each line i append.

How do i edit or delete text in this file?
Is there no command for that?

#1
12/22/2007 (7:20 pm)
Just get any clue for me to understand this with filewriting coz im having a really hard time and been looking for answers everwhere.

Anohter wierd thing is that the file im writing to doesnt exicst according to windows.. But still i can write AND read from it in torque =)
#2
12/23/2007 (11:00 am)
I found a sample and edited the code to this:
function writeFile()
{
  %fileObj = new FileObject();
   
   %lvl01 = "Level01";
   %sLvl01 = "100001";
   if(!%fileObj.openForWrite( "game/data/files/scoreboard.txt" ))
   {
      %fileObj.delete();
      return "Failed to open file for write!";
   }
   
   error("Succesfully opened FileObject for write");
   
   %fileObj.writeLine(%lvl01);
   %fileObj.writeLine(%sLvl01);
   %fileObj.close();
   
   %fileObj.delete();
   
   error("Succesfully wrote to and closed FileObject");
}
function readFile() {
   
   %fileObj = new FileObject();
   
   // Test reading from the file we just wrote
   if(!%fileObj.openForRead( "game/data/files/scoreboard.txt" ))
   {
      %fileObj.delete();
      return "Failed to open file for read after succesful write!";
   }
   error("Succesfully re-opened the file for Read");
   error("Dumping the contents of the below :");
   error("Begin File Text ------------------- ");
   
   while( !%fileObj.isEOF() )
   {
      %line = %fileObj.readLine();
      error(%line);
   }
   error("End File Text ------------------- ");

   %fileObj.delete();
}


So it works to write and read as long as the application is running. when i restart the game everyhing will be lost.. Thats not what i want.. Please help! My grade is at state :P

and yes! My purpose is to have a highscore!
#3
12/23/2007 (12:13 pm)
To answer your question about editing and deleting a line, I ran into the same problem so what I did was read the entire text document into an array, do what I wanted with the data and then write it back out to the file. Probably not the most efficient way but if you are working with small text files it will be lightening fast to do.
#4
12/24/2007 (9:29 pm)
Hey, thats a great id
#5
12/31/2007 (1:49 am)
To help u start out with file writing try out the following, its very simple and a good way to start u off
it will save two lines for you ... and open the file to retrieve the two lines and show them in the console


function saveAfile()
{
$newscore = " Player has scored 10000000";


%file = new FileObject();

%file.openforwrite("./save/test.txt ");

%file.writeLine("This is the test file");
%file.writeLine( $newscore);
%file.close();

}

function loadAfile()
{
%file = new FileObject();

%file.openForRead("./save/test.txt ");

$firstlinebuffer = %file.readLine();
$firstscorebuffer = %file.readLine();

%file.close();
%file.delete();

echo($firstlinebuffer );
echo($firstscorebuffer);
}

for your needs , make sure that when the game starts (prolly in the function onwake::mainmenu) that u call loadAfile , which buffers your scores .... enjoy
#6
12/31/2007 (2:48 pm)
Has somebody noticed if something is broken with 1.6 because I can't save my data using save() in 1.6 when I was able to do that in 1.5

thanks