Save/Load High Scores

Started by
18 comments, last by Kk1496 8 years, 8 months ago

So it's choking on the deserialize line. Try this: delete the .dat file and see if a newly created one plays nicer with the current code.

Hazard Pay :: FPS/RTS in SharpDX (gathering dust, retained for... historical purposes)
DeviantArt :: Because right-brain needs love too (also pretty neglected these days)

Advertisement

Thnx. :) Now it works! So I'm guessing the dat file was expecting a single int so when I changed the code to an int array, they weren't compatible anymore?

Exactly. The dat file had previously serialized out an older version of the struct that held a single integer instead of an array. As the dat file was external to the program context, nothing updated its internal structure when you changed the code itself. Deserialize was reading this old data type and choking when your explicit cast said "now populate this array field with this integer".

Hazard Pay :: FPS/RTS in SharpDX (gathering dust, retained for... historical purposes)
DeviantArt :: Because right-brain needs love too (also pretty neglected these days)

Where is the best place to compare high scores. Right now, I have a timer that checks that the time hasn't run out, and i compare the current score to the saved high scores when the timer runs out. However, it runs every frame, so it checks correctly the first frame, but then when it runs again, it changes the next value to the current score. I can't think of another place to run this comparison function. Any Suggestions?

Why can't it just run once? It's something you only need to do once at the end of a game session. Certainly no need for including it in every frame.

I'd lean towards sticking it in a static utility function just for ease of editing later. Then in whichever method handles "what happens when a game is over" you just call over to the score-checking method and shuffle around high score order if necessary.

There's no "one best way" (arguably) to do it, but there are certainly easier-to-maintain ways.

First, just make it work. Then, if you want and have the time/need, make it work better :)

Hazard Pay :: FPS/RTS in SharpDX (gathering dust, retained for... historical purposes)
DeviantArt :: Because right-brain needs love too (also pretty neglected these days)

My problem is that I'm trying to figure out HOW to make it run once. I already have it in a separate function and I call it where the rest of my and game logic goes. Unfortunately, that's in Updat because I have to check that a timer ran out.

I've thought of stopping the timescale and do the end game logic in fixedUpfate, but I think I'll have the same problem.

You might want to look at implementing different game states to the overall game loop, so that you don't have to fit the logic for every possible state of the game into one monolithic Update() method. That's not a brief topic though, (and I suggest doing some googling on "managing multiple game states" even here in the forums).

As a rough explanation, when your game is playing, some variable somewhere should know that the game is in regular "play mode". That's a state, and update should only do stuff that's pertinent for "play mode" updates. Things like decrementing the timer and checking for "is time up yet", for example.

When the timer reaches zero, state should switch to "endgame mode", and then update calls should have a completely separate set of logic that makes more sense for the game during the post-play time period. Things like "was the high score good enough to put on the board" and "did the player press 'restart' ", for example.

However, in the interest of a quick solution for now, it sounds like "I already have it in a separate function and I call it where the rest of my [end] game logic goes" should result in only calling the score-comparison once. What does that code look like?

Hazard Pay :: FPS/RTS in SharpDX (gathering dust, retained for... historical purposes)
DeviantArt :: Because right-brain needs love too (also pretty neglected these days)

Heres the Update that calls the function. I was trying to fix the problem, thats why stuff is commented out.

void Update ()
{
if (gameTimer > 0)
{
gameTimer -= Time.deltaTime;
minutes = (int)gameTimer / 60;
seconds = (int)gameTimer % 60;
timer.text = "Time: " + minutes + ":" + seconds;

}
if (gameTimer <= 0)
{
//Debug.Log ("Game Over");
inGame.SetActive (false);
playArea.SetActive (false);
gameOver.SetActive (true);
finalScore.text = "Score: " + GameManager.score;
Manager.currentScore = GameManager.score;
//Manager.CalculateHighScores()
}
}

Here is the function itself.

public static void CalculateHighScores ()

{
Debug.Log ("Calculating High Scores");
bool isGreater = false;
int i = 0;
//Debug.Log ("Recieved" + newScore);
do {
if (Manager.currentScore > Manager.highScores.Length)
{
Debug.Log("Curent Score: " + Manager.currentScore + ">" + Manager.highScores[i]);
isGreater = true;
Manager.highScores[i+1] = Manager.highScores[i];
Manager.highScores[i] = Manager.currentScore;
Debug.Log("break;");
}
else{
i++;
}
}while(!isGreater);
Debug.Log ("Done");
}

When should we to save our game data?

I Mean if we should save data before quit Game,or just save to file when game data changed?

When should we to save our game data?

I Mean if we should save data before quit Game,or just save to file when game data changed?

That depends on how your game works. My game was a small mobile game so I could save every time the player game ended. However, if you had an RPG type game you may want to set points in the game world at which your player could use a save option.

This topic is closed to new replies.

Advertisement