Game Development Community

Saving New Script Files

by Jerane Alleyne · in Torque Game Engine · 10/10/2002 (2:15 pm) · 4 replies

I'm trying to create an inventory system that will allow you to save new profiles for a player. My plan was to use $pref::player::* variables to save the specific info, and save that to a new script file to keep the info. While there is no problem in saving the prefs, I'm curious about what command I can use to save a new script file. I was thinking I could use the export() command, but was not sure if the argument for the filename would allow for anything other than an actual file name (pref.cs as opposed to something like %filename). Is there another command I should use in this situation? I haven't seen one that saves custom scripts. I'm going to look at the level editor to see how it saves .mis files and see if I can use a similar method.

Thanks for any help :)

#1
10/14/2002 (6:40 pm)
After looking over the EditGui.cs script in the Torque terrain editor, looking for how it saves unique scripts (.mis files). I came across a command getSaveFilename(). This seems to, in its arguments, save under a certain extension, states the function that gets the name, and apparently the variable it relates to(?).

I was wondering if anyone can confirm that this command saves the script under the filename you specify, and all of the other arguments this command uses. I tried to use console doc and its not listed, neither is it in the alpha console commands reference.

Appreciate it :)
#2
10/14/2002 (9:22 pm)
Not sure exactly what you mean. TGE script has built in plain text file support for both reading and writing. You can use these functions to generate any data file format you want.

Below are two code samples I wrote to demonstrate the file functions. The first is simply two functions one to read a file and echo out the contents and write specific information into a file.

The second is two functions that allow you to open a file (read or write), specify a filehandle ID and reference it from anywhere in your code.

function readfile(%filename)
{
   %file = new FileObject();
   if(%file.openForRead(%filename))
   {
      while(!%file.isEOF())
      {
        %input = %file.readLine();
        echo(%input);
      }
   }
   %file.close();
   %file.delete();
}

function write(%filename)
{
   %file = new FileObject();
   %file.openforWrite(%filename);
   %file.writeLine( "This is an output file);
   %file.writeLine( "I can contain any plain text that the user wishes to output." );
   %file.close();
   %file.delete();
}


// Usage:
//     openFile("name","fileHandle","R");
//  Open a file named "name" assign a fileHandle ID of "fileHandle", open as Readonly.
//  Reference in another function:
//     fileHandle.readLine();
//
//     openfile("name","fileHandle","W");
//  Open a file named "name" assign a fileHandle ID of "fileHandle", open as Writeonly.
//  Reference in another function:
//     fileHandle.writeLine();
//
//     closeFile("fileHandle");
//  Closes the file associated with the fileHandle ID of "fileHandle"

function openFile(%filename,%IDname,%rw)
{
   %file = new FileObject(%IDname);
   if (%rw $= "R")
   {
   	if(%file.openForRead(%filename))
   	{
   		return 1;
   	}
   	else
   	{
   		return 0;
   	}
   }
   else if (%rw $= "W")
   {
   	%file.openForWrite(%filename);
   	return 1;	
	}
	else
	{
		return 0;
	}
}

function closeFile(%IDname)
{
   %IDname.close();
   %IDname.delete();
}
#3
10/15/2002 (12:25 am)
And to answer your other question.

getSaveFilename() is a scripted function / GUI that allows you to pick a filename for the file you wish to save using file specs.

To understand the function you need to know the arguements. Below is a prototype of the function.

getSaveFilename(%filespec, %functionCallback, %currentFilename);

%filespec - The wildcard filepec for the dir listing that the save dialog will show. ('~/directory/*.ext' for example)
%functionCallback - The function that the getSaveFilename will call when a filename is selected.
%currentFilename - The "default" save filename to pass to the function.

So to expand on the previous file function samples.

function SaveFile()
{
     getSaveFilename("*.txt",saveTextFile,"default.txt");
}

function saveTextFile(%fileName)
{
   %file = new FileObject();
   %file.openforWrite(%filename);
   %file.writeLine( "This is an output file);
   %file.writeLine( "I can contain any plain text that the user wishes to output." );
   %file.close();
   %file.delete();
}


There is also a simliar function for loading files named getLoadFilename. It only uses the first two arguements though.

function LoadFile()
{
     getLoadFilename("*.txt",loadTextFile);
}

function loadTextFile(%fileName)
{
   %file = new FileObject();
   if(%file.openForRead(%filename))
   {
      while(!%file.isEOF())
      {
        %input = %file.readLine();
        echo(%input);
      }
   }
   %file.close();
   %file.delete();

}
#4
10/15/2002 (4:32 am)
Hey Harold,

I think this is exactly what I was looking for, or as close as possible. I was going to ask about getLoaFfile as well, figuring it was just like that :)

Thanks a lot man!