File IO Tutorial?
by Matthew Mather · in Torque Developer Network · 02/10/2009 (4:50 pm) · 4 replies
I'm on the programming team for a student project. Our game's web site is mission9games.sitesled.com/
Anyway, we've figured out that we're going to need to have save files. I have no experience with file IO so far (it's not covered in Structured Programming).
I just need to be able to write and retrieve a few integers to and from a file using Torquescript. I can't seem to find any straightforward info on this.
Anyway, we've figured out that we're going to need to have save files. I have no experience with file IO so far (it's not covered in Structured Programming).
I just need to be able to write and retrieve a few integers to and from a file using Torquescript. I can't seem to find any straightforward info on this.
#2
02/10/2009 (5:48 pm)
Here are the SimObject functions://******************************************************************************
//******************************************************************************
// SimObject functions
//******************************************************************************
//******************************************************************************
//******************************************************************************
// Function: ReleaseSimObjects
// Description: Recursively frees a SimObject, and all of it's children.
//******************************************************************************
function ReleaseSimObjects(%object)
{
// First, release all the children.
%fldcnt = %object.getDynamicFieldCount();
for (%i=0;%i<%fldcnt;%i++)
{
%fldname = %object.getDynamicField(%i);
%fldvalue = %object.getFieldValue(%fldname);
if (isObject(%fldvalue))
{
ReleaseSimObjects(%fldvalue);
}
}
%object.delete();
}
//******************************************************************************
// Function: WriteSimObject
// Description: Writes a SimObject to an open XML file. Will recursively call
// itself to write any member SimObjects as children.
//******************************************************************************
function WriteSimObject(%Xml, %object, %brecurse)
{
%ClassName = %object.getName();
%Xml.writeClassBegin(%ClassName);
// write dynamic fields
%fldcnt = %object.getDynamicFieldCount();
if (%fldcnt == 0)
{
// must have at least one field!!!
%object.savemarker = 1;
%fldcnt = 1;
}
for (%i=0;%i<%fldcnt;%i++)
{
%fldname = %object.getDynamicField(%i);
%fldvalue = %object.getFieldValue(%fldname);
if (%brecurse && isObject(%fldvalue))
{
WriteSimObject(%Xml,%fldvalue);
}
else
{
%Xml.writeField(%fldname,%fldvalue);
}
}
%Xml.writeClassEnd();
}
//******************************************************************************
// Function: ReadSimObject
// Description: Reads a SimObject from an open XML file. Will recursively load
// any child SimObjects
//******************************************************************************
function ReadSimObject(%Xml,%SimObject,%ClassName)
{
echo("Sim object " SPC %SimObject );
echo("Loading Sim Object: " @ %ClassName);
%Xml.readClassBegin(%ClassName);
%SimObject.index = 0;
while (true)
{
%FieldName = %Xml.getFieldNameByIndex(%SimObject.index);
if (%FieldName $= "") break;
if (%Xml.isClass(%SimObject.index))
{
// Clear out any junk by freeing and recreating the members.
%childobject = %SimObject.getFieldValue(%FieldName);
if (isObject(%childobject))
{
ReleaseSimObjects(%childobject);
}
%NewObject = new SimObject();
%NewObject.internalName = %FieldName;
%NewObject.Name = %FieldName;
%SimObject.setFieldValue(%FieldName,%NewObject);
ReadSimObject(%Xml,%NewObject,%FieldName);
}
else
{
%cinfo = %Xml.readFieldByIndex(%SimObject.index);
%FieldName = getField(%cinfo,0);
%FieldValue = getField(%cinfo,1);
%SimObject.SetFieldValue(%FieldName,%FieldValue);
}
%SimObject.index++;
}
%Xml.readClassEnd();
}
#3
i think you might be able to get away with something a lot simpler than an xml spec.
basic torquescript file io looks sort of like the following,
which should be enough, combined with stuff like getWord(), getWordCount() etc to set you on the way to reading & writing simple values:
02/10/2009 (6:15 pm)
if all you want is to read and write a few integersi think you might be able to get away with something a lot simpler than an xml spec.
basic torquescript file io looks sort of like the following,
which should be enough, combined with stuff like getWord(), getWordCount() etc to set you on the way to reading & writing simple values:
function writeSomeStuff(%fileName, %someStuff, %someMoreStuff)
{
%fo = new FileObject();
if (!%fo.openForWrite(%filename))
{
error("could not open file for write -" SPC %filename);
}
else
{
%fo.writeLine(%someStuff );
%fo.writeLine(%someMoreStuff);
%fo.close();
}
%fo.delete();
}
function readSomeStuff(%fileName)
{
%fo = new FileObject();
if (!%fo.openForRead(%filename))
{
error("could not open file for read -" SPC %filename);
}
else
{
%someStuff = %fo.readLine();
%someMoreStuff = %fo.writeLine();
%fo.close();
echo("read this stuff:" SPC %someStuff );
echo("and this stuff:" SPC %someMoreStuff);
}
%fo.delete();
}
#4
The other two are way more advanced than I can use right now. :)
02/12/2009 (10:31 am)
Thanks guys! The third one was the sort of thing I was looking for. The other two are way more advanced than I can use right now. :)
Associate Jaimi McEntire
King of Flapjacks
Here are some code snippets from my save system - they should get you going in the right direction.
function OpenXML(%FileName) { if (isFile(%FileName)) { %Xml = new ScriptObject() {class = "XML";}; %Xml.beginRead(ExpandFilename(%FileName)); %Header = %Xml.readHeader(); %headertype = getField(%Header,0); %headertarget = getField(%Header,1); /* code snipped */ return %Xml; } return ""; // doesn't exist, but don't send an error. } function LoadGame(%FilePath, %MissionFile, %PlayerName) { %Xml = OpenXML(%FilePath); if (%Xml $= "") return; %Xml.readClassBegin("SaveData"); ReadSimObject(%Xml,SaveFileInfo,"SaveFileInfo"); ReadSimObject(%Xml,GameState,"GameState"); %Xml.readClassEnd(); %Xml.delete(); /* This actually starts a game, but you don't need to do this... createServer("SinglePlayer", %MissionFile); %conn = new GameConnection(ServerConnection); RootGroup.add(ServerConnection); %conn.setConnectArgs(%PlayerName); %conn.setJoinPassword($Client::Password); %conn.connectLocal(); */ } function SaveGame(%FilePath) { %Xml = new ScriptObject() {class = "XML";}; %Xml.beginWrite(%FilePath); %Xml.writeHeader("SinglePlayer","MyApp","1.0",""); %Xml.writeClassBegin("SaveData"); // You would write whatever you wanted here, instead of // using my SaveFileInfo and GameState objects WriteSimObject(%Xml,SaveFileInfo,false); WriteSimObject(%Xml,GameState,true); %Xml.writeClassEnd(); %Xml.endWrite(); }