TGB Platformer Kit Save and Load System in 1 File
by Orion the Hunter · 11/07/2012 (10:33 am) · 6 comments
But without any further ado, let's implement this code. This script will save the file in to a script *.cs file using the function "writeFile()" and "readFile()". It is important that you do not tamper with the variables. All you need to do is follow these steps:
1. Make a file called "WriteLoadSave.cs" in the gameScripts folder.
2. In that, you need to paste the following code:
Now that wasn't so hard, was it? Well… there is more. You need to execute the files for this to work, but it's still one save code in one file because the executing part isn't save-related. :P
Add this to "games.cs" in the gameScripts folder in the function "startGame(%level);"
And… that's it! To use the function first start the game. Then write in the console "level1()" or "tutorial." Then put "writeFile()" and then "readFile." It should load the level, provided it's file name is "level1.t2d." Otherwise, you have to adapt the script likewise.
Enjoy data-loss-free gaming!
~AJRPCEO
(12/18/12)UPDATE 2:
I thought you might want to see my current save code to get an idea of what you can do with this thing:
2. Cleaned up my code a bit, added account functionality and it now saves more variables including the checkpoint.
1. Make a file called "WriteLoadSave.cs" in the gameScripts folder.
2. In that, you need to paste the following code:
function writeFile()
{
%file = new FileObject();
if(%file.openForWrite("~/data/saves/savedgame.cs"))
{
//Now for the actual saving!
%file.writeLine("$SaveDir = " @ $SaveDir);
%file.writeLine("sceneWindow2D.loadLevel( expandFilename($SaveDir) ); ");
good();
playSound( "LevelSaved", 1.5 );
echo("sucess!");
}
else
{
error("File is not open for saving!");
bad();
}
%file.close();
%file.delete();
}
//////////////////////////////////////////////////////////////////////////////////////////
function readFile()
{
%file = new FileObject();
good();
if(%file.openForRead("~/data/saves/savedgame.cs"))
{
%x=1;
while(!%file.isEof())
{
$playerlevel = %file.readLine();
exec ("~/data/saves/savedgame.cs");
%x++;
}
}
else
{
error("CANNOT OPEN FOR READ");
bad();
}
%file.close();
%file.delete();
}
//Save Directory: This will stell the variable "$SaveDir" what the level's names are. Just call one of the functions, then writelfile and readfile.
function tutorial()
{
$SaveDir = "\"game/data/levels/demoLevel.t2d\";";
}
function level1()
{
$SaveDir = "\"game/data/levels/Level1.t2d\";";
}
function level2()
{
$SaveDir = "\"game/data/levels/Level2.t2d\";";
}
function level3()
{
$SaveDir = "\"game/data/levels/Level3.t2d\";";
}
function level4()
{
$SaveDir = "\"game/data/levels/Level4.t2d\";";
}
function level5()
{
$SaveDir = "\"game/data/levels/Level5.t2d\";";
}
function level6()
{
$SaveDir = "\"game/data/levels/Level6.t2d\";";
}
//Add any other levels to the directory. They should correspond with the function name. Just copy one of the examples and name the file. Example: (Notice that the //'s disable the function. Do not copy those.)
//Be careful not to edit the "'s. It took a long time to place those!!!
//function level12()
//{
//$SaveDir = "\"game/data/levels/Level12.t2d\";";
//}Now that wasn't so hard, was it? Well… there is more. You need to execute the files for this to work, but it's still one save code in one file because the executing part isn't save-related. :P
Add this to "games.cs" in the gameScripts folder in the function "startGame(%level);"
exec ( "./WriteLoadSave.cs" );
And… that's it! To use the function first start the game. Then write in the console "level1()" or "tutorial." Then put "writeFile()" and then "readFile." It should load the level, provided it's file name is "level1.t2d." Otherwise, you have to adapt the script likewise.
Enjoy data-loss-free gaming!
~AJRPCEO
(12/18/12)UPDATE 2:
I thought you might want to see my current save code to get an idea of what you can do with this thing:
//Put this little save thing here...
TheoraVideo.setFile("game/data/video/menuvideo.ogg");
function writeFile()
{
%file = new FileObject();
if(%file.openForWrite("~/data/saves/savedgame" @ "-" @ $SaveExt @ ".cs"))
{
//Lets gather a few variables to save, shall we?
//Now for the actual saving!
echo($Pref::points);
echo($Game::Player.Position);
echo($Game::Player.Lives);
echo($Game::Player.Health);
echo($Game::Player.RespawnPosition);
%file.writeLine("$SaveDir = " @ $SaveDir);
%file.writeLine("sceneWindow2D.loadLevel( expandFilename($SaveDir) ); ");
%file.writeLine("$PlayerHealth = " @ $Game::Player.Health @ ";");
%file.writeLine("$PlayerLives = " @ $Game::Player.Lives @ ";");
%file.writeLine("$PlayerPosition = " @ "\"" @ $Game::Player.Position @ "\"" @ ";");
%file.writeLine("$PlayerScore = " @ $Pref::Points @ ";");
%file.writeLine("$PlayerRespawnPoint = " @ "\"" @ $Game::Player.RespawnPosition @ "\"" @ ";");
echo("Sucess! " @ %file);
good();
}
else
{
error("File is not open for saving!");
Canvas.PushDialog(AreYouSure2Gui);
$AYSText = "<just:center>The data you have attempted to save appears to be corrupt. If problem persists, try to reinstall the game. Sorry, bud. No, I will not offer any cheats. Press enter to dismiss the message.";
$AYSWindowText = "Save Error";
AYSWindowText.setText($AYSWindowText);
AYSText.setText($AYSText);
}
%file.close();
%file.delete();
}
//////////////////////////////////////////////////////////////////////////////////////////
function readFile()
{
canvas.popdialog(NowLoadingGui);
%file = new FileObject();
if(%file.openForRead("~/data/saves/savedgame" @ "-" @ $SaveExt @ ".cs"))
{
%x=1;
while(!%file.isEof())
{
$playerlevel = %file.readLine();
exec ("~/data/saves/savedgame" @ "-" @ $SaveExt @ ".cs");
%x++;
good();
}
}
else
{
error("CANNOT OPEN FOR READ");
bad();
schedule(1000, 0, "LoadErrorMessage");
}
//Make sure there is save data to read...
%file.close();
%file.delete();
}
//Save Directory: This will tell the variable "$SaveDir" what the level's names are. Just call one of the functions, then writelfile and readfile. Let's also not incorporate the music there, too.
function tutorial()
{
$SaveDir = "\"game/data/levels/demoLevel.t2d\";";
}
function level1()
{
$SaveDir = "\"game/data/levels/Level1.t2d\";";
}
function level2()
{
$SaveDir = "\"game/data/levels/Level2.t2d\";";
}
function level3()
{
$SaveDir = "\"game/data/levels/Level3.t2d\";";
}
function level4()
{
$SaveDir = "\"game/data/levels/Level4.t2d\";";
}
function level5()
{
$SaveDir = "\"game/data/levels/Level5.t2d\";";
}
function level6()
{
$SaveDir = "\"game/data/levels/Level6.t2d\";";
}
//Add any other levels to the directory. They should correspond with the function name. Just copy one of the examples and name the file. Example: (Notice that the //'s disable the function. Do not copy those.)
//Be careful not to edit the "'s. It took a long time to place those!!!
//function level12()
//{
//$SaveDir = "\"game/data/levels/Level12.t2d\";";
//}
function PlaceInPos()
{
$game::player.Position = $PlayerPosition;
$game::player.Health = $PlayerHealth;
$game::player.Lives = $PlayerLives;
$pref::points = $PlayerScore;
$game::player.updateHealthGui();
$game::player.updateExtraLivesGui();
$game::player.RespawnPosition = $PlayerRespawnPoint;
}
function LoadErrorMessage()
{
Canvas.PushDialog(AreYouSure2Gui);
$AYSWindowText = "Load Error";
$AYSText = "It appears that there is no data to read yet. Please make a save before attempting to load. I can\'t just materialize files out of thin air, you know. I put you on level 1. Press enter to dismiss this mesage.";
AYSWindowText.setText($AYSWindowText);
AYSText.setText($AYSText);
_newGame();
resetPoints();
}
//Now the files stuff
function file1()
{
$SaveExt = "File 1";
}
function file2()
{
$SaveExt = "File 2";
}
function file3()
{
$SaveExt = "File 3";
}
function file4()
{
$SaveExt = "File 4";
}
function file5()
{
$SaveExt = "File 5";
}
function checkisAliveWrite()
{
if (!$game::player.Alive)
{
canvas.pushDialog(AreYouSure2Gui);
$AYSWindowText = "Saving is a bad idea";
$AYSText = "Attempting to save while you are dead will result in all your progress up to the last save lost! (Press enter to dismiss)";
AYSWindowText.setText($AYSWindowText);
AYSText.setText($AYSText);
}
else
{
canvas.PushDialog(nowSavingGui);
}
}
function checkisAliveRead()
{
if (!$game::player.Alive)
{
canvas.pushDialog(AreYouSure2Gui);
$AYSWindowText = "Reading is a bad idea";
$AYSText = "Attempting to reading while you are dead will result in all your progress up to the last save lost! (Press enter to dismiss)";
AYSWindowText.setText($AYSWindowText);
AYSText.setText($AYSText);
}
else
{
Canvas.pushDialog(NowLoadingGui);
}
}2. Cleaned up my code a bit, added account functionality and it now saves more variables including the checkpoint.
About the author
#2
I'm pretty sure this will also work with any other TGB game because it uses the File I/O function. I just haven't tested it anywhere else. :o)
11/11/2012 (10:30 am)
Thanks for the feedback!I'm pretty sure this will also work with any other TGB game because it uses the File I/O function. I just haven't tested it anywhere else. :o)
#3
i think u will get t3d version too(may be in steve's resource).
not hard for a t3d coder to do that in t3d.
11/11/2012 (9:31 pm)
Mack,do a search.i think u will get t3d version too(may be in steve's resource).
not hard for a t3d coder to do that in t3d.
#4
11/12/2012 (10:33 am)
I've only ever seen a T3d resource. This one is just a very simple version for people who want a simple save code. ;)
#5
12/09/2012 (11:45 am)
UPDATED: 12/8/12
#6
12/18/2012 (10:32 am)
UPDATED: 12/18/12 
Torque Owner 000
-