Hello,
I would like to save the state of the game to reload it later. I have found some approaches, but I don’t know which is best. Of course I can’t save the state of the JVM, so I have to save the state of all objects. How could this be done?
i’ll give you a quick example, then you should be able to apply it to the rest of your game
suppose your whole game is a square that move with arrow keys,
your start at x = 0 , y = 0 ;
during the game, you moved a little bit to the right,
so let’s say
x = 150, y=0
now you want to quit and save your game,
you just have to write the x value in a text or a binary file
then when you re-open the game, you check if the text file contain x or y value, and you apply it to your current x and y
so in the end, saving and loading, are just 2 methods, the one write your values in a file, and the other read values from the file and apply each one to the correct object
good luck
PS :
am not sure you’re posting in the correct section
Thanks. You are right, that’s not the right section, but I understood “design” in terms of software architecture and not graphical appearance.
I know which data to save, but I don’t know how to persist this data. I could write it to a XML file, text file or some other file, but I don’t know which way is the best. Maybe I should use java.io.Serializable?
As far as I know Serializable is the best choice for save/load - I did some tests and it works perfectly in any case.
Good. Then I know what to focus on. Should I collect the data from all classes that contain information that should be saved or should I copy all relevant data into some kind of game state class?
you’re not gonna be writing into your file from the start to the end of the game and i think you can find the proper way by trying and testing what’s works best for you :
XML works great, but I don’t know how I could store it securely on a remote server.
With my limited experience, I would do it like this:
On the server side I would limit the access to the XML file only to specific IP addresses and on the client side I would encrypt the XML file before it is uploaded to the server. However, I don’t know if that’s the best way to get it done.
Do you need to encrypt it at all? Correct me if I’m wrong, but essentially it is a game save? Providing there isn’t any sensitive information contained in it (passwords, contact information, credit cards etc.) I wouldn’t stress about encryption, at least from client to server.
Or are you worried about people getting in and modifying it while its on the server? In that case yes, you should certainly limit who & what can access your data/databases, and it should be standard practice. Otherwise you will find all kinds of nasty things happening when you aren’t looking, such as your data disappearing completely. But once again I wouldn’t panic about encryption, as people should be stopped from accessing it well before encryption comes in to play.
If however you do need to encrypt it for whatever reason, there are a few things to keep in mind. (This is a topic and field of study of its own, of which I know bugger all, but others on here might). If people have access to your client-side application, there is nothing stoping them from decompling it, wading through obfuscation if present, and nutting out your encryption method, and ultimately decoding your file. If the encryption is weak, it will just slow them down a bit. As such you should consider encrypting it again on the server prior to storage, using a different key or method (with the expense of extra performance overhead). You should also be thinking towards using SSL for transmitting the data if it is sensitive, to prevent interception.
But before you do anything like that, consider if 1) Would people would want to intercept & decode the file in the first place, and 2) what are the consequences if they did?
nerb.
I have a few questions about how you want to save. (loading is fine).
- Save the whole game like the current map and the positions of each and every object?
- Just save the current map number? (Most games do this)
If you are going to save the whole map state, then serialization is the best option. But I strongly recommend the second one. Just save the map number to a file and read it when you want to load your game.
As a side note, if I was playing a game and am I just going to lose some health, I save the game and closes it. I prefer to play from the first of the level again but if I was playing a game in which the saving is implemented in serialization, when I reload the game, I’ll again fall and lose health.
I like the latter option and it’s much easier too.