Since threads about these have recently been popping up, I thought I'd make a neat little tutorial about them.
Contents-Introduction
-What is File I/O?
--Uses of File I/O
-Basic Use
-Writing files
-Reading files
-Miscellaneous Information
IntroductionIn this tutorial, I'll be teaching you how to read and write files in Torquescript using fileObjects.
What is File I/O?File I/O stands for "File Input/Output". This means you can read and write files using this system.
Uses of File I/OFile I/O can be used to read and write files. This is done through objects called "fileObjects". Some examples of when this is needed or could be used to make things easier:
- Saving data and reading it after the server closes and opens back up.
- Dumping data from objects.
- Saving a list of words or values.
Basic UseTo start off, we need to know how to use them.
%file = new fileObject();
This creates a fileObject and the variable %file represents it in script.
To close the file after reading or writing, do this:
%file.close();
%file.delete();
This closes the file and deletes the fileObject.
Writing FilesTo write a file, you use the functions
openForWrite, which overwrites anything already in the file, and
openForAppend, which simply adds onto a file.
Files should be specified like so:
<parent folder(s) seperated by forward slashes>/<filename>.<fileextension>
An example of openForWrite:
%file = new fileObject();
%file.openForWrite("blah/blah.blah");
%file.writeLine("blah");
%file.close();
%file.delete();
blah/blah.blah:blah
You can do this as many times as you want and the file blah.blah will not change unless you change
%file.writeLine("blah");.
An example of openForAppend:
%file = new fileObject();
%file.openForAppend("blah/blah.blah");
%file.writeLine("blah");
%file.close();
%file.delete();
After the last one, the file should now read:
blah
blah
If you do this more, it will add more lines containing whatever is in openForAppend.
Reading filesFor reading files, there are a few functions you will most likely use.
openForRead - opens a file to read
isEOF - checks if we're at the end of the file or not.
These work together to do something like this:
%file = new fileObject();
%file.openForRead("blah/blah.blah");
while(!%file.isEOF())
{
echo("Lines read:" SPC %lineCt);
%lineCt++;
echo(%file.readLine());
}
echo(%lineCt SPC "lines read total.");
%file.close();
%file.delete();
Basically, what this does is open our blah.blah file for read and as long as we haven't reached the end of the file, it echos how many lines we have read so far and echos the line. Once we reach the end of the file, it should tell us how many lines were read in all.
It would do something like this:
Lines read: 0
blah
Lines Read: 1
blah
1 lines read total.
Miscellaneous InformationThis is a good reference for more information on File I/O.
http://docs.garagegames.com/tge/official/content/documentation/Reference/Console%20Functions/TorqueScript_Console_Functions_7.htmlThe add-on execution system works something like this:
function loadClientAddons()
{
for(%i = findFirstFile("Add-Ons/*/client.cs"); %i !$= ""; %i = findNextFile("Add-Ons/*/client.cs"))
{
echo("Loading: "@strReplace(strReplace(%i, "/client.cs", ""), "Add-Ons/",""));
exec(%i);
}
}
function loadServerAddons()
{
for(%i = findFirstFile("Add-Ons/*/server.cs");%i!$="";%i=findNextFile("Add-Ons/*/server.cs"))
{
echo("Loading: "@strReplace(strReplace(%i, "/server.cs",""), "Add-Ons/", ""));
exec(%i);
}
}
Using findFirstFile and findNextFile, it executes every single addon with the proper packaging.
The function save(object, file); can save data from an object to a file.
Hope this helps, if any questions or comments, say so.