Game Development Community

FileObject error, any help welcome.

by Matthew Medina · in Torque Game Builder · 01/05/2007 (8:11 pm) · 5 replies

Hi, I'm using the FileObject Tutorial (http://tdn.garagegames.com/wiki/TorqueScript_FileObject). I created a file called test.cs and have it set to execute whenever my level loads. The problem is when the level loads, the console displays a parse error on %file.close();. I'm trying to get it to write "This is the test file" into a text file, for testing purposes. Here is my code that is in test.cs:

(code)

%file = new FileObject();

%file.OpenForWrite(gamescripts/test.txt);
%file.writeline(This is the test file.);
%file.close();
%file.delete();
return;
}

while( !%file.isEOF() )
{
%line = %file.readline();
echo(%line);
}

(/code)

Is there anyway I can get this to work? Thanks

#1
01/06/2007 (1:33 am)
Just offhand your strings are not quoted, you should specify a ~ to specify your game directory at the beginning of your path and you're trying to read from a file you haven't opened for reading. Try something more like this instead, calling "testFiles();" when your level loads:

function testFiles()
{
   testFileWrite();
   testFileRead();
}

function testWriteFile ()
{
   %file = new FileObject();
   %file.OpenForWrite("~/gamescripts/test.txt");
   %file.writeline("This is the test file.");
   %file.close();
   %file.delete();
}

function testReadFile()
{
   %file = new FileObject();
   %file.OpenForRead ("~/gamescripts/test.txt");
   
   while( !%file.isEOF() )
   {
      %line = %file.readline();
      echo(%line);
   }

   %file.close();
   %file.delete();
}
#2
01/06/2007 (10:52 am)
Also use square brackets [ ] for your tags in posts instead of parens ( )
#3
01/06/2007 (2:59 pm)
Okay, I got it to work, thanks.

Now I'm wondering how I can get it to continue a game with this system. For every level, it is supposed to write the path to the level in the "test.sav" file it creates. It keeps the path written in there, and the next time you play and click "Continue" it reads the file and opens the last level written down. This works when I test it by just writing a sentence in the file instead of the path. Here's is what I'm doing :

{%file = new FileObject();
%file.OpenForWrite("./test.sav");
%file.writeline("This is a test");
%line = %file.readline();

%file.close();

The above script works exactly how it should, and I have it write a different sentence every level, so I think I'm doing something right.

{%file = new FileObject();
%file.OpenForWrite("./test.sav");
%file.writeline("sceneWindow2D.schedule(1000, loadlevel, ~/Game/data/levels/Level4.t2d);");
%line = %file.readline();

%file.close();

The above script does not work, which is easy to understand because the way it's written without quotation marks. When I put the quotation marks, it has errors, I'm guessing because it thinks that's where I want it to stop writing:

sceneWindow2D.schedule(1000, loadlevel, ~/Game/data/levels/Level4.t2d);

But if I wrote it like below:

sceneWindow2D.schedule(1000, loadlevel, "Game/data/levels/Levelt4.t2d");

With quotation marks, the continue system does work. I know this because I wrote it in the "test.sav" file, and when I pressed "Continue" in the game, it loaded the level.

So what I really need to know, is there any way I can have it write the code just like above, and have it write it into the "test.sav"? That is the only way I can think of to get my continue system to work.

Thank you
#4
01/07/2007 (11:18 am)
You don't need to write out the script command that you want to execute for the level. Try just writing out the number of the level, like "4". Then read back in the number of the level in your level load and construct the command you want.

// assume you've read the level number into %levelNumber
   sceneWindow2D.schedule (1000, loadlevel, "Game/data/levels/Levelt " @ %levelNumber @ ".t2d");

If you really did want to do this and have the inner quotes written, you can escape the quotation marks when you're writing it like this:

%file.writeline("sceneWindow2D.schedule(1000, loadlevel, \"~/Game/data/levels/Level4.t2d\");");
#5
01/07/2007 (12:30 pm)
Thank you, that got it working the way it should.