Game Development Community

SCripting Help

by CDK · in Torque Game Engine · 01/14/2004 (1:05 am) · 5 replies

Hi all,
I am trying to read and write to a .txt file using script

Now firts this is where i define my file name
if I use
$MissionTransporterFile="fps/client/scripts/Teleport/Teleports.txt";

I can read the file in like

%fileName = findFirstFile($MissionTransporterFile);
if (%fileName $=""){error("No FIle");}
%file = new FileObject();
if ( %file.openForRead( %fileName ) )
{
while ( !%file.isEOF() )
{
%line = %file.readLine();
%line = trim( %line );
}
%file.close();
}
//All workes fine
but when I am trying to save data to the file like so :
echo("New Append" SPC %fileName);
%fileNew = new FileObject();

if (%fileNew.openForAppend(%fileName))
{

echo("WRITTING TO FILE");
%fileNew.writeLine( %ItemToCheck);
//%file.writeLine( "I can contain any plain text that the user wishes to output." );

}
%fileNew.close();

nothing happens but when I change the $MissionTransporterFile="fps/client/scripts/Teleport/Teleports.txt"; to $MissionTransporterFile="~/client/scripts/Teleport/Teleports.txt"
;
the write statement creates a new directory and file under date ~/client/scripts/Teleport/Teleports.txt
I do not whant the new directory I would klike to use
"fps/client/scripts/Teleport/Teleports.txt";

Can anyone help please

#1
01/14/2004 (11:16 am)
I believe that the way the FileObject and ResourceManager are set up you cannot write to a game or mod directory directly. BanList is an example of how to get around this.

The thinking behind this is that games and mods are often stored in ZIP files, CDs, or they are not for modification (imagine if you downloaded a new GUI script and it overwrote some of your core game files!). Thus, all written data is segregated into a seperate directory.
#2
01/14/2004 (9:30 pm)
Thanks for that anilisis Ben but
this is my scenario I need to store location data into the client side text file olmost like saving a game but per mission and then I need to recall that info again.

is their no example/tutorial on how to save?
#3
01/14/2004 (11:19 pm)
You can save data in a mod directory.
#4
01/15/2004 (12:40 am)
// Torque File Operation Examples

function createfile()
{
        Error("--=[ Creating File: starter.fps/test.txt ]=--");
        
        %fw = new FileObject("FW");                     // Create File Object and give it a name for easier reference.
        FW.OpenForWrite("starter.fps/test.txt");        // Open / Create the file test.txt in starter.fps.
        FW.writeline("This is the test file.");         // Put a line of text into the file.
        FW.Close();                                     // Close the file.
}

function appendfile()
{
        Error("--=[ Appending To File: starter.fps/test.txt ]=--");
        
        %fa = new FileObject("FA");                             // Create File Object and give it a name for easier reference.
        FA.OpenForAppend("starter.fps/test.txt");               // Open the file test.txt in starter.fps to append data.
        FA.writeline("This line is appended to the file.");     // Put a line of text into the file.
        FA.Close();                                             // Close the file.        
}

function readfile()
{
        Error("--=[ Reading File: starter.fps/test.txt ]=--");
        
        %fr = new FileObject("FR");                             // Create File Object and give it a name for easier reference.
        FR.OpenForRead("starter.fps/test.txt");                 // Open the file test.txt in starter.fps to read data.
        while ( !FR.isEOF() )                                   // If we aren't at the end of the file, keep looping
        { 
                %line = FR.readline();                          // Read a line from the file
                echo(%line);                                    // Echo the line to the console
        }
        FR.Close();                                             // Close the file.        
}

function testFile()
{
        createfile();
        appendfile();
        readfile();
}
#5
01/15/2004 (2:08 am)
Hi,
Well I do not know I think an emmy award must be awardet to "Labrat"
thnks man this worked
My code stated working after I ran yours once on createing my file therafter
I was able to use my old code can it be becouse i created the file in textpad?

but all is well

thnks

I hope that I will be able to help you in the neer fauture with a problem.