Score isn't saving
by Matthew Medina · in Torque Game Builder · 01/09/2007 (5:04 pm) · 2 replies
Hi, I'm trying to get it so that my score saves itself at the beginning of each level, so if you quit, then later when you continue your game, your score is the same. I know there have been tutorials and other threads about this, but I must be doing something wrong, because it is not working for me. This is the tutorial I used to do the basic score: tdn.garagegames.com/wiki/TGB/MiniTutorials/GUIScoreAndTime
I have it so that it writes the score to a file, like this:
Here's is the problem I'm currently having. When the game first starts, before you save or anything, you already have a score, if you get killed I have it subtract an amount of it, if you do something good I have it add to it, and to add some variety the score goes down the longer you play. I have some of the scripts that add or subtract the score inside the files of the objects that do the adding or subtracting, not the score file, if that matters.
This is how I have it changing the score:
Then when I quit, and check to see if it wrote the score, it just has the original score that you start the game with, it didn't add any of the other scores or anything.
I've been trying to figure this out for a few days now, and haven't had any luck. Can anyone please give me a few pointers on how I can get it to write the real score, that is displayed on the GUI, so that next time I load the game, the score is there?
Thanks
I have it so that it writes the score to a file, like this:
$ScoreboardGuiScore = 600;
ScoreboardGuiScore.setText($ScoreboardGuiScore);function t2dSceneGraph::saveScore()
{
%file = new FileObject();
%file.openForWrite("Game/data/files/score.cs");
%file.writeLine("$ScoreboardGuiScore = " @ $ScoreboardGuiScore @ ";");
%file.close();
%file.delete();
}Here's is the problem I'm currently having. When the game first starts, before you save or anything, you already have a score, if you get killed I have it subtract an amount of it, if you do something good I have it add to it, and to add some variety the score goes down the longer you play. I have some of the scripts that add or subtract the score inside the files of the objects that do the adding or subtracting, not the score file, if that matters.
This is how I have it changing the score:
function enemy::onCollision( %srcObj, %dstObj )
{
if(%dstObj.class $= "player")
{
ScoreboardGuiScore.setValue(ScoreboardGuiScore.getValue() - 100);
ScoreboardGuilives.setValue(ScoreboardGuilives.getValue() - 1);
%srcObj.safeDelete();
%dstObj.respawn();
}
}function t2dSceneGraph::updateScoreboardScore(%this)
{
ScoreboardGuiScore.setValue(ScoreboardGuiScore.getValue() - 2);
%this.scoreSchedule = %this.schedule(1000, updateScoreboardScore);
}Then when I quit, and check to see if it wrote the score, it just has the original score that you start the game with, it didn't add any of the other scores or anything.
I've been trying to figure this out for a few days now, and haven't had any luck. Can anyone please give me a few pointers on how I can get it to write the real score, that is displayed on the GUI, so that next time I load the game, the score is there?
Thanks
About the author
#2
Here is what I did:
Instead of using this tutorial tdn.garagegames.com/wiki/TGB/MiniTutorials/GUIScoreAndTime, I followed the first half of this one tdn.garagegames.com/wiki/TGB/ScriptTutorials/ScoreTutorial
Then I set my script like this,
In my menu .cs file, I have it set the score to its default number when you press "New Game", in this case, 600:
Then in my score .cs file:
That's making it so that it writes the score to a file when the level first starts, so if you progress to the next level, it will save the score when the new level starts, as you can also see, it deducts 2 points from the score every second.
When you quit the game and look in the file it wrote to, you should see that it has whatever the score was when you started that level written down. Like this:
I then have it set so when you hit "Continue Game" on the main menu, it reads that file, and sets that score to what was written down:
All the other scripts still work also, so points are still deducted every second, but instead of starting at the default score, it starts at your saved score. If you press "New Game" from the menu, it resets the score that was written down back to the default score (600), and then you can start again.
Hope this helps if anyone ever needs to do this.
01/10/2007 (4:38 pm)
No, it wasn't that, because it was writing the default score, just not the current score when you left the game. Thanks anyway though, I figured out how to get it to work.Here is what I did:
Instead of using this tutorial tdn.garagegames.com/wiki/TGB/MiniTutorials/GUIScoreAndTime, I followed the first half of this one tdn.garagegames.com/wiki/TGB/ScriptTutorials/ScoreTutorial
Then I set my script like this,
In my menu .cs file, I have it set the score to its default number when you press "New Game", in this case, 600:
$score = 600; score.setText($score);
Then in my score .cs file:
function t2dSceneGraph::onLevelLoaded(%this)
{
%this.scoreSchedule = %this.schedule(500, saveScore);
%this.scoreSchedule = %this.schedule(1000, updateScore);
}
function t2dSceneGraph::updateScore(%this)
{
$score = $score - 2;
score.setText($score);
%this.scoreSchedule = %this.schedule(1000, updateScore);
}
function t2dSceneGraph::saveScore()
{
%file = new FileObject();
%file.openForWrite("Game/data/files/score.cs");
%file.writeLine("$Score = " @ $Score @ ";");
%file.close();
%file.delete();
}That's making it so that it writes the score to a file when the level first starts, so if you progress to the next level, it will save the score when the new level starts, as you can also see, it deducts 2 points from the score every second.
When you quit the game and look in the file it wrote to, you should see that it has whatever the score was when you started that level written down. Like this:
$Score = 3050;
I then have it set so when you hit "Continue Game" on the main menu, it reads that file, and sets that score to what was written down:
exec("Game/data/files/Score.cs");All the other scripts still work also, so points are still deducted every second, but instead of starting at the default score, it starts at your saved score. If you press "New Game" from the menu, it resets the score that was written down back to the default score (600), and then you can start again.
Hope this helps if anyone ever needs to do this.
Torque Owner Dustin Sims
If not if may have something to do with this line in your code...
%file.openForWrite("Game/data/files/score.cs");I have not researched it but there were some other threads and I remember typically usingsomething like "~/data/files/score.cs" as the path (~ instead of Game)
Maybe that has something to do with it if your file is not being written to at all...