Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 05-15-2008, 05:12 PM   #1
earman
Registered User
 
Join Date: May 2008
Posts: 2
Default sharedObject custom class problems with persistence

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!
ActionScript Code:
private function initApplication():void{     registerClassAlias("classes.Game", Game);             savedGame=SharedObject.getLocal("savedGameFile");         this.game=new Game();         this.gameRunning=true;     refreshGame(); } private function doInput():void {     if(input.text=="save"){         savedGame.clear();              savedGame.data.GA=this.game;         savedGame.flush();     }else if(input.text=="load"){                             this.game=savedGame.data.GA;                         }else{         this.game.update(input.text);     }     this.refreshGame();                 }
earman is offline   Reply With Quote
Old 05-15-2008, 08:40 PM   #2
earman
Registered User
 
Join Date: May 2008
Posts: 2
Default figured it out

this site helped me out a lot

ActionScript Code:
//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 savedGame                     this.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 functions import flash.utils.*;     public class Game implements IExternalizable { //private members private var world:World; private var currentDescription:String; private var 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 constructor public function Game(){             } //create a speperate initialization function to initialize instance members public function 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:) public function writeExternal(output:IDataOutput):void {              output.writeObject(this.world);              output.writeObject(this.currentDescription);              output.writeObject(this.currentObject); } public function 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

hope this helps anyone who wants to save a game or load a game
ActionScript Code:
registerClassAlias("classes.Game", Game);  registerClassAlias("classes.World", World);  registerClassAlias("classes.Action", Action); registerClassAlias("classes.Room", Room);                 registerClassAlias("classes.BaseObject",BaseObject);
earman is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Using the mx.util.Delegate class within your classes! madgett ActionScript 2.0 3 09-03-2007 12:11 AM
can't populate combo box in custom class cblaich Components 1 05-16-2007 08:37 AM
I've Got No Class.... just class problems. tg ActionScript 2.0 7 06-14-2005 08:31 PM
Database simulated with arrays on a cd-rom lecasn5 Components 61 09-07-2004 12:40 PM
card game: designing Deck class and good OOP? pigpen Gaming and Game Development 1 11-26-2002 12:41 PM


All times are GMT. The time now is 08:40 AM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.