file output in t2D
by Nathan Morton · in Torque Game Builder · 12/12/2011 (4:53 pm) · 11 replies
Trying to write to a file, having a heck of a time code is below.
If I could get a few hints as to what I'm doing wrong I'd appreciate it.
If I could get a few hints as to what I'm doing wrong I'd appreciate it.
function saveHighScore()
{
%file = new FileObject();
%filelocation = "C:/Documents and Settings/Nathan/Desktop/test.txt";
echo(%filelocation);
if(%file.openForWrite("C:\Documents and Settings\Nathan\Desktop\test.txt"))
{
%file.writeLine("TestText");
echo("File Written");
}
else
{
error("File is not open for writing");
}
%file.close();
%file.delete();
}
#2
from the code I posted I'm actually getting the "File Written" message back on the console, but no file ever shows up.
that would leave me to believe that my file is being opened and written to, I assume that I don't need a %file.save(); in there anywhere.
my second assumption is that %file.delete(); is actually only deleting my file object and *not* the file itself.
12/14/2011 (6:51 pm)
Actually I'm using a win xp machinefrom the code I posted I'm actually getting the "File Written" message back on the console, but no file ever shows up.
that would leave me to believe that my file is being opened and written to, I assume that I don't need a %file.save(); in there anywhere.
my second assumption is that %file.delete(); is actually only deleting my file object and *not* the file itself.
#3
01/08/2012 (7:18 pm)
still fighting this, any help would be more than appreciated
#4
01/08/2012 (11:58 pm)
You can't write anywhere in the system. For more than one reason there's specific directory TGB can write to on each supported platform. You would use something like "~/saves/test.txt" to save file, and TGB then decides where to put it - for windows this place is somewhere under appData.
#5
01/14/2012 (9:45 am)
Here's my code for File I/O, but it looks to me that your problem is you delete the file that you are writing to for no reason. Even if the file did write as it was supposed to you delete it at the end of the function.$highScoreNum = 9
function saveHighScores()
{
%filePath = "~/fileIO/HighScores.txt";
%file = new FileObject();
if(%file.openForWrite(%filePath))
{
for(%i = 0; %i <= %highScoreNum; %i++)
{
%file.writeLine($highScores[%i]);
}
echo("The files can be found here: " @ %filePath);
}
else
{
echo("We have a problem writing to this location: " @ %filePath);
}
%file.close();
}
function loadHighScores()
{
%filePath = "~/fileIO/HighScores.txt";
%file = new FileObject();
if(%file.openForRead(%filePath))
{
for(%i = 0; %i <= $highScoreNum; %i++) //read each high score in a different line (1-10)
{
$highScores[%i] = %file.readLine();
}
echo("I got the scores from over here: " @ %filePath);
}
else
{
echo("Unable to read a thing in this location: " @ %filePath);
}
%file.close();
}
#6
Had some problems with my PC so I'm having to reinstall windows. I'll check out the suggested solutions in the next few days and let you know how it turned out.
01/15/2012 (7:37 pm)
@Rpahut - if I read that correctly, I don't get to choose where TGB puts the save file?Had some problems with my PC so I'm having to reinstall windows. I'll check out the suggested solutions in the next few days and let you know how it turned out.
#7
Hope it helps!
01/17/2012 (12:55 pm)
Next time you try this, look in your user/appdata/ folder for a subfolder called independent. For more information look at the File IO document in the T2D documentation. There is a link in there to another thread where this is discussed as well.Hope it helps!
#8
01/18/2012 (2:06 pm)
Got it working thanks for all the help guys!
#9
I'd rather have my files in a "static" location.
If it's not possible that's fine I just wanted to check.
Thanks again!
02/16/2012 (6:30 pm)
Sorry to necro this thread, any chance I can output a file someone OTHER than app data?I'd rather have my files in a "static" location.
If it's not possible that's fine I just wanted to check.
Thanks again!
#10
The tilde at the start means the root of your game folder - now specially if your game is running from a secured location on more recent systems (as is the Program Files folder) it will most likelly write to the appdata folder.
However, if you change that to a non-secure location (like C:/HighScores.txt) it should work as you want.
02/17/2012 (4:13 am)
If you are using Glenn's code, he has:%filePath = "~/fileIO/HighScores.txt";
The tilde at the start means the root of your game folder - now specially if your game is running from a secured location on more recent systems (as is the Program Files folder) it will most likelly write to the appdata folder.
However, if you change that to a non-secure location (like C:/HighScores.txt) it should work as you want.
#11
02/18/2012 (8:17 am)
See the Documentation under Scripting > File IO Tutorial - it also has information on this.
Torque 3D Owner Harry Durnan
The way I got file read/write to work on my machine was to use a default path:
if(%file.openForWrite(expandFilename("~/saves/ <file name>)
{%file.writeLine(<stuff>;}
It creates the files under a specific directory under Users/<user name>/App Data/Roaming/Independent/<program name> on those systems. There's probably some other way around it, but it really depends what you are trying to do. Good enough for save games, but if your trying to do something fancier might need to do something else.