i have a text based game that gets pretty complex. I am trying to make a save game feature. I have a custom class Game which is pretty complex and holds all the game logic and state info. It creates object instances of other custom classes World, Room, Action, BaseObject.

The following code is how i am saving the game to the shared object and loading it back. It does not throw any errors(which took me a long time to figure out), but when i save, close the app, and then reopen, and try to load, it does not seem to have saved any of the state information about the game object.

its as if when i load, it successfully gets the game object from the savedGame:SharedObject but it is a new instance of a game object, not a persistent version from the last save. Is my game object too complex. It would be great if i could just save the whole game object in the Shared object and recoup it later.

Also, if i save and then load in the same app session it doesn't throw me back to the beginning of the game. i don't get it!

//on save clear and flush the shared object
savedGame.clear();
savedGame.flush();
//make a byte array to copy the object- not sure if this is necessary//just thought i'ld make sure to make a deep copy var copier:ByteArray = new ByteArray();
copier.position=0;
copier.writeObject(this.game);
//set byte array position to the beginning so it reads the object properlycopier.position=0;//save the copy of the game object into the shared object data//pretty sure you need registerClassAlias("classes.Game", Game); so that the //object keeps type info.
savedGame.data.GA=copier.readObject();
savedGame.flush();
//on load set the game object to the object stored in savedGamethis.game=savedGame.data.GA;

what really made it work was IExternalizable
to make any custom class serializable- like if you want to story it in a byte array or sharedobject and keep all its state info- you implement IExternalizable like this

ActionScript Code:

//import flash.utils.* to use iexternalizable and its functionsimport flash.utils.*;
publicclass Game implements IExternalizable
{//private membersprivatevar world:World;
privatevar currentDescription:String;
privatevar currentObject:String;
//for serialization you need a constructor with no parameters, also if you //don't want your deserialized object to reinitialize everything don't put //anything at all in the constructorpublicfunction Game(){}//create a speperate initialization function to initialize instance memberspublicfunction init():void{//initialize variables here..this.world=new World();
this.world.init("args here");
//and so on..}// the point of IExternalizable is to make sure your stuff is serialized correctly//so you need to manually write and read all private data members//these to functions are necessary when implementing IExternalizable//also, make sure you write and read in the exact same order//all functions will be serialized and any public members will to, but if they are//initialized before the init statement, that is the value they will have when //they are deserialized. So make sure you initialize stuff in your init function //so it doesn't get screwed up:)publicfunction writeExternal(output:IDataOutput):void{
output.writeObject(this.world);
output.writeObject(this.currentDescription);
output.writeObject(this.currentObject);
}publicfunction readExternal(input:IDataInput):void{this.world=input.readObject();
this.currentDescription=input.readObject()this.currentObject=input.readObject();
}}//end of class

make sure all you referenced classes like World are serializable too!!! this is very important. I had to go and refactor all my classes, it was a pain but now i can save the game and have it load up exactly where it was saved.

also put this stuff right before your sharedObject.getLocol("ex") thing