Game Development Community

Writing to a file with: openForWrite

by PsychicParrot · in Torque Game Builder · 08/16/2007 (2:51 pm) · 7 replies

This might be related to the "save()" thread posted a month ago.
www.garagegames.com/mg/forums/result.thread.php?qt=64729


I'm trying to write to a file to save and load data during play.
I pulled and used the code directly from the TGB sample to make certain that I wasn't doing something silly.
I even tried dropping that code into the 1.5.1 BehaviorPlayground sample and still failed.

It seems like the file is never "open for writing".

Here is the code from the documentation:

function writeFile()
{
   %file = new FileObject();

   if(%file.openForWrite("~/data/files/test.txt"))
   {
      %file.writeLine("Hello World!");
      echo("File Written");
   }else{
      error("File is not open for writing");
   }
   %file.close();
   %file.delete();
}

#1
08/16/2007 (3:27 pm)
The only way I got openforwrite to work (returns 1) is by putting my project directory in c: and then opening it with the full path, like %file.openForWrite("C:/myproject/file.ext");
#2
08/17/2007 (7:18 am)
Does anyone else find this function doesn't work anymore?

Using the full filepath is not an option, as the game is for retail :o

It's a huge problem for us, since we need to be able to do saving and loading of files.

Anyone at GG know the answer to this?

Thanks!
#3
08/17/2007 (9:12 am)
First exec this code before writing any file :
%modpath = findfirstfile("*/game/main.cs");
if(isFile(%modpath))
$mymodpath = filePath(%modpath) @ "/";
else
{
	%modpath = findFirstFile("*/game/main.cs.dso");
	$mymodpath= filePath(%modpath) @ "/";
}

Then when you write a path, write it this way :
%file.openforwrite($mymodpath @ "foo/test.txt")
And it will save in the "game/foo/" folder.

It works for save() and openforwrite() (not for export since 1.5.1). This is the only way to have save working.
#4
08/17/2007 (11:26 am)
Excellent, works like a charm, thank you so much Ben G!
#5
09/02/2007 (10:44 pm)
Works great - thanks so much!
#6
07/23/2012 (1:53 pm)
This is not working for me. Was hoping this would be it.

Edit - Gee maybe I should look at the dates before posting.
#7
07/24/2012 (6:05 am)
@Luis, have you seen this thread?

http://www.garagegames.com/community/forums/viewthread/113819