Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

How do I make it so that in a multiple-level game the score will stay where it was from the previous level?

share|improve this question
add comment

1 Answer

Store the score outside of the level, probably in the game object that manages which level you are on:

class Game {
  int score;
  Level * currentLevel;
  ...
};

If there is code within a given level object that might need to manipulate the score, you can pass that information to the level when it is constructed. You could simply pass a pointer to the score integer (the simplest, most straightforward option) or you could encapsulate the score in its own interface:

struct Score {
  int GetCurrentPoints () const;
  void AddPoints (int points);
  ...
};

struct Level {
  Level (Score * globalScore)
  : score(globalScore) {
  }

private:
  Score * score;
};

class Game {
  Score score;
  Level * currentLevel;
  ...
};

Pass the a pointer to the game's instance of the single global score to each level, which they store and manipulate, and it will persist across levels as you'd like.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.