Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (481)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (548)
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 1114 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 Sluigi_91@hotmail.com

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
player.name = John Smith
player.age = 22
player.race = Elf
player.health = 100

And you have this code..
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
import java.io.*;
import java.util.*;
public class Test {
   public static void main(String args[]) {
      Properties vars = loadVars();
      System.out.println(vars.getProperty("player.name")); // 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");
         p.load(in);
         in.close();
      }
      catch (IOException e) {
         e.printStackTrace();
      }
      return p;
   }
   public static void saveVars(Properties vars) {
      try {
         OutputStream out = new FileOutputStream("variables.txt");
         vars.store(out,null);
         out.close();
      }
      catch (IOException e) {
         e.printStackTrace();
      }
   }
}


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 Ninja


Medals: 43



« 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  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

atombrot (26 views)
2014-08-19 09:29:53

Tekkerue (24 views)
2014-08-16 06:45:27

Tekkerue (23 views)
2014-08-16 06:22:17

Tekkerue (14 views)
2014-08-16 06:20:21

Tekkerue (21 views)
2014-08-16 06:12:11

Rayexar (60 views)
2014-08-11 02:49:23

BurntPizza (38 views)
2014-08-09 21:09:32

BurntPizza (30 views)
2014-08-08 02:01:56

Norakomi (37 views)
2014-08-06 19:49:38

BurntPizza (67 views)
2014-08-03 02:57:17
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org 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‑gaming.org
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!