Custom FileIO Object.
by Jacob S. · in Torque 3D Professional · 05/19/2010 (7:58 am) · 2 replies
//-----------------------------------------------------------------
// Code Repository
//-----------------------------------------------------------------
//%this.fileName.openforwrite(%this.path @ "test.txt");
//%this.fileName.openForAppend(%this.path @ "test.txt");
//%file.delete();
%this.path = "";
%this.fileName = "";
%this.file = new fileobject();
//-----------------------------------------------------------------
// Find Location
//-----------------------------------------------------------------
datablock ScriptObject(FileIO) {
category = "CustomScripts";
};
function FileIO::prewrite() {
%modpath = findfirstfile("~/main.cs");
if(isFile(%modpath)) {
%mymodpath = filePath(%modpath) @ "/";
} else {
%modpath = findFirstFile("~/main.cs.dso");
%mymodpath= filePath(%modpath) @ "/";
}
return %mymodpath;
}
//-----------------------------------------------------------------
// Write Logic
//-----------------------------------------------------------------
function FileIO::writeToFile(%output) {
if ( %this.fileName $= "" ) {
%this.fileName = new fileobject();
%this.path = prewrite();
%this.fileName.openForAppend(%this.path @ "test.txt");
}
if (%this.fileName.openForAppend(%this.path @ "test.txt")) {
%this.fileName.writeLine(%output);
} else {
error("File is not open for writing");
}
}
//-----------------------------------------------------------------
// Read Logic
//-----------------------------------------------------------------
function FileIO::readFromFile() {
%this.path = prewrite();
if ( %this.fileName $= "" )
%this.fileName = new fileobject();
%this.fileName.openForRead(%this.path @ "test.txt");
while(!%this.fileName.isEOF()) {
echo(%this.fileName.readLine());
}
%this.fileName.close();
}
function FileIO::closeFile() {
%this.fileName.close();
}
function FileIO::TestWrite() {
%this.writeToFile(" -I- ");
%this.writeToFile(" -I- ");
%this.writeToFile("-Want-");
%this.writeToFile(" -This- ");
%this.writeToFile(" -To- ");
%this.writeToFile("-Work-");
%this.closeFile();
}and calling this in playGUI.cs via
%this.output = new FileIO();
So I got my fileIO working and I figured I'd attempt to encompas it in it's own object. To make it easier to write to multiple files and have a common interface for multiple projects. Unsuprisingly my stab in the dark did not work. I'm looking at other objects (AIPlayer and like) in torsion and attempting to copy them with little success.
So I'm asking the general "What am I doing wrong?" and "Can this be done without source modifications?".
#2
Edit: Used it a bit and tweaked it for a bit more usability
Working code for anyone interested.
05/20/2010 (5:37 am)
Thanks Konrad works like a charm.Edit: Used it a bit and tweaked it for a bit more usability
//-----------------------------------------------------------------
// Example Declaration
//-----------------------------------------------------------------
//%myNewObject = new ScriptObject() {
// class = "FileIO";
//};
datablock ScriptObject(FileIO) {
category = "CustomScripts";
};
//-----------------------------------------------------------------
// Object Variables
// %this.file
// %this.path
// %this.fileName
//-----------------------------------------------------------------
//-----------------------------------------------------------------
// Specify File Name
//-----------------------------------------------------------------
function FileIO::initializeFile(%this, %fileName){
%this.fileName = %fileName;
}
//-----------------------------------------------------------------
// Find Location
//-----------------------------------------------------------------
function FileIO::getPath() {
%modpath = findfirstfile("~/main.cs");
if(isFile(%modpath)) {
%mymodpath = filePath(%modpath) @ "/";
} else {
%modpath = findFirstFile("~/main.cs.dso");
%mymodpath= filePath(%modpath) @ "/";
}
return %mymodpath;
}
//-----------------------------------------------------------------
// Write Logic
//-----------------------------------------------------------------
function FileIO::writeln(%this, %output) {
if ( %this.file $= "" ) {
%this.path = %this.getPath();
%this.file = new FileObject();
}
%this.file.openForAppend(%this.path @ %this.fileName);
if (%this.file.openForAppend(%this.path @ %this.fileName)) {
%this.file.writeLine(%output);
} else {
error("File is not open for Writing: " @ %this.path @ %this.fileName);
}
%this.file.close();
}
//-----------------------------------------------------------------
// Read Logic
//-----------------------------------------------------------------
function FileIO::readFile(%this) {
%input = new ArrayObject();
if ( %this.file $= "" ) {
%this.path = %this.getPath();
%this.file = new FileObject();
}
%this.file.openForRead( %this.path @ %this.fileName );
if (%this.file.openForRead( %this.path @ %this.fileName )) {
while(!%this.file.isEOF()) {
%input.push_back("random", %this.file.readLine());
}
%this.file.close();
return %input;
} else {
error("File is not open for Reading:" SPC %this.path);
}
}
//-----------------------------------------------------------------
// Misc Logic
//-----------------------------------------------------------------
function FileIO::closeFile() {
%this.fileName.close();
}
function FileIO::TestWrite(%this) {
%this.writeln(" -I- ");
%this.writeln("-Want-");
%this.writeln(" -This- ");
%this.writeln(" -To- ");
%this.writeln("-Work-");
%this.closeFile();
}
//-----------------------------------------------------------------
// Misc
//-----------------------------------------------------------------
//%this.fileName.openforwrite(%this.path @ "test.txt");
//%file.delete();Working code for anyone interested.
Associate Konrad Kiss
Bitgap Games
%myNewObject = new ScriptObject() { class = "FileIO"; };