A question for Java programmers

Subscribe to A question for Java programmers 11 posts

avatar for Ampkit123 Ampkit123 77 posts
Flag Post

How do I make Java store variables and load them? Kinda like a save game?
BTW, the game I am making is a 100% text game.
Thanks in advance. :)

 
avatar for ooflorent ooflorent 1 post
Flag Post

You could use an SQLite embedded database!
If you want something simpler/safer, write your own file-based save system.

 
avatar for Ampkit123 Ampkit123 77 posts
Flag Post

Um.. but I’m just a beginner o.O But yeah, I was trying to find a save system.

 
avatar for stage_phrite stage_phrite 42 posts
Flag Post

to be honest ooflorents idea of sql databases is the best you are going to get…but if you insist on not following that root then you could also use javas file io classes, some basic tutorials here……

http://docs.oracle.com/javase/tutorial/essential/io/

 
avatar for Ampkit123 Ampkit123 77 posts
Flag Post

Ow. I don’t really get it, guess I’ll just not make the save system :/ Guess I’ll have some revision to do….

 
avatar for strumpetdreams strumpetdreams 12 posts
Flag Post

Serialization would probably be easiest. It’s surprisingly not difficult.
http://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html

 
avatar for Ampkit123 Ampkit123 77 posts
Flag Post

Thanks! But it would be a bit too hard to put the code into the game, since it means I would have to change a lot of things. Any way to put my database(with variables inside) into a textfile and load it later?

 
avatar for strumpetdreams strumpetdreams 12 posts
Flag Post

Save to file and you yourself decide on a way to interpret the data upon reading the file.

e.g.:
Scanner s = new Scanner(new File(“savedata.txt”));
int numRobots = s.nextInt();

 
avatar for silkenshadow silkenshadow 2 posts
Flag Post

If you end up using databases it may be a good idea to use something like Hibernate" to manage your persistence. If you’re going to allow the user to input data that will then get added to the database it’s very important to sanitize the data first as sql injection can be a major issue. Here is an article that gives a good short overview of prevention in Java SQL injection prevention

 
avatar for Ampkit123 Ampkit123 77 posts
Flag Post

I guess injections could be pretty dangerous o.O But I don’t think most people will care to do that to a simple useless text game o.O

Ty strumpetdreams and silkenshadow. One very last question.. where would be the file be stored?

 
avatar for strumpetdreams strumpetdreams 12 posts
Flag Post

You get to specify, but they are usually relative from the root directory of the executable (i.e.: the same directory as the .class being run, the .jar, or where the java command was called from). I believe there is a way to stash it into a user’s home directory in a clever way, depending on the OS. That would require some googling.

Though I strongly recommend picking up skills like hibernate or serialization anyway, eventually. _