-2

I'm coding a Java game and there are 26 levels so far. When the user completes a level, I want the space above the level to have a checkmark or something. However I don't know how to make it where when I close the game, the checkmark is still there from last time I played it and it doesn't reset the game from the beginning.

How can I store data for a game and save the values of variables so the game doesn't restart every time I close out of it?

Kara
  • 6,115
  • 16
  • 50
  • 57
Joey402
  • 3
  • 4

3 Answers3

1

Persisting data between program executions means saving it to a file or database. There are many options available for this depending on your environment. It sounds like you are running the game as a local java application - if this is the case, saving a file is probably your best option. When the program starts up, it would read the file to find where it left off previously. During execution, it would save the current state either in the background automatically or when the user explicitly selects to.

Ezra
  • 802
  • 4
  • 11
0

How can I store data for a game and save the values of variables

This means you need to write your class objects in a file like this

FileOutputStream fout = new FileOutputStream("c:\\game.ser"); // your file
ObjectOutputStream oos = new ObjectOutputStream(fout);
oos.writeObject(address);

For further details see this

Muhammad Suleman
  • 2,892
  • 2
  • 25
  • 33
0

An embedded database like SQLite is an easy way to store information. You could have columns(levelNumber INT, completed INT) where completed is either a 1 or 0.

When you complete a level, send an update to the database changing completed to 1 for the appropriate level number. Before each level is loaded, have a method that checks your database for the completed value:

This tutorial is fairly straight forward. There might be a slight learning curve if you're unfamiliar with SQL but its good to know how to use it.

Rocky
  • 132
  • 1
  • 2
  • 9