Blockland Forums > Modification Help
File I/O Tutorial
otto-san:
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
Introduction
In 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/O
File 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 Use
To start off, we need to know how to use them.
--- Code: ---%file = new fileObject();
--- End code ---
This creates a fileObject and the variable %file represents it in script.
To close the file after reading or writing, do this:
--- Code: ---%file.close();
%file.delete();
--- End code ---
This closes the file and deletes the fileObject.
Writing Files
To 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:
--- Code: ---%file = new fileObject();
%file.openForWrite("blah/blah.blah");
%file.writeLine("blah");
%file.close();
%file.delete();
--- End code ---
blah/blah.blah:
--- Quote ---blah
--- End quote ---
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:
--- Code: ---%file = new fileObject();
%file.openForAppend("blah/blah.blah");
%file.writeLine("blah");
%file.close();
%file.delete();
--- End code ---
After the last one, the file should now read:
--- Quote ---blah
blah
--- End quote ---
If you do this more, it will add more lines containing whatever is in openForAppend.
Reading files
For 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:
--- Code: ---%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();
--- End code ---
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:
--- Quote ---Lines read: 0
blah
Lines Read: 1
blah
1 lines read total.
--- End quote ---
Miscellaneous Information
This 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.html
The add-on execution system works something like this:
--- Code: ---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);
}
}
--- End code ---
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.
Treynolds416:
Looks like a very thorough tutorial, good job.
When you say %file.openForWrite("blah/blah.blah");, you should probably specify that it will go something like %file.openForWrite("Add-Ons/YourMod/server.cs");
And also you put two hyphens in the third item in the table of contents. :cookieMonster:
otto-san:
--- Quote from: Treynolds416 on April 12, 2011, 05:50:49 PM ---Looks like a very thorough tutorial, good job.
When you say %file.openForWrite("blah/blah.blah");, you should probably specify that it will go something like %file.openForWrite("Add-Ons/YourMod/server.cs");
And also you put two hyphens in the third item in the table of contents. :cookieMonster:
--- End quote ---
Yeah, I should. will do.
the two hyphens signal a subcategory. as does the fact that that one is not in size 15 print.
Treynolds416:
--- Quote from: otto-san on April 12, 2011, 05:52:19 PM ---Yeah, I should. will do.
the two hyphens signal a subcategory. as does the fact that that one is not in size 15 print.
--- End quote ---
K
You could've just indented it, though. :cookieMonster:
Placid:
this actually looks pretty nice. i've never really learned how to do it.
for whatever reason, one time i tried it and it didn't work. however I have a feeling i know what i did:
--- Code: ---%file.openForRead("config/client/database/database.cs");
%file.writeline(%name SPC %id);
--- End code ---
hurr.
anyways, nice tutorial.