Author Topic: Improved FlxSave for saving and retrieving objects  (Read 1868 times)

mol

  • Member
  • **
  • Posts: 33
  • Karma: +0/-0
    • View Profile
Hello flixel community

i noticed that FlxSave at loading files it cast them directly to Objet, losing its original class

that is fine if you save simple variables like health cash or points

but for a complete class this does not make the cut

so i just implemented a new method in FlxSave for this

you jut have to import

Code: [Select]
import flash.net.registerClassAlias;


and add this method

Code: [Select]
public function bind_custom_class(Name:String,classAlias:String,className:Class):Boolean
{
destroy();
name = Name;
try
{
registerClassAlias(classAlias, className);
_sharedObject = SharedObject.getLocal(name);
}
catch(e:Error)
{
trace(e)

FlxG.log("ERROR: There was a problem binding to\nthe shared object data from FlxSave.");
destroy();
return false;
}
data = _sharedObject.data;
return true;
}

wich is really the normal bind method with some slight modiffications, anyway to load a objet with its class definition you just have to do a code somewhat like this


Code: [Select]
//File save

var save:FlxSave = new FlxSave();
save.bind("file_i_alredy_saved");
save.data.imadethisvariablebefore = new ClassName();
save.flush()



//some code later
//...
//File Load

var load:FlxSave = new FlxSave();
load.bind_custom_class("file_i_alredy_saved","imadethisvariablebefore",ClassName);
var simplevar:ClassName = (load.data.imadethisvariablebefore as ClassName)



i hope this helps some of you! : D


Edit: i moddified some typos
« Last Edit: Tue, May 15, 2012 by mol »

paala

  • Contributor
  • ****
  • Posts: 250
  • Karma: +0/-1
    • View Profile
Re: Improved FlxSave for saving and retrieving objects
« Reply #1 on: Fri, May 4, 2012 »
Did I get it right?
With this you can save the state of a PlayState, and revert back to the state saved?

auriplane

  • Snails!!
  • Contributor
  • ****
  • Posts: 497
  • Karma: +1/-0
  • Snails!!
    • View Profile

initials

  • Contributor
  • ****
  • Posts: 378
  • Karma: +0/-0
  • Initials
    • View Profile
    • Initials Blog. Code and other things.
Re: Improved FlxSave for saving and retrieving objects
« Reply #3 on: Sat, May 5, 2012 »
This looks great. Ran into this problem a few times.

The thing I don't like about FlxSave is that you lose everything if you clear your browser cache.
Initials: Super Lemonade Factory, Super Lemonade Factory Part Two, Above The Clouds, Revvolvver, Four Chambers of the Human Heart