Problem reading text files
by Nicolai Dutka · in Torque Game Builder · 05/30/2008 (3:12 pm) · 20 replies
I am working on a high score list and have some VERY strange behavior going on. I have created a text file with a list of names and numbers. I saved the text file next to my game.cs file. Here is my code for opening it for reading:
if(%file.openforRead( "./scores.kct" ) )
Now, this is where it gets strange.... The VERY first time I run the program, it works. I can manipulate the document through script, check numbers, and update the file. For instance, I get a score higher than someone in that text file, it updates it with my score. I shut off the game, turn it back on, and my NEW score IS there. So, it seems to be working right?
Ok then, let's reset the values. Close the game, open my text file.... what? The values are still the original!! Ok.... if the game is showing my NEW high scores when I turn it on, why are my OLD scores still in the text file?
Whatever, let's work on that later... So I go to school the next day and begin showing the project in class. I get to the end at the high score and... WTF?? The numbers are all different!! They are NOT showing OLD scores OR the NEW scores!! They are showing a completely different, 3rd set of scores! It's not like some trash file either, the names and numbers are all legit!
Ok, so I go home to begin working on it and... WTF AGAIN?? The numbers are back to the NEW scores (the original new scores, not the "new new" scores. AKA the 2nd set, not the 3rd) Make sense, or are you as lost as I am?? :(
So why do both computers display different scores, neither of which match what is in the text file I am telling the program to read?
PS: I tried a 3rd machine which never ran the game before. The first time through, it uses the text file as a read-in. Every time after that, it seems to be pulling these numbers from somewhere else! I've done a Windows Search for my text file on all 3 machines and am ONLY finding the original one I made!
Where the heck is Torque saving this info to and where is it reading it from???
if(%file.openforRead( "./scores.kct" ) )
Now, this is where it gets strange.... The VERY first time I run the program, it works. I can manipulate the document through script, check numbers, and update the file. For instance, I get a score higher than someone in that text file, it updates it with my score. I shut off the game, turn it back on, and my NEW score IS there. So, it seems to be working right?
Ok then, let's reset the values. Close the game, open my text file.... what? The values are still the original!! Ok.... if the game is showing my NEW high scores when I turn it on, why are my OLD scores still in the text file?
Whatever, let's work on that later... So I go to school the next day and begin showing the project in class. I get to the end at the high score and... WTF?? The numbers are all different!! They are NOT showing OLD scores OR the NEW scores!! They are showing a completely different, 3rd set of scores! It's not like some trash file either, the names and numbers are all legit!
Ok, so I go home to begin working on it and... WTF AGAIN?? The numbers are back to the NEW scores (the original new scores, not the "new new" scores. AKA the 2nd set, not the 3rd) Make sense, or are you as lost as I am?? :(
So why do both computers display different scores, neither of which match what is in the text file I am telling the program to read?
PS: I tried a 3rd machine which never ran the game before. The first time through, it uses the text file as a read-in. Every time after that, it seems to be pulling these numbers from somewhere else! I've done a Windows Search for my text file on all 3 machines and am ONLY finding the original one I made!
Where the heck is Torque saving this info to and where is it reading it from???
#2
Can't I make Torque write to the file I TOLD IT TO in the first place? I think that's a little... um... retarded. Yes, that's a good word for it. This has to be the most assanine thing I have come across in Torque yet... I can't write to my own files, it writes to a clone in a folder I can't find named AppData?? Wtf is that?
05/30/2008 (3:28 pm)
If I delete the one inside the game folder, new users will have a broken high score list. Then, if those users copy their games to a different machine, they have to start all over.Can't I make Torque write to the file I TOLD IT TO in the first place? I think that's a little... um... retarded. Yes, that's a good word for it. This has to be the most assanine thing I have come across in Torque yet... I can't write to my own files, it writes to a clone in a folder I can't find named AppData?? Wtf is that?
#3
My code reads in a file found inside the game folder and writes to that very same file. I just don't understand how it could be reading/writing that file the very first time you run the game, but be reading a DIFFERENT file EVERY time you play it thereafter. What's even more confusing is why I CANNOT find ANY other files on my entire hard drive that would have ANYTHING else to do with this.
Seriously, does it have to be THIS difficult to write a simple text file?
05/30/2008 (3:34 pm)
I just found and searched through my entire AppData folder. I found some Torque Game Builder folders and dug through both, but I am not seeing ANYTHING that could be storing my high score list.My code reads in a file found inside the game folder and writes to that very same file. I just don't understand how it could be reading/writing that file the very first time you run the game, but be reading a DIFFERENT file EVERY time you play it thereafter. What's even more confusing is why I CANNOT find ANY other files on my entire hard drive that would have ANYTHING else to do with this.
Seriously, does it have to be THIS difficult to write a simple text file?
#4
game/gameScripts/game.cs
I have my high score list (scores.kct) here (this is a simple *.txt file that I renamed to my own *.kct extension):
game/gameScripts/scores.kct
ALL of the following code is found in my games.cs in the location noted above!
Here is my code for my high scores:
You'll notice I read in the file 2 times. The first time, I start at line one and skip every other line (reading in JUST the player names). The 2nd time, I skip every other one, THEN starting reading lines (reading in JUST the player scores).
I then do some checks to see if the current players made the high score list, if so collect their info, add to the in-game list, then write to the file to be used later (next time they play whether they shut off the game or not):
Now, I am no brain surgeon, but it seems to me if I say "save a file here", I really do mean HERE, not THERE.... Am I wrong?
05/30/2008 (3:41 pm)
I have a game.cs file here:game/gameScripts/game.cs
I have my high score list (scores.kct) here (this is a simple *.txt file that I renamed to my own *.kct extension):
game/gameScripts/scores.kct
ALL of the following code is found in my games.cs in the location noted above!
Here is my code for my high scores:
//make high score list
%file = new fileObject();
if(%file.openforRead( "./scores.kct" ) )
{
while( !%file.isEOF() )
{
%newText = %file.readLine();
%name = new GuiMLTextCtrl() {
canSaveDynamicFields = "0";
Profile = "GuiMLTextProfile";
HorizSizing = "relative";
VertSizing = "relative";
position = "0 0";
Extent = "300 20";
MinExtent = "8 2";
canSave = "1";
Visible = "1";
hovertime = "1000";
lineSpacing = "2";
allowColorChars = "0";
maxChars = "-1";
text = "";
};
nameList.add(%name);
%name.setText("<font:arial:18><just:left>"@%newText);
%y = namelist.getCount() * 20;
%name.setPosition(0,%y);
%name.name=%newText;
%skipLine = %file.readLine();
}
}
else
{
%file.delete();
return false;
}
%file.close();
%file.delete();
%file = new fileObject();
if(%file.openforRead( "./scores.kct" ) )
{
while( !%file.isEOF() )
{
%skipLine = %file.readLine();
%newText = %file.readLine();
%score = new GuiMLTextCtrl() {
canSaveDynamicFields = "0";
Profile = "GuiMLTextProfile";
HorizSizing = "relative";
VertSizing = "relative";
position = "0 0";
Extent = "300 20";
MinExtent = "8 2";
canSave = "1";
Visible = "1";
hovertime = "1000";
lineSpacing = "2";
allowColorChars = "0";
maxChars = "-1";
text = "";
};
scoreList.add(%score);
%score.setText("<font:arial:18><just:right>"@%newText);
%y = scoreList.getCount() * 20;
%score.setPosition(0,%y);
%score.score=%newText;
}
}
else
{
%file.delete();
return false;
}
%file.close();
%file.delete();You'll notice I read in the file 2 times. The first time, I start at line one and skip every other line (reading in JUST the player names). The 2nd time, I skip every other one, THEN starting reading lines (reading in JUST the player scores).
I then do some checks to see if the current players made the high score list, if so collect their info, add to the in-game list, then write to the file to be used later (next time they play whether they shut off the game or not):
//save High Score List
%file = new fileObject();
if( !%file.openforWrite( "./scores.kct" ) )
{
echo("cannot save high score list");
%file.delete();
return false;
}
else
{
echo("Saving high score list.");
%loop=0;
while(%loop<10)
{
%obj = nameList.getObject(%loop);
%file.writeLine(%obj.name);
%obj = scoreList.getObject(%loop);
%file.writeLine(%obj.score);
%loop++;
}
%file.close();
%file.delete();
}Now, I am no brain surgeon, but it seems to me if I say "save a file here", I really do mean HERE, not THERE.... Am I wrong?
#5
05/30/2008 (3:46 pm)
I also want to point out the MAJOR problem this creates in a networked game. I have NO CLUE why this is happening, but in a networked game where player 2 has CLEARLY made the high score list, the window to congratulate that player and get their info NEVER shows up. At this point, I don't know if player 2's score is being compared to the right values or any values at all because I am lost as to what file it could possibly be trying to open as its "scores.kct" file. I mean, I know what file I told it to read, but clearly the whole read/write thing seems JACKED at this point...
#6
05/30/2008 (3:58 pm)
At the VERY VERY least, I need to get all my scores in the high score list reset to zeros before shipping the game. I can't even do that because I have no clue where the information is being stored. I had Windows search for "scores.kct" and the ONLY one it found is in my game folder. I went ahead and deleted it like Philip said in his first post. The game is still reading in the scores! WHERE FROM!!
#7
It is a Windows standard. Torque is just obeying the rules.
Inside your AppData (Vista) or Application Data (XP) folder, you should see one there with your name on it, or maybe "Independent". Inside that folder should contain your game. Insde that there should be your gamescripts/scores.kct file. If it is not there, then you are looking in the wrong place.
Once you deleted the scores.kct file inside your game directory, are the files being saved and loaded correctly (no matter where you think they should be saved)?
05/30/2008 (4:05 pm)
Quote:Can't I make Torque write to the file I TOLD IT TO in the first place? I think that's a little... um... retarded
Quote:Now, I am no brain surgeon, but it seems to me if I say "save a file here", I really do mean HERE, not THERE.... Am I wrong?
It is a Windows standard. Torque is just obeying the rules.
Inside your AppData (Vista) or Application Data (XP) folder, you should see one there with your name on it, or maybe "Independent". Inside that folder should contain your game. Insde that there should be your gamescripts/scores.kct file. If it is not there, then you are looking in the wrong place.
Once you deleted the scores.kct file inside your game directory, are the files being saved and loaded correctly (no matter where you think they should be saved)?
#8
Because the files are stored inside the AppData folder they will not be exported when you build your game. I am pretty confident that you don't have anything to worry about, provided you don't include the contents of your AppData folder with your shipped game ;)
05/30/2008 (4:08 pm)
Nicolai,Because the files are stored inside the AppData folder they will not be exported when you build your game. I am pretty confident that you don't have anything to worry about, provided you don't include the contents of your AppData folder with your shipped game ;)
#9
I opened my AppData and deleted every folder with the name Torque or Garage Games on it. There was NOTHING with my game info there at all.
After deleting every single copy of scores.kct, the high score list is still working correctly, I just cannot reset it at all. I also want to be able to transfer the high score list to a different machine by simply copying the whole game folder.
This is BS. In TGE I did NOT have these problems. I was always able to use text files directly in my game folder and it NEVER pulled crap like this...
05/30/2008 (4:11 pm)
I have Windows Vista Ultimate 64. If I push the "search" button that came with Vista and type in "scores.kct" and have it search EVERYTHING, it only finds the one in my game folder!I opened my AppData and deleted every folder with the name Torque or Garage Games on it. There was NOTHING with my game info there at all.
After deleting every single copy of scores.kct, the high score list is still working correctly, I just cannot reset it at all. I also want to be able to transfer the high score list to a different machine by simply copying the whole game folder.
This is BS. In TGE I did NOT have these problems. I was always able to use text files directly in my game folder and it NEVER pulled crap like this...
#10
All 3 systems use a text file or set of text files which are saved to a folder INSIDE the game folder. In my first 2 projects, these files could then be read in later and I could OVERWRITE them directly in the game folder!
05/30/2008 (4:16 pm)
This is my 3rd time utilizing a save/load system. The first 2 were using TGE 1.5.2 and they both work FLAWLESSLY. They both read from files directly in the game folder and save directly to those files. No BS about cloned files or AppData! I can provide working examples to back these claims!All 3 systems use a text file or set of text files which are saved to a folder INSIDE the game folder. In my first 2 projects, these files could then be read in later and I could OVERWRITE them directly in the game folder!
#11
I realize the numbers WILL be reset on other peoples computers when they first get it, but that's exactly it... what if I dont want that?? What if I WANT the high scores to be transferable? I can't because I cannot find my list!
05/30/2008 (4:20 pm)
Regardless of how it all works, I CANNOT find the files this game is using for my high score list. I did so I am no brain surgeon, but even an idiot knows how to use the Windows search button and I am telling you, there is NO scores.kct file that exists on any hard drive on my computer at ALL but the damn high score list is still working! I want to reset the values to zeros!I realize the numbers WILL be reset on other peoples computers when they first get it, but that's exactly it... what if I dont want that?? What if I WANT the high scores to be transferable? I can't because I cannot find my list!
#12
Try searching the AppData folder itself. Also try searching hidden files and folders, as well as non-indexed locations.
The scores file IS inside the AppData. Don't look for GarageGames or TorqueGameBuilder, look for either your name, your company name or "Independent".
I don't think you read my previous post though, these files will not be sent with your game and therefore you shouldn't even need to worry about them.
05/30/2008 (4:20 pm)
Quote:I opened my AppData and deleted every folder with the name Torque or Garage Games on it. There was NOTHING with my game info there at all.
Try searching the AppData folder itself. Also try searching hidden files and folders, as well as non-indexed locations.
The scores file IS inside the AppData. Don't look for GarageGames or TorqueGameBuilder, look for either your name, your company name or "Independent".
I don't think you read my previous post though, these files will not be sent with your game and therefore you shouldn't even need to worry about them.
#13
So... each user of Windows is going to have their own high score list now? The file is being stored in each individual user's own Application Data folder, so ya, my guess is they'd have their own high score list... How does that make any sense at all? I install the game on one machine, but my wife has her own high score list and I have my own... ya... that's just great... so what is the point of a high score list if we can't compete? LAME!!
Isn't there a better way?
1. I need ALL users to have the same high score list
2. I need to be able to transfer MY high score list to my other computer so I don't have to start over. (I need the average user to be able to do this too). So putting files buried 4 folders deep inside an AppData folder is NOT ok with me.
What happened to the good old days of saving files to your My Documents folder? Why can I do EXACTLY what I am describing using TGE but not TGB? I swear to you, my files save directly to the game folder using TGE and I have NEVER once before had to do this kind of searching and work to find my files. I find this to be just completely ridiculous... A man should never have to leave his game folder to find his text files! BLASPHEMY!
(I really do appreciate all your help and patience! I must sound like a jerk... I am not, just very irritated and my personal life is NOT helping...)
05/30/2008 (4:35 pm)
Ok, I did the "include non-indexed" etc and did finally find the file. SHEESH!!So... each user of Windows is going to have their own high score list now? The file is being stored in each individual user's own Application Data folder, so ya, my guess is they'd have their own high score list... How does that make any sense at all? I install the game on one machine, but my wife has her own high score list and I have my own... ya... that's just great... so what is the point of a high score list if we can't compete? LAME!!
Isn't there a better way?
1. I need ALL users to have the same high score list
2. I need to be able to transfer MY high score list to my other computer so I don't have to start over. (I need the average user to be able to do this too). So putting files buried 4 folders deep inside an AppData folder is NOT ok with me.
What happened to the good old days of saving files to your My Documents folder? Why can I do EXACTLY what I am describing using TGE but not TGB? I swear to you, my files save directly to the game folder using TGE and I have NEVER once before had to do this kind of searching and work to find my files. I find this to be just completely ridiculous... A man should never have to leave his game folder to find his text files! BLASPHEMY!
(I really do appreciate all your help and patience! I must sound like a jerk... I am not, just very irritated and my personal life is NOT helping...)
#14
I need consistency! How are you gonna tell me I can't save text files into MY OWN game folder???
05/30/2008 (4:42 pm)
What REALLY REALLY bugs me the most is that I said "SAVE HERE" and the game said "OK, I'll save it THERE".... I didn't SAY THERE, I said HERE dammit... am I speaking Chinese? Seriously, come on!I need consistency! How are you gonna tell me I can't save text files into MY OWN game folder???
#15
How do I do that please?
05/30/2008 (4:44 pm)
Let's set ALL things aside and look at the single most important aspect of this whole thing: I want to be able to save text files to my game folder, NOT to a copy of my game folder somewhere else.How do I do that please?
#16
tdn.garagegames.com/wiki/TGB/FileIO
I don't know this for sure, but I believe to write to the project directory you will have to do so in C++.
05/30/2008 (5:11 pm)
Here is a nice TDN doc describing file save/loading changes that occurred with TGB 1.6.0 tdn.garagegames.com/wiki/TGB/FileIO
I don't know this for sure, but I believe to write to the project directory you will have to do so in C++.
#17
05/30/2008 (5:12 pm)
Do you have access to TGB's source? Otherwise, you cannot.
#18
05/30/2008 (5:17 pm)
I certainly do have source access. I just don't understand why something like this can't be included or at least optional. It seems so fundamental...
#19
I suggest that you find a look at the function "expandScriptFilename". I haven't done it myself, but I think that is where you should start.
05/30/2008 (5:24 pm)
Nicolai, as I said before, Torque is just obeying the rules.I suggest that you find a look at the function "expandScriptFilename". I haven't done it myself, but I think that is where you should start.
#20
The rational behind the FileIO changes are detailed in the document on TDN referenced above.
If you want to bypass that then there's an easy way to do so without all the angry fuss. ;) *kidding*
You can either recompile without the "TORQUE_PLAYER" pre-processor definition and with the the "TORQUE_TOOLS" one in which case you essentially get the TGBGame executable compiled with all the capabilities of the Game editor rather than the distributable "player". I really don't recommend this at all.
A much less severe method is to make an isolated and minor modification to the "FileObject::openForWrite()" method to change this:
Then you're restricted to writing to the game folder and beyond with:
In summary, change the "TORQUE_PLAYER" to "false" to revert to older functionality.
Hope this helps,
Melv.
06/01/2008 (6:12 am)
Nicolai,The rational behind the FileIO changes are detailed in the document on TDN referenced above.
If you want to bypass that then there's an easy way to do so without all the angry fuss. ;) *kidding*
You can either recompile without the "TORQUE_PLAYER" pre-processor definition and with the the "TORQUE_TOOLS" one in which case you essentially get the TGBGame executable compiled with all the capabilities of the Game editor rather than the distributable "player". I really don't recommend this at all.
A much less severe method is to make an isolated and minor modification to the "FileObject::openForWrite()" method to change this:
#ifdef TORQUE_PLAYER...to this...
#ifdef false
Then you're restricted to writing to the game folder and beyond with:
%myFile = new FileObject();
echo( "Write File:" SPC %myFile.getId() );
%myFile.openForWrite("~/myFile.txt");
%myFile.writeLine( %myFile.getId() );
%myFile.close();
%myFile.delete();
%myFile = new FileObject();
%myFile.openForRead("~/myFile.txt");
%text = %myFile.readLine();
echo( "Read file:" SPC %text );
%myFile.close();
%myFile.delete();... which should appear in your game folder. You'd probably want to write to "~/data/scores" or something I presume.In summary, change the "TORQUE_PLAYER" to "false" to revert to older functionality.
Hope this helps,
Melv.
Associate Phillip O'Shea
Violent Tulip
Whenever this happens, Torque assumes that you want to use the one inside the game directory. When you write to a file, it sends it to the AppData directory.
What I suggest you do is delete the one you have created inside the game folder. This way there is no confusion for Torque.