Question about recording and loading saves
by Jereis Zaatri · in Torque Game Builder · 08/24/2010 (1:04 am) · 9 replies
Hello,
Is there a general form or practice to loading and saving games? I noticed there's support for xml. Is there a detailed tutorial for such things? I saw a few tutorial that gave a basic overview to reading in xml files, but is there one that goes in great detail such as having sub-categories like [save slot > levels > items unlocked].
Also what's the general practice if a save file gets corrupted and what does TGB (or iTGB in my case) spit out when such a case happens?
Is there a general form or practice to loading and saving games? I noticed there's support for xml. Is there a detailed tutorial for such things? I saw a few tutorial that gave a basic overview to reading in xml files, but is there one that goes in great detail such as having sub-categories like [save slot > levels > items unlocked].
Also what's the general practice if a save file gets corrupted and what does TGB (or iTGB in my case) spit out when such a case happens?
#2
08/24/2010 (2:37 am)
That method looks interesting, but I don't see what exactly is going on. For instance I don't what happens when you [ exec( "state.obj" ); ]. Does it automatically know what objects to fill or does it create an object that I then use to fill in the blanks with?
#3
First, this code assumes that you can build your game from any state. For example, when your main player gets a certain weapon, you could do this: "GameState.stateVariable1 = true;". When you start your game, you either call "createNewGameState()", or "loadGameState()". From there, when you build your character, if GameState.stateVariable1 is true, then you create your player with that weapon.
When you save your game (by calling "GameState.saveState();"), a file called "state.obj" will be created and will look like
By exec'ing that file, you create the GameState object.
So your load/save flow could look like this:
1) Create New Game or Load Game
2) Build the game from the GameState variables
3) Play!
Of course, this is greatly simplified, but hopefully you get the idea.
08/24/2010 (7:13 pm)
Code copied for reference:function createNewGameState()
{
new ScriptObject(GameState)
{
stateVariable1 = 0;
stateVariable2 = 0;
};
}
function GameState::saveState( %this )
{
new FileObject( SaveFile );
SaveFile.openForWrite( "state.obj" );
SaveFile.writeObject( GameState );
SaveFile.close();
SaveFile.delete();
}
function loadGameState()
{
exec( "state.obj" );
}First, this code assumes that you can build your game from any state. For example, when your main player gets a certain weapon, you could do this: "GameState.stateVariable1 = true;". When you start your game, you either call "createNewGameState()", or "loadGameState()". From there, when you build your character, if GameState.stateVariable1 is true, then you create your player with that weapon.
When you save your game (by calling "GameState.saveState();"), a file called "state.obj" will be created and will look like
new ScriptObject(GameState) {
canSaveDynamicFields = "1";
stateVariable1 = "1";
stateVariable2 = "0";
};In other words, it creates some TorqueScript.By exec'ing that file, you create the GameState object.
So your load/save flow could look like this:
1) Create New Game or Load Game
2) Build the game from the GameState variables
3) Play!
Of course, this is greatly simplified, but hopefully you get the idea.
#4
08/24/2010 (7:30 pm)
So just to make sure I understand. When I exec, I'll get a object with the name in the "()" which in the given example is GameState.
#5
08/25/2010 (3:34 pm)
Yep!
#6
UPDATE:
It seems to write the file, but only temporarily in memory as I'm able to read what I just wrote, but not from a prior run and it doesn't show up in the file system. Is there a setting I need to modify?
08/28/2010 (9:25 pm)
The save method doesn't seem to work. Where does the file get written to? I'm not seeing it in the project. Does iTGB do it differently? UPDATE:
It seems to write the file, but only temporarily in memory as I'm able to read what I just wrote, but not from a prior run and it doesn't show up in the file system. Is there a setting I need to modify?
#7
08/29/2010 (6:10 am)
I have no clue what iTGB does. The files on a PC are written to %APPDATA%/<YourCompany>/<YourGame>. Also, files don't get written to memory, so you're very likely just looking at the already created game object.
#8
The documentation I have is misleading. According to the documentation, it saves in the folder of the executable.
08/29/2010 (6:29 am)
Thanks, it is in applications data.The documentation I have is misleading. According to the documentation, it saves in the folder of the executable.
#9
Looks like I'm still having trouble. So how do I exec that saved file when it's clearly not in the same folder as all my other script files? Is there a way I can get the full path, because expandFilename() doesn't do the job as shown below.
expandFilename("./data/saves/gSettings.cs");
GOT:
game/scripts/data/saves/gSettings.cs
NEED:
C:\Users\###\AppData\Roaming\###\###\game\data\saves\gSettings.cs
12/29/2010 (9:17 am)
Hello.... Looks like I'm still having trouble. So how do I exec that saved file when it's clearly not in the same folder as all my other script files? Is there a way I can get the full path, because expandFilename() doesn't do the job as shown below.
expandFilename("./data/saves/gSettings.cs");
GOT:
game/scripts/data/saves/gSettings.cs
NEED:
C:\Users\###\AppData\Roaming\###\###\game\data\saves\gSettings.cs
Associate William Lee Sims
Machine Code Games