Load progress when a save file is opened with my game
by Orion the Hunter · in Torque Game Builder · 01/28/2013 (12:21 pm) · 19 replies
Hello,
Usually my questions are about small things but this one is about my save file. I want to make it so that when an *.hdsave is opened with my game, the game will automatically execute that file and load its progress. I assume this has to do with source edits.
I've already made my game recognize that file so I've done the easy part, but I just need to know how to make it work properly. As it stands, my game just opens and acts like I opened it normally (BTW, this may be Mac specific).
Thanks!!!
Usually my questions are about small things but this one is about my save file. I want to make it so that when an *.hdsave is opened with my game, the game will automatically execute that file and load its progress. I assume this has to do with source edits.
I've already made my game recognize that file so I've done the easy part, but I just need to know how to make it work properly. As it stands, my game just opens and acts like I opened it normally (BTW, this may be Mac specific).
Thanks!!!
#2
01/28/2013 (2:56 pm)
Have you already implemented and have working the fundamental save/loading abilities or is that what you are stuck on? The save/loading abilities shouldn't need any source code edits; just knowledge on how to do it.
#3
@William — Checking it out...
01/28/2013 (4:01 pm)
@Alpha-Kand — I've already finished the save/load code so it saves the files in the hdsave format.@William — Checking it out...
#4
01/28/2013 (4:03 pm)
Oh, I see what you think the problem is, William. No, that's not the problem. My problem is I need to make it so that when I open the file it is executed by Torque. It isn't a problem with the file. In fact, it isn't a problem at all, I just need the game to know that when these files are opened, it should execute them, or preform a function for that matter.
#5
Have your code write legitimate lines of code in the file.
For example say your writing object is named $SAVEGAME:
All you have to do is "exec" the file and it will set variables and call functions.
01/28/2013 (6:17 pm)
If I'm picking up what you're laying down then the solution is fairly easy I have found from past experience.Have your code write legitimate lines of code in the file.
For example say your writing object is named $SAVEGAME:
$SAVEGAME.writeLine("$SCORE = "@%SCORE@";");
$SAVEGAME.writeLine(" call(foo,"@%parameter@"); ");All you have to do is "exec" the file and it will set variables and call functions.
#6
01/28/2013 (6:45 pm)
Thanks, but that's not what I needed help with. Ou know how you can use Microsoft Word to save a document?, then you can open it at a later time and it (obviously) has your whole document? That's what I want, but instead of words, progress.
#7
You want to take a mid game snapshot that you can continue from later right? For example if you saved your game when a platformer character had jumped and you loaded your game later the character would still be in the air as if you never left?
The only thing I can think of is writing the entire contents of your scenegraph and all necessary variables in your save file so you can put the game back together piece by piece giving the illusion you never left.
If this still isn't what you have in mind I give up. :)
01/28/2013 (8:49 pm)
Ohhhhhh!! I had almost written an entire post when it finally dawned on me what I think you are shooting for.You want to take a mid game snapshot that you can continue from later right? For example if you saved your game when a platformer character had jumped and you loaded your game later the character would still be in the air as if you never left?
The only thing I can think of is writing the entire contents of your scenegraph and all necessary variables in your save file so you can put the game back together piece by piece giving the illusion you never left.
If this still isn't what you have in mind I give up. :)
#8
01/29/2013 (12:27 am)
When you open a file, your operating system will see if there is an executable associated with it. If you associate "*.hdsave" files with your game, then it will run your executable with the name of the save file. This is why I say that you need to set up a file association.
#9
I've already made a save and load code. It works fine. I already made it so the game will save my progress in the HDsave format. When I compiled the executable, I made sure to associate *.hdsave with it. When I double click an HDsave file it opens the game. I want it to not only open the game, but to preform the "readfile" function as well.
Sorry if it wasn't making sense before. ;)
01/29/2013 (6:19 am)
^^ and ^Perhaps I'm being unclear? :DSorry if it wasn't making sense before. ;)
#10
And I see I read the title and not the text of the original post....
01/29/2013 (7:57 am)
Pull down the T3D MIT project and look in the template in all three MissionDownload.cs files (core/scripts/client, scripts/client and scripts/server). These scripts update a progress bar during file load progress. Then check out the core/art/gui/loadingGui.gui file that contains the progress bar. That should get you pointed in the right direction.And I see I read the title and not the text of the original post....
#11
01/29/2013 (10:54 am)
Yeah... that's a cool tip, but it's not exactly what I wanted. :)
#12
1) Double click on your save file (test.hdsave)
2) Operating system starts: yourGame.exe test.hdsave
You'll notice that the file is passed as a parameter to your game's executable.
T2D then passes the parameters into TorqueScript through global variables. The first is "$Game::argc" which is the count of of the number of arguments passed to your game. The second is "$Game::argv[0]", "$Game::argv[1]", etc. based upon how many parameters were passed in.
Your game *probably* has no other parameters being passed in, just the save file name. Therefore, if $Game::argv[0] isn't blank, your user double clicked on the save file.
Hope that helps!
01/29/2013 (12:09 pm)
Now that you have the file association, you'll need to update your code to use the file passed to the executable. I'm not 100% certain about this, but I believe that the game will be run like this...1) Double click on your save file (test.hdsave)
2) Operating system starts: yourGame.exe test.hdsave
You'll notice that the file is passed as a parameter to your game's executable.
T2D then passes the parameters into TorqueScript through global variables. The first is "$Game::argc" which is the count of of the number of arguments passed to your game. The second is "$Game::argv[0]", "$Game::argv[1]", etc. based upon how many parameters were passed in.
Your game *probably* has no other parameters being passed in, just the save file name. Therefore, if $Game::argv[0] isn't blank, your user double clicked on the save file.
if( $Game::argv[0] !$= "" )
{
// Call the load function with $Game::argv[0] as the filename.
}Hope that helps!
#13
01/29/2013 (12:28 pm)
So I'll just pop that in main.cs and let you know how it worked.
#14
01/29/2013 (12:31 pm)
Oh, wonderful. My game has decided this is the PERFECT moment to give me an error about a corrupt common directory. Gonna take a minute to clean this up.
#15
Any more help?
01/29/2013 (12:37 pm)
I echoed "$Game::argv[0]" and I got: "/Users/Torque/Desktop/PlatformerGame (Mac)/PlatformerGame/Contents/MacOS/TGBGame"Any more help?
#16
Hopefully, one of those will be the save file you're looking for.
01/29/2013 (12:47 pm)
Try echoing $Game::argv[%i] where %i is a count variable that matches the number of arguments, something like this:for (%i = 0; %i < $Game::argc; %i++)
{
echo(" --- argument " @ %i @ " : " @ $Game::argv[%i]);
}Though I am not sure if $Game::argc exists. If not, just pick an arbitrary number like 5 (shouldn't be that many arguments, really) - any that aren't used will probably just print garbage.Hopefully, one of those will be the save file you're looking for.
#17
Taking Richard's example, you can do this:
The "strstr" function will return "-1" if the search string (".hdsave") is in the current argument. Otherwise, it will return where in the string it is located.
Make sure to call "break;" after you load your game or else it may try to load multiple games. This code will only load the first save game passed to your game.
01/29/2013 (12:58 pm)
Oh yeah! The first argument is *always* your executable name. It's the second one that you'll want.Taking Richard's example, you can do this:
for( %i = 0; %i < $Game::argc; %i++ )
{
if( strstr( $Game::argv[%i], ".hdsave" ) >= 0 )
{
// Call your load function here...
break;
}
}The "strstr" function will return "-1" if the search string (".hdsave") is in the current argument. Otherwise, it will return where in the string it is located.
Make sure to call "break;" after you load your game or else it may try to load multiple games. This code will only load the first save game passed to your game.
#18
Wow. I think I hurt myself on that one....
01/29/2013 (1:24 pm)
I concur - after digging through about two dozen hit-and-miss articles (I read this in Programming Windows by Charles Petzold about 14 years ago and so only vaguely remember it) the second argument ($Game::argv[1]) should be the name of the file you double-clicked.Wow. I think I hurt myself on that one....
#19
And in the console I wrote "echo(documentfile());" and it gave me the save directory. Weird, weird, weird. :-/ Any more help?
01/29/2013 (5:02 pm)
Now, I still get the game executable. I even made a function: function documentfile()
{
for( %i = 0; %i < $Game::argc; %i++ )
{
if( strstr( $Game::argv[%i], ".hdsave" ) >= 0 )
{
echo("Code 304" @ $Game::argv[%i]);
// Call your load function here...
break;
}
}
}And in the console I wrote "echo(documentfile());" and it gave me the save directory. Weird, weird, weird. :-/ Any more help?
Associate William Lee Sims
Machine Code Games