Sign in to follow this  
General ONeil

Saving/Loading with Java

Recommended Posts

Hi, I have been programming for a little bit now, and I got a question from a friend's freind on how to make saving/loading games with Java. If anyone has code or anything that can help me with this i'd really appreciate it. Thanks in advanced.

Share this post


Link to post
Share on other sites
sorry no code here, just pointers...

I don't think there's anything automatic that'll work for any game, unless you use serialization...google out on serialization in java if you want to know
what I do for saving games is just outputting the important variables to a file, and reconstruct the state by opening the file

Share this post


Link to post
Share on other sites
It helps if you can make your scenario files the same format as your save files. (This applies mainly to strategy games.) I usually make my scenario files plain text, and just write out the game state to that format.

Share this post


Link to post
Share on other sites
Is this a standalone Java application or an applet? If it is an application you can just do what SlashOwnsU does, writing to files should be easy. If it is an applet, then you need to figure out where to save the data. You can try and put cookies on the clients' computer, but I'm not sure if that is possible with Java.

Share this post


Link to post
Share on other sites
Ok, thanks for the help, It is a standalone and i like the idea of outputting a file, but a question, well two questions are Can you make it so that you can get the game to load the state instead of having to locate the file and click on it? and Also is any of this on this site in a tutorial or something I can look at, or should I just do a search?

Share this post


Link to post
Share on other sites
if using java, i highly recommend using XERCES to build a XML Document (DOM) object. Then you can just output the XML DOM object as a String to a file. Using XML is a great way to save data. The drawbacks are: 1) big file size, 2) increased parsing time, and 3) hackers can easily modify the data. The positives are 1) easy to read, 2) easy to modify and change 3) can be changed in a way to support backwards compatability with older files (ex. adding new attributes or elements might not break the loading of older files). If your worried about hackers though, definitly don't go with XML, (or text). In that case, I would choose a binary form (object serilization might be the easist route to take in this case)

Share this post


Link to post
Share on other sites
I think it unlikely that hackers will be a concern; I do hope the OP isn't making an MMORPG, and for anything else, the user absolutely should be allowed to modify his game files.

About the game finding the state, it depends a bit on what kind of game you are doing. If you want to have several saved games available, then you're going to have to let the user choose between them no matter what you do. So in that case, you can certainly make a file-choosing dialog that automatically goes to the saved games location, and ignores any files not in the right format. What you can't do, though, is let the game choose for the user! On the other hand, if you only want one save game, then sure, you can simply hardcode the filename, and look up the same filename when the user clicks 'load game'.

Share this post


Link to post
Share on other sites
I don't know of any tutorials, but if you want to see how it should be done, I recommend you look into some of Paradox Interactive's games. Europa Universalis, Crusader Kings, Hearts of Iron 2 : All these have text scenario and save files, very easy to mod. I consider them the canonical example of how to do save games when hacking is not a concern, and ease of modding is. Also, they are really great games in their own right, and getting on a bit, so they're cheap.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this