Game Development Community

OpenForWrite() issues

by Q90Games - Christopher Evans · in Technical Issues · 12/12/2007 (1:06 pm) · 5 replies

Every time I try to use openForWrite() to write out a file it returns false. I've tried numerous things and have found that when i use a filepath that the script isn't adding the base directory information to the string and so the only way to get it to open correctly is to hardcode the entire file path starting from c:\. Any suggestions on how I could add the base directory info from code?

#1
12/12/2007 (4:32 pm)
Why don't you post your script code. You shouldn't have to hard code the path in, you should start it with either ~/ or ./

I'm quoting from a pdf, maybe it will help you -

Direct paths start with a slash (/). For example, "/starter.fps/main.cs" points to the file "main.cs" under the
game directory "starter.fps", where "starter.fps" is in the root directory (the directory where the executable is
started from.

Relative pathing can be accomplished in three basic ways.
In the first method, if an unadorned name is used as the first part of a directory ("starter.fps/test.cs"), the
engine will assume that this first name is the name game directory, but then if the unadorned name does not
match the game directory, the file match/search will fail. In general, you should not use unadorned names.

In the second method, if a tilde (~) is used as the first part of a directory ("~/test.cs"), the engine will
assume that the tilde should be replaced by the root-child this file lives in. For example, the root child of
"starter.fps/client/doit.cs" is "starter.fps". Therefore, if we use this path "~/somefile.cs" in a command within
the file "doit.cs", the path will be expanded to be "starter.fps/somefile.cs".

In the third and final relative pathing method, if a dot (.) is used as the first part of a directory ("./test.cs"),
then the dot is expanded to be the root- path that the current file resides in. For example, the root-path of
"starter.fps/client/doit.cs" is "starter.fps/client". Therefore, if we use this path "./somefile.cs" in a command
within the file "doit.cs", the path will be expanded to be "starter.fps/client/somefile.cs".
#2
12/13/2007 (3:55 pm)
%fullFilePath = expandFilename("~/data/audio/NeedThisFile.txt");
// I've also tried: %fullFilePath = expandFilename("data/audio/NeedThisFile.txt");
// and %fullFilePath ="/data/audio/NeedThisFile.txt";

%file2 = new FileObject();

if(%file2.openForWrite(%fullFilePath))// This doesn't for some reason
//%file.openForWrite("C:/code/wizdev/Game/game/NeedThisFile.txt")// This Works
{
%file2.writeLine("FileIO working");
%file2.close();
}
else
{
echo("Could not open file: __________ " @ %unexpandedName);
}

%file2.delete();


We have found a work around for this using:
%fullFilePath = findFirstFile("*NeedThisFile.txt");

but i was trying to figure out why we were having this problem.
#3
12/13/2007 (7:43 pm)
I don't use expandfilename with my file objects and it works just fine. I do something like

%filename = "./characters/text.txt";
#4
12/14/2007 (6:37 pm)
Was this figured out? You seem to be trying to open the files in several places.

Is the file read only (some source control programs do this)?

Where is the actual file located? In your example you provide two different paths (one that works and one that doesn't) are you sure you are referencing the correct path?

Also, what does %fullFilePath contain (echo it to the console) after the expandFileName call?

Like Steve said, expandFileName shouldn't be needed but it also shouldn't hurt anything.

Open file for write will not create new folders. Does the audio folder exist?

If "C:/code/wizdev/Game/game/NeedThisFile.txt" works than "~/NeedThisFile.txt" should work because they are equivalent references. Does that work for you?


Just trying to help here.
#5
11/09/2009 (6:05 am)
Hi Q90Games,
I tested the code shown next, yeah, indeed it can create the file GameSaveData.txt, and also can read the data written into the file. but the very confusing thing is that I can not find the file in the path. any idea?

%saveFile = new FileObject();
%dataFile = "game/data/save/GameSaveData.txt";
%saveFile.openForWrite(%dataFile);
%saveFile.writeLine(" Saved Game Data for MyGame, Version 1.0");
%saveFile.close();
%saveFile.openForRead(%dataFile);
echo(%saveFile.readLine() NL %dataFile );
%saveFile.close();
%saveFile.delete();