I'm trying to figure a good way to handle saving game state to file (common save/load game function in games). ObjectOutput/InputStream seems most promising so far.
However,the object I'm saving contains lots of other objects which in turn contain other objects. All objects implement Serializable (I've given them all serialVersionUID = 1, not sure whether it matters).
It seems to write the object properly but loading it is a bit of a problem.
This is the function I'm using:
- private Game readObject(String n)throws IOException, ClassNotFoundException{
-
ObjectInputStream ois = null;
-
Game ga = null;
-
//Construct the ObjectInputStream object
-
ois = new ObjectInputStream(new FileInputStream("res/"+n+".gla"));/
-
-
Object obj = ois.readObject(); //this should somehow be made to read the entire file?
-
if (obj instanceof Game) { //obviosly this is not enough. Returns always null.
-
-
ga = (Game)obj;
-
}
-
if (ois != null) {
-
ois.close();
-
}
-
return ga;
-
}
Object's class variables:
- private final static long serialVersionUID = 1;
-
private Tavern tavern;
-
private Blacksmith blacksmith;
-
private Spellshop spellshop;
-
private Vector<Team> teams;
-
private Team activeTeam;
-
private Gladiator currentGladiator = null;
-
private Season season;
-
private Battle battle;
-
private int currentseason;
-
private int humanplayers = 0;
Any help or insight upon the matter would be greatly appreciated.