File storage anamoly?
by Michael Cozzolino · in iTorque 2D · 03/08/2010 (4:40 am) · 11 replies
I'm reading a file %ScoresFile.openForRead("~/data/levels/scores.txt");
That reads the intended file I echo out line by line.
Then I write to file and what seems to happen is the file above contents get copied then modified to sort out the high score order as desired but then saves out to the directory below.
3/8/10 7:27:22 AM [0x0-0x21021].com.garagegames.torqueGameBuilder[211] creating path /Users/mike/Library/Application Support/Independent/UntitledGame/game/data/levels/scores.txt
The original scores.txt file never gets written to.
If I run again then the /Users/mike/Library/Application Support/Independent/UntitledGame/game/data/levels/scores.txt path gets read from.
Is this the correct behavior and this is just how things work when using the simulator and/or the run game button and not running on the device?
That reads the intended file I echo out line by line.
Then I write to file and what seems to happen is the file above contents get copied then modified to sort out the high score order as desired but then saves out to the directory below.
3/8/10 7:27:22 AM [0x0-0x21021].com.garagegames.torqueGameBuilder[211] creating path /Users/mike/Library/Application Support/Independent/UntitledGame/game/data/levels/scores.txt
The original scores.txt file never gets written to.
If I run again then the /Users/mike/Library/Application Support/Independent/UntitledGame/game/data/levels/scores.txt path gets read from.
Is this the correct behavior and this is just how things work when using the simulator and/or the run game button and not running on the device?
About the author
Indie Developer in the Albany NY area. iOS, PC, Mac OSX development. http://itunes.apple.com/us/artist/michael-cozzolino/id367780489
#2
Thanks
03/08/2010 (10:23 am)
I don't really need to have an original scores.txt as long as I can access a scores.txt somewhere and update it. This directory path that Torque makes, is that "simulating" the a app document directory or is that something that I must do to set up a way to access the app document directory? I tend to think that once I get my developer account and can play my game on the device that I will find some things out real quickly good and bad.Thanks
#3
03/08/2010 (12:31 pm)
Use that function to get the directory, and only read and write scores from that. Then everything should be fine.
#4
void copyHighscoresIfNeeded()
Still get self is out of scope though as well as the function getting called as being out of scope in _iPhoneRunTorqueMain.
I seems strange to me that the Torque FileObject read an write capabilities can't just read and write to the device document directory by default.
03/15/2010 (3:30 am)
Guessing (void)copyHighscoresIfNeeded should bevoid copyHighscoresIfNeeded()
Still get self is out of scope though as well as the function getting called as being out of scope in _iPhoneRunTorqueMain.
I seems strange to me that the Torque FileObject read an write capabilities can't just read and write to the device document directory by default.
#5
(There should also be a reference in the header file to accompany that messy little function)
There are still some desktop-isms left in the engine, and the FileObject is one of those. Perhaps a DocumentObject to accompany that would be an idea (since there are all sorts of reasons to read files from the app bundle). We could use an errata/wishlist section like this to report bugs/enhancement requests and see their status.
03/15/2010 (11:16 am)
There should actually be a "-" in front of the function (really method) name. Bad cut & paste job with distractions ;)(There should also be a reference in the header file to accompany that messy little function)
There are still some desktop-isms left in the engine, and the FileObject is one of those. Perhaps a DocumentObject to accompany that would be an idea (since there are all sorts of reasons to read files from the app bundle). We could use an errata/wishlist section like this to report bugs/enhancement requests and see their status.
#6
I'm off to work now. If you don't have the time then I guess I will have to figure something out.
Thanks.
03/15/2010 (2:47 pm)
Hate to ask but could you walk me through setting up the header file? I'm new to Objective C and not the most experienced C/C++ guy either. I will gladly create the resource for others if I can get this working. It would be nice to fill in some gaps for people to be able to just have things working so they can focus on game play. I'm off to work now. If you don't have the time then I guess I will have to figure something out.
Thanks.
#7
(All code off the top of my head, untested :)
1.Make a script function which opens the highscore list for reading, filling in a list you display. Use dummy data if there is nothing to read. This should set a global variable with the contents. How to get the name:
2.Make another function which stores a score and name in the right position, pushing out scores.
3.Make a third function which saves the list from the global var.
Now you're probably wondering the neatest way to represent a highscore list in memory :)
You could use a multi-dimensional array or a SimSet. Arrays use numbers, SimSets are a bit more involved, but I think they look neater.
Example creation of a dummy list:
Get entry 1 with:
I can't find any proper insert-after type of operation, but you could use a combination of $highscores.bringToFront(%entry) and $highscores.pushToBack(%entry). I'm at work all day today (pizza+boardgames), so I can't hack up anything stable and tested just yet. See what you can come up with on your own till I have time for something more useful :)
03/16/2010 (2:56 am)
Simplest possible solution. The procedure is something like this:(All code off the top of my head, untested :)
1.Make a script function which opens the highscore list for reading, filling in a list you display. Use dummy data if there is nothing to read. This should set a global variable with the contents. How to get the name:
%name = getUserDataDirectory() @ "/highscores.txt";
2.Make another function which stores a score and name in the right position, pushing out scores.
3.Make a third function which saves the list from the global var.
Now you're probably wondering the neatest way to represent a highscore list in memory :)
You could use a multi-dimensional array or a SimSet. Arrays use numbers, SimSets are a bit more involved, but I think they look neater.
Example creation of a dummy list:
$highscores = new SimSet(Highscores);
for(%i = 0; %i < 10; %i++)
{
%entry = new ScriptObject();
%entry.name = "Nobody";
%entry.score = 10000 - (%i * 1000);
$highscores.add(%entry);
}Get entry 1 with:
%entry = $highscores.getObject(1);
I can't find any proper insert-after type of operation, but you could use a combination of $highscores.bringToFront(%entry) and $highscores.pushToBack(%entry). I'm at work all day today (pizza+boardgames), so I can't hack up anything stable and tested just yet. See what you can come up with on your own till I have time for something more useful :)
#8
I can't get my head around the Objective C code you posted. I can't get it to compile. This is where I'm stuck where you mentioned
I could probably use the loadGameDataFromDevice saveGameDataToDevice methods to do this but I will have to figure out how to parse out the string. Doesn't save to the iPhone documents directory though which kind of turns me off. It will have to wait til the weekend probably though.
Just been working a lot and I don't have the time to sit and bang on things I once had.
03/16/2010 (12:07 pm)
I think I'm set on the script side. If I can get access to a file I'm good. My process is get the file read line by line and put into an array, get players score and compare to existing scores, and sort then write back to file.I can't get my head around the Objective C code you posted. I can't get it to compile. This is where I'm stuck where you mentioned
Quote:(There should also be a reference in the header file to accompany that messy little function)
I could probably use the loadGameDataFromDevice saveGameDataToDevice methods to do this but I will have to figure out how to parse out the string. Doesn't save to the iPhone documents directory though which kind of turns me off. It will have to wait til the weekend probably though.
Just been working a lot and I don't have the time to sit and bang on things I once had.
#9
Also see this thread:
www.torquepowered.com/community/forums/viewthread/112693
03/17/2010 (12:47 pm)
Yeah, I said to ignore the ObjC and do a scripting approach instead :)Also see this thread:
www.torquepowered.com/community/forums/viewthread/112693
#10
That makes complete sense to me :) Yay!
I just worked 18 hours and have been up for more than 20 hours so knowing that I can move forward, once I get some rest makes me happy.
I really appreciate your help and everyone on here. Seems the Torque for iPhone owners had to do a lot of bootstrapping in the early days especially compared to the other Torque products. Glad I waited or probably would have been really frustrated.
Mikd
03/17/2010 (2:21 pm)
Must have missed that :)That makes complete sense to me :) Yay!
I just worked 18 hours and have been up for more than 20 hours so knowing that I can move forward, once I get some rest makes me happy.
I really appreciate your help and everyone on here. Seems the Torque for iPhone owners had to do a lot of bootstrapping in the early days especially compared to the other Torque products. Glad I waited or probably would have been really frustrated.
Mikd
#11
The changes from earlier to 1.3+ may look minor, but make a huge difference. I know the Luma guys are a fan of using a platform's features, so there will probably be more iPhone-specific functions and ways to do things in the next release.
03/20/2010 (11:47 am)
I expect v1.4 to be wonderful :)The changes from earlier to 1.3+ may look minor, but make a huge difference. I know the Luma guys are a fan of using a platform's features, so there will probably be more iPhone-specific functions and ways to do things in the next release.
Torque 3D Owner Ronny Bangsund
Torque Cheerleaders
If you want to do some engine hacking, here's something you can use on the first run to check if a pre-populated highscore list exists, and copy yours to the correct place if not:
// Objective C (void)copyHighscoresIfNeeded { NSFileManager *fileManager = [NSFileManager defaultManager]; NSError *error; NSString *path = [[self appDocsPath] stringByAppendingPathComponent:@"scores.txt"]; BOOL success = [fileManager fileExistsAtPath:path]; if(!success) { NSString *defaultPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"game/data/levels/scores.txt"]; success = [fileManager copyItemAtPath:defaultPath toPath:path error:&error]; } }Stick this somewhere in the iPhoneMain.mm file and call it at the start of _iPhoneRunTorqueMain, then you can just load scores.txt from your document directory in script. Use getUserDataDirectory() and add the filename to that path when loading. NOTE: Somewhat untested. Hopefully I got all the paths right :)
(I use ObjC because I use code like this in several non-Torque projects and I just couldn't find a neat way to check for existing files in TorqueScript :)