Game Development Community

FileOpenForRead is not working

by Katrina Rose · in Torque Game Engine · 11/08/2004 (6:32 am) · 7 replies

function saveTextFile(%fileName)
{
  echo("----------------------Saving File-------------------------------");
  %file = new FileObject();
  %file.openForAppend("../players/players.psc");
  %file.writeLine($pref::Player::Name@" "@$myPlayerScore);
  %file.close();
  %file.delete();
}
function loadTextFile(%fileName)
{
  $scoreFile = 0;
  %file = new FileObject();
  %file.openForRead("../players/players.psc");
  while(!%file.isEOF())
	{
	  //%input = %file.readLine();
        echo(%file.readLine());
        $scoreFiles[$scoreFile] = %file.readLine();
        $scoreFile++;
	}
   %file.close();
   %file.delete();
}
when I use this The file is saved on my desktop in a folder called players, but the !isEOF is returning true for some reason when I try to load the file back in even though the file contains the following:
cox 0
joe 0
jenny 0

Why is openForRead not working for this file?

Marrion Cox

#1
11/08/2004 (7:07 am)
OpenFileForRead uses the ResourceManager to open the file stream for reading. If the file is not found in the resource manager, it will not open the file.

When you call openFileForAppend, it will open the file for WRITE using the resource manager, which will automatically add it to the resource manager. There are a few directions you could go here. One is to hack around it, the other is to put that file into a directory which will be scanned by the resource manager on startup.

This is the hack:
function loadTextFile(%fileName)
{
  $scoreFile = 0;
  %file = new FileObject();
  // BEGIN HACK---
  %file.openForAppend("../players/players.psc");
  %file.close();
  // END HACK---
  %file.openForRead("../players/players.psc");
  while(!%file.isEOF())
   {
     //%input = %file.readLine();
        echo(%file.readLine());
        $scoreFiles[$scoreFile] = %file.readLine();
        $scoreFile++;
   }
   %file.close();
   %file.delete();
}
What this does is open the file for writing using the resource manager. You should use "Append" not "write" because append will not clobber the file, and you are not writing any data, you just want to add it to the resource manager.

The other method is to put the file somewhere else. Maybe something like "~/players/players.psc", that would put it in the 'players' directory inside the mod directory. So if your mod was called "foo_fps", the path would be "foo_fps/players/players.psc".

Hope that helps.
#2
11/08/2004 (7:24 am)
Ye the path is really important here ,i hade lots of problems before i figured out why it was not working .
I hade same problem Katrina or Marrion :)
#3
11/08/2004 (7:51 am)
Thanks Pat,

That worked great. One more question. What do I need to put in the %fileName to write the file to c:/windows/temp? I have tried "c:/windows/temp, but it does not write the file there. We are running the game from a CD and would like to keep the player data on the machine because we don't want a player to start the next mission until they have played the first mission. Thanks again for your help.

Marrion
#4
11/08/2004 (7:58 am)
IIRC.. For security reasons TGE will not see any files/dirs outside of its predefined modpath

see main.cs in the example dir

$modPath = pushback($userMods, $baseMods, ";");
setModPaths($modPath);

-Ron
#5
11/08/2004 (8:03 am)
Thanks Ron,

What do I need to change to have C:/windows/temp added to the list?

Thanks for your help.

Marrion
#6
11/08/2004 (8:30 am)
You'll have to muck around in fileObject.cc to have it not use the resource manager to open the file stream. Instead use FileStream directly. That should work with a bit of tweeking.
#7
11/08/2004 (8:36 am)
I am good at C#.NET, but not too good at C++. What do I need to change to accomplish this? Your help is greatly apreciated.

Marrion