Game Development Community

Files and paths

by Vern Jensen · in Torque Game Builder · 05/07/2007 (2:26 pm) · 18 replies

Just trying to create my first files.

function createFile()
{
	%myFileObject = new FileObject();
		
	%myFileObject.openForWrite("~/MyTestFile");
	%myFileObject.writeLine("Testing this file!");
	%myFileObject.close();
	%myFileObject.delete();
}

But where is this file put? I'm testing this on OS X using TGB 1.5 Beta 3, and I try searching for "MyTestFIle" after running the code, and nothing is found. I search around my project's various directories, and don't see anything. But no compile/run time errors are given.

Do I need to make use of the "FileSystem" object to make paths? How do I know where my files are going to go when I create them? What if I used "MyTestFile" instead of "~/MyTestFile" -- what's the difference?

#1
05/07/2007 (2:38 pm)
Quick follow-up. If I attempt to read the file I just created, I get a blank line written to the console when I echo the results:

%myFileObject = new FileObject();
		%myFileObject.openForWrite("VernsTestFile");
		%myFileObject.writeLine("Testing this file!");
		%myFileObject.close();
		%myFileObject.delete();
		
		%myFileObject = new FileObject();	
		%myFileObject.openForRead("VernsTestFile");
		%myString = %myFileObject.readLine();
		%myFileObject.close();
		%myFileObject.delete();
		
		echo(%myString);

So indeed it seems no file is being created, anywhere -- not just that I can't find it. So I guess the new question is, what am I doing wrong?
#2
05/09/2007 (12:38 pm)
Surely *someone* on here has worked with files before...
#3
05/10/2007 (6:55 am)
I ended up putting in a full path for the file; and it works like a charm.
#4
05/10/2007 (12:32 pm)
Could you post what you did? I'm not sure exactly how to do a "full path" -- where do you start? (Everything in main.cs where it exec's the script files uses ~ or . to start the paths...)
#5
05/11/2007 (10:11 am)
You can use the expandFilename() function

%fullfile=expandFilename("~/MyTestFile");
%myFileObject = new FileObject();
%myFileObject.openForWrite(%fullfile);
etc...

In my experience, using expandFIlename anytime I want to refer to a stored file is good practice.

Greg
#6
05/11/2007 (10:59 am)
Quote:In my experience, using expandFIlename anytime I want to refer to a stored file is good practice.

seconded
#7
05/11/2007 (11:39 am)
Okay, so I tried:

%filePath = expandFilename("~/VernsTestFile");
		
		%myFileObject = new FileObject();
		%myFileObject.openForWrite(%filePath);
		%myFileObject.writeLine("Testing this file!");
		%myFileObject.close();
		%myFileObject.delete();
		
		%myFileObject = new FileObject();	
		%myFileObject.openForRead(%filePath);
		%myString = %myFileObject.readLine();
		%myFileObject.close();
		%myFileObject.delete();
		
		echo("Contents of file are: " @ %myString);

I get this in the console:

Contents of file are:

In other words, *nothing*. The file still doesn't seem to be getting created.

Is this a Mac 1.5 beta 3 bug??? Or a Mac bug in general? Or something I'm not doing right?
#8
05/11/2007 (11:44 am)
Okay, how about this: I actually don't care at this point about *creating* files. I just tried creating it so I could find out exactly where it would be put when I used "~/VernsTestFile" as the path.

So all I really need to know is, in order to load a file I created using any old text editor, with a path of "~/VernsTestFile" where do I put the file?
#9
05/11/2007 (7:04 pm)
Do this:
%filePath = expandFilename("~/VernsTestFile");
echo(%filePath);

and see what it says.

Greg
#10
05/14/2007 (11:33 am)
Thanks, didn't think of that.

The path is "game/VernsTestFile" so I created a file there named VernsTestFile, and lo and behold, the game loads it!!! Whoohoo.

So now the question is, why is the Torque command openForWrite failing to create a new file in that same folder? I don't have any strange permission setup on my MacOS X system... it should be allowed to create the file just fine...
#11
05/14/2007 (7:42 pm)
If VernsTestFile is a directory in /games then you may be having a problem trying to create a file in the same place where there is already a folder of the same name.

Greg
#12
05/14/2007 (10:57 pm)
Nope, there is no directory named VernsTestFile. I've made no custom directories in any of the folder hierarchies.
#13
05/15/2007 (3:30 am)
Try this and see what happens:
%filePath = expandFilename("~/VernsTestFile");
		
		%myFileObject = new FileObject();
		%myFileObject.openForWrite(%filePath @ "testing.txt");
		%myFileObject.writeLine("Testing this file!");
		%myFileObject.close();
		%myFileObject.delete();
		
		%myFileObject = new FileObject();	
		%myFileObject.openForRead(%filePath @ "testing.txt");
		%myString = %myFileObject.readLine();
		%myFileObject.close();
		%myFileObject.delete();
		
		echo("Contents of file are: " @ %myString);

Greg
#14
05/15/2007 (3:55 am)
Id just do this

%myFileObject.openForWrite("./test.txt");

This creates test.txt in the same folder as the script that runs the above code.
#15
05/15/2007 (10:11 am)
Greg -- Your code gives me the same result -- "Contents of file are: " with nothing after it, indicating nothing was ever written to disk.

Anthony -- I also get the same result when using your suggestion.

Does this sound like a bug I should report in the Bugs forum?
#16
05/05/2010 (7:08 am)
So I know this is 3 yrs. old now but I am having this same problem. Did anyone ever find a solution?
#17
05/05/2010 (7:31 am)
I dont' use a Mac much but I think it was answered more recently here.

`Patrick
#18
05/05/2010 (9:37 am)
This was fixed (for me) in one of the TGB 1.7.x updates. Or maybe it was 1.5.x.