Game Development Community

TGB 1.7.5 File I/O Save/Load

by Peter K · in Torque Game Builder · 02/21/2011 (4:38 pm) · 3 replies

I'm having problems saving files and reading them again. At this point i'm able to use .savecopy() to create a file in the game directory however, using .loadcopy() I get the error t2dSceneObject::loadCopy() - Could not Open File 'test.txt' for Copy. I have tried adding file paths to the file name but no luck there either.

I've also tried openforwrite which doesn't seem to to create a file even in the users/appdata/roaming/company/projectname directories. I'm using vista by the way. I've tried some different examples here on the forums but not have worked.

so some examples i can create the file with $testplayer.savecopy("test.txt");
However the using $testplayer.loadcopy("test.txt") give the error

The other example i have I pulled off the site here (and now i can't find the page again)
function filewrite(%this)
{
%saveFile = new FileObject();

%dataFile = expandFilename("game/save/MyGameSaveData.dat");
%saveFile.openForWrite(%dataFile);

%saveFile.writeLine("Saved Game Data for MyGame, Version 1.0");

%saveFile.writeLine("Test - Hello World");


%saveFile.close();
%saveFile.delete();
}

function frtest()
{
%loadFile = new FileObject();
%dataFile = expandFilename("game/save/MyGameSaveData.dat");

// Use the 'openForAppend' workaround since loading files does not
// work unless they are already in memory, which openForAppend will
// put them in memory.
%loadFile.openForAppend(%dataFile);
%loadFile.close();

// now, officially open saved game data file
%loadFile.openForRead(%dataFile);

// grab header line
%line = %loadFile.readLine();
echo("Loading " @ %line);


%loadFile.close();
%loadFile.delete(); // delete %loadFile object, not file itself

}

If anyone has any ideas to try I would love to hear them. Right now either option for read/writing would work for me :) I've been working on this for a week now and I'm out of ideas.

#1
02/22/2011 (2:39 am)
I don't have vista to try it there, but following works perfectly fine for Windows 7:

objectname.saveCopy("game/data/obj.txt")
objectname.loadCopy("game/data/obj.txt")

objectname.saveCopy("game/save/obj.txt")
objectname.loadCopy("game/save/obj.txt")

Anyway, it is bad idea to save stuff into game directory. FileObject is more appropriate way, and example you have posted above looks okay. Make sure you've looked for your .dat file in a right directory and its not hidden by Windows.

Open 'Build Project' dialog in the game builder, there you will see the actual Company and Product names your game is built with. Look for those in the Application Data directory. You can press Windows key + R and type in '%appData%' to make sure you open your AppData directory and not something else.
#2
02/22/2011 (8:38 am)
@ Rpahut: Thanks Rpahut I tried those but could not get loadcopy to work for some reason.

In the end I stayed up way to late last night going through the TGB forum post by post looking for another example to try. ( I have a hard time stopping for the night when I feel like the answer can't be that hard )

I found a post (http://www.garagegames.com/community/forums/viewthread/72837) from 2008 with some examples and managed to get something going, and it's something I can work with for now. I'll have to revisit this later i'm sure, but for now this will work.

Write to file

$stream= new FileStreamObject();
$stream.open("test.obj","Write");
$stream.writeline("test writ line);
$stream.close();
$stream.delete();

Read from file

$stream = new filestreamobject();
$stream.open("test.obj", "read");
while(!$stream.isEOF())
{
%line=$stream.readline();
echo("LINE CONTENTS:" @ %line);
}
$stream.close();
$stream.delete();
#3
11/16/2012 (5:23 am)
I just made a save code. It uses a save directory and you might want to check it out: http://www.garagegames.com/community/resources/view/21997