Loading and Saving Game

I am doing a shooting game like a plane shooting aliens (like asteroids) for sch assignment. Very urgent coz deadline is Monday.

I don't know how to save and load the game such that the image of my plane's position, score, stages etc. I don't know what class to use for saving and loading.....

Thank you to those who read about my problem.

[361 byte] By [phozphatea] at [2007-9-29 14:06:53]
# 1
The regular java.io.* stuff will probably work just fine.
paulcwa at 2007-7-15 4:41:17 > top of Java-index,Other Topics,Java Game Development...
# 2

for a quick and dirty, not to mention bloated savegame file, just save your main game object to file.

i.e. use ObjectOutputStream.writeObject(mainGameObj) to save and ObjectInputStream.readObject(mainGameObj) to load.

one the object is loaded it is in exactly the same state as when you have saved it so little if any initalization is needed to continue the game.

klana001a at 2007-7-15 4:41:17 > top of Java-index,Other Topics,Java Game Development...
# 3

> for a quick and dirty, not to mention bloated savegame

> file, just save your main game object to file.

>

> i.e. use ObjectOutputStream.writeObject(mainGameObj)

> to save and ObjectInputStream.readObject(mainGameObj)

> to load.

>

> one the object is loaded it is in exactly the same

> state as when you have saved it so little if any

> initalization is needed to continue the game.

I would be suprised if that worked in anything beyond trivial examples.

We all know what Serialisation does when faced with linked lists, trees and [pretty much any] other type Collection ;)

Abusea at 2007-7-15 4:41:17 > top of Java-index,Other Topics,Java Game Development...
# 4
I don't - what does it do then?
Kevin-Porsa at 2007-7-15 4:41:17 > top of Java-index,Other Topics,Java Game Development...
# 5
I don't either? I am currently serializing my game objects, is there a better way?
Isuraa at 2007-7-15 4:41:17 > top of Java-index,Other Topics,Java Game Development...
# 6
The ins and outs of Serialization could make up an entire chapter of a book.Go read about it, then come back here if u need specific answers.
Abusea at 2007-7-15 4:41:17 > top of Java-index,Other Topics,Java Game Development...
# 7

If the game has levels you could simplify your saving by only saving between levels. with this you only need to save a small amount of info like highest level, player name and so on.

if you want to save at any point of the game you will need to save all your info which is a bit complicated if you want to save images and such (in that case i recomend only saving image paths as strings and not the images themselfs).

In an application i created i save Serializable objects in an xml format like this

//lets call your object game which includes all the objects that make

//up the game info

private void save(Game game,String fileName) throws IOException

{

//save the game in an ".xml" file

if(!fileName.endsWith(".xml"))

fileName=fileName+".xml";

FileOutputStream out=new FileOutputStream(fileName);

BufferedOutputStream outstream=new BufferedOutputStream(out);

XMLEncoder encoder=new XMLEncoder(outstream);

//write the data into the file

encoder.writeObject((Game)game);

encoder.close();

outstream.close();

out.close();

}

sushika at 2007-7-15 4:41:17 > top of Java-index,Other Topics,Java Game Development...