Game Development Community

TorqueScript FileObject path issues

by Dave "Jellybit" Freeman · in Torque Game Builder · 12/25/2005 (10:34 pm) · 3 replies

I'm trying to figure out how to read the second line from a text file in Torque. It's giving me problems in merely finding the file, and I'm not sure why. It says I have an error on line 4 (OpenForRead line). The only documentation I could find refers to the path as "starter.fps" in the example. I'm assuming that's a TGE thing, and has nothing to do with T2D. But with the way this forum/TDN works for owners of only T2D, it's hard to find that out for sure. So, here's my code:
function readFile()
{
	%file = new FileObject();
	%file.OpenForRead("~/client/levels/easy.txt");
	while( !%file.isEOF() )
	{
		%line = %file.readline();
		echo(%line);
	}
}
The path and filename exist. I even tried to create "file.txt" without any path, just to see how this function sees paths, and what it considers the root. This didn't do anything, except for give an error message, and I still haven't figured out how to get it to find the path successfully. I've tried with/without quotes. I have no idea what it wants for the path anymore. Can anyone help me out? Has anyone saved/read from files? Thanks for your help!

- Dave

#1
12/26/2005 (10:22 am)
I found out the problem. In the TDN entry on reading/writing files (http://tdn.garagegames.com/wiki/TorqueScript_FileObject) , it uses a left and right quote, like so: "starter.fps/test.txt" (edit: Apparently, this forum is incapable of showing them. But trust me, they are there, and I can see them as I edit this post)

This, used in the TDN entry, actually stopped my script from working entirely, and returned an error. I changed it to regular quotes, and it returns no error. It just simply does not work :). I run "readFile();" in the console, and nothing happens. I made a write function as well (using no path, just a filename), and that also does nothing. not even returning an error. It echoes some test text I threw in the script, so I know it's running the function. The functions just don't write or read anything at all. It apparently thinks the path is valid. I don't know what's wrong. Am I giving the path properly? (yes, the path does actually point to the file. I'm just asking about syntax) It would return an error if it were wrong, right?
#2
12/27/2005 (10:39 am)
David,

The problem isn't which direction slashes you are using, either should work. The problem is that, as far as I know, only object-fields can use the abbreviations such as "~". Before you pass something like this into any functions (in other words, not a field) you must expand it using the function "expandFilename()" which will convert abbreviations such as "." and "~" into their correct context. In your example, you want the "~" to be replaced with the current MOD context name; "expandFilename()" will do this for you.

Also, there are a few things I'd suggest to make this stuff safer. First is that you can quickly test for the presence of a file (at least one that the resource-manager knows about) with "isFile()" which returns a flag indicating if the file is valid. This saves you creating a file-object in the first place. On the subject of the "FileObject"; be careful to delete the object afterwards otherwise you'll leak memory.

You may know all this and your example was just for brevity but I thought it was good to point this out in case anyone else reads it as well.

Anyway, being as it's xmas and I'm feeling happy; here's an example of what I mean:

// Read Level-File Lines.
function readLevelFile( %filename )
{
	// Expand Filename Abbreviations.
	%filename = expandFilename(%filename);
	
	// Valid File?
	if ( isFile( %filename ) )
	{
		// Yes, so create File Object.
		%fileObject = new FileObject();
		
		// Open File?
		if ( %fileObject.OpenForRead(%filename) )
		{
			// Yes, so read File.
			while( !%fileObject.isEOF() )
			{
				// Read Line.
				%line = %fileObject.readline();
				// Echo Line.
				echo( %line );
			}
		}
		else
		{
			// No, so error.
			echo("readLevelFile() - Error opening level-file:" SPC %filename);
		}
		
		// Delete File Object.
		%fileObject.delete();
	}
	else
	{
		// No, so error.
		echo("readLevelFile() - Error locating level-file:" SPC %filename);
	}
}
... calling it with ...
// Read Level File.
	readLevelFile("~/client/levels/easy.txt");

I personally feel that at least the "FileObject" should automatically expand the filename and taken to its furthest extreme, added to the resource-manager for any file-resources that are selected. I'm sure there's a good reason not to do the later but it would make things easier.

I guess the rule for today is, if in doubt, pass your filenames through "expandFilename()".

Hope this helps,

- Melv.
#3
01/09/2006 (5:17 pm)
Thanxs Melv this "expandFilename" really was handly...