Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (757)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (844)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Saving user changed variables  (Read 1690 times)
0 Members and 1 Guest are viewing this topic.
Offline Shane75776
« Posted 2009-01-23 05:08:18 »

Alright i think im sounding really stupid about this but i have recently made a text based rpg and the entire game is based on variables so i was wondering how i could make a save game button or something that saves all the variables as they are and a load button.. so pretty much you would name your save file or somethin and you could then load it...

i have never done this before and i have been searching google for about 2 hours now and i come up with nothing.

anyways if anyone has any helpful code or tutorial links they would be greatly apreciated, and if you would need my game to look at it to be able to help me im on msn and would gladly give you the executable Jar file for it.

Im currently on messenger

Thanks for any help!!

Check out my Snipping Tool++ ! An advanced snippet/screenshot/text uploading tool! Meant to replace the windows snipping tool.

Check out Pixel Rain My most recent Swing based game!
Offline woogley
« Reply #1 - Posted 2009-01-23 06:00:56 »

One of the easier ways would be to use Properties. With this class, you can set variables using setProperty, retrieve them with getProperty, save them to a file with the 'store' method, and load them from a file with the 'load' method.

The advantage to this is that it is extremely easy to load/save variables, but the catch is, the file is very readable and thus extremely easy for the end-user to edit (if that is something you're concerned about)

So let's say you have a text file, variables.txt:
Quote = John Smith
player.age = 22
player.race = Elf = 100

And you have this code..
import java.util.*;
public class Test {
   public static void main(String args[]) {
      Properties vars = loadVars();
      System.out.println(vars.getProperty("")); // John Smith
      System.out.println(vars.getProperty("player.age")); // 22

      vars.setProperty("player.age","23"); // changes age to 23
      saveVars(vars); // saves changes to file
   public static Properties loadVars() {
      Properties p = new Properties();
      try {
         InputStream in = new FileInputStream("variables.txt");
      catch (IOException e) {
      return p;
   public static void saveVars(Properties vars) {
      try {
         OutputStream out = new FileOutputStream("variables.txt");,null);
      catch (IOException e) {

The first time you run that.. it will print "John Smith" with age 22. The second time you run it, it will display 23.

Other options would be to create your own file format, maybe a binary format, an INI file, or an XML file.

Another option is creating a Serializable 'Variables' class whose only purpose is to track variables and be able to be serialized to a file. This option is not much more advanced than using Properties, so it may be worth looking into.

Good luck
Offline cylab

JGO Kernel

Medals: 180

« Reply #2 - Posted 2009-01-23 10:16:27 »

Using Serializable would also allow you to store your variables in a typed manner, so you don't have to convert from/to strings like with the Properties approach. Google for java serialization tutorial to get more info.

Mathias - I Know What [you] Did Last Summer!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Shane75776
« Reply #3 - Posted 2009-01-23 20:26:45 »

thank you for the help!

Check out my Snipping Tool++ ! An advanced snippet/screenshot/text uploading tool! Meant to replace the windows snipping tool.

Check out Pixel Rain My most recent Swing based game!
Pages: [1]
  ignore  |  Print  

EgonOlsen (79 views)
2018-06-10 19:43:48

EgonOlsen (59 views)
2018-06-10 19:43:44

EgonOlsen (78 views)
2018-06-10 19:43:20

DesertCoockie (261 views)
2018-05-13 18:23:11

nelsongames (159 views)
2018-04-24 18:15:36

nelsongames (158 views)
2018-04-24 18:14:32

ivj94 (901 views)
2018-03-24 14:47:39

ivj94 (162 views)
2018-03-24 14:46:31

ivj94 (813 views)
2018-03-24 14:43:53

Solater (177 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!