Game Development Community

File Input/Output question [Update: Need a little more help, please!]

by Matt · in Torque Game Builder · 01/11/2013 (11:00 am) · 7 replies

Hello all.

First of all, I have already read some responses to this issue on the forums but none were satisfactory to me. When you write a file with a FileObject, even though you tell it to go to ./game/saves/charData.txt, it actually goes off into space to a place called APPDATA. I understand the reasoning, because you need to run the game in administrator mode to edit files in install directory. So can anyone explain:

When I make character data saves, or any save, I want them easily accessible so people can share them etc. Not hidden in some obscure hidden folder. For instance, I have the game Dragon Age 2. In My Documents, under Bioware, Dragonage2, characters, saves, and boom there is everything! I want this power to know where my files are put. Is there no way to actually put a true destination for your files? It just seems like I should be able to put files where I want. Thanks everyone.

PS. I'm quite willing to edit the C++ code, as I think this is a rather important thing, and
it discourages me.

About the author

Recent Threads


#1
01/11/2013 (12:13 pm)
You can't save just any old where under Windows - it throws fits if you don't have the correct permissions. Do not tell it to save a file to <your game directory>/saves/savefile.txt. Try telling it to save to the user's documents folder - in platform/platformFileIO.cc at the end of the file there is a section that is def'd out with TORQUE_TOOLS. Remove the ifdef block and recompile, then you can use getUserHomeDirectory to get their My Documents folder and save to My Documents/<yourGame>/saves.
#2
01/11/2013 (12:59 pm)
Thank you so much for the reply! I killed the ifdef block, and see that getUserHomeDirectory is now being compiled. How do I use this though? In other words, do I have to modify more c++ code or?

Ive tried in the script:

%myDocs = getUserHomeDirectory();
%myDocs = getUserHomeDirectory;

I would use it like: %thePath = %myDocs @ "/My Documents/TheGame/Saves/save1.txt"

It doesnt recognize the function, so I'm sure im doing this wrong. Thank you again for the quick reply, I greatly appreciate it.


#3
01/11/2013 (10:57 pm)
You have to copy the new game executable to your project from the tgb/gameData/T2DProject folder and replace the old one there. When the source project compiles it doesn't update the executable in your game projects....

Otherwise, yes - you use it like so:
%savePath = getUserHomeDirectory()@"/MyGameCompany/MyGame/Saves/SaveFile.txt";

getUserHomeDirectory() should point to <User>/My Documents.
#4
01/12/2013 (10:30 am)
Oh I see - I wont be at the computer in question until Monday, but that sounds like it will fix it. All I had done was change the c++, recompile, and then fire up Torsion script editor to edit the script. Thank you again for taking the time to help me, I just think its useful to be able to easily locate save files on the disk. Have a nice day!
#5
01/13/2013 (11:52 pm)
Okay, I tried your suggestion. The function getUserHomeDirectory() now works, and points to C:/Users/Matt/Documents as shown by an echo. But whenever I use the following code:

%savePath = getUserHomeDirectory()@"/MyGameCompany/MyGame/Saves/SaveFile.txt";

echo(%savePath);

if(%file.openForWrite(%savePath))
{
%file.writeLine("Hello World!!");
echo("File Written");
}
else
{
error("File is not open for writing");
echo(%savePath);
}

It says File is not open for writing. All folders/etc are closed by the way, only Torsion editor and Google Chrome are running.

Now if I say %savePath = "./game/data/saves/test1.txt" everything works great, says file written (but of course its in appData, not game/data/saves/). So my new roadblock is file.openForWrite(%savePath) is returning false for C:/Users/Matt/Documents/MyGameCompany/MyGame/Saves/test1.txt
I thought it might not be able to create folders, so I tried creating the folders for it in advance. So all it had to do was go to that exact path and write test1.txt. I'm really sorry to keep bothering you with this issue, but I feel like its close to being resolved.

As a review, everything I've done:
1. Brought up Engine source Solution in Visual Studio 2010.
2. Un-ifdef'd the block of code you referred to.
3. Re-compiled the c++ code, and ensured the TGBGameBuilder.exe was up to date.
4. Dragged the new game executable from tgb/gameData/T2DProject to my project folder.
5. Function getUserHomeDirectory() now works.
6. Path is C:/Users/Matt/Documents/MyGameCompany/MyGame/Saves/test1.txt that I pass to %file.openForWrite().
7. openForWrite() returns false, meaning I could not create/open the file.
8. In-project paths work, like ./game/data/saves/whatever.txt.
9. I've tried running as administrator (TGB, Torsion, and the game itself). Bringing up the console always shows the echo's signifying failure, and upon checking the path it is empty.

Thanks again for your time!
#6
08/24/2013 (8:44 pm)
This might be a handy little tidbit for later on down the road if you are working with WIN 7 and maybe 8
www.garagegames.com/community/blogs/view/22334
#7
08/24/2013 (9:49 pm)
Try this:
%savePath = expandPath(getUserHomeDirectory() @ "/MyGameCompany/MyGame/Saves/SaveFile.txt");


%file = new FileObject();
if(%file.openForWrite(%savePath))
{
  %file.writeLine("Hello World!!");
  echo("File Written");
}
else
{
  error("File is not open for writing");
  echo(%savePath);
}

%file.close();
%file.delete();

Sorry, I had forgotten about expandPath() when I posted the previous suggestion.