0

I'm making a Flash game where I need a way to store a lot of data about the world, as in data that is constant all throughout the game, between all gameplays. Lots of values (integer, float, string), grouped in objects hierarchy. The point is to make it easy to edit during development (like an ordinary text file, or XML) and to have zero performance overhead while reading the data, and minimal overhead at launch time.

The question can be reduced to this: how to put ready-to-use ActionScript objects with all fields already filled within the SWF file (without a need to run init functions on them)?

The first thing that comes to mind is that MXML is the way, but I have no idea how should I use it to get the job done - not to mention the possible overhead (I've read MXML is slow). Is it even possible to do so without a need to parse things? If not, what is best alternative? XML? JSON? Custom format and parser functions?

Xirdus
  • 2,997
  • 6
  • 28
  • 36

1 Answers1

0

Make the class you want to save implement IExternalizable (and don´t forget to actually write the necessary functions ;) ). The use registerClassAlias to make the Class available for AMF-encoding.

Then write the class to a ByteArray (with ByteArray.writeObject()) and compress it. After that, write the ByteArray to a file.

When reading the file, read the content to a ByteArray, use decompress() and after that use "readObject() as YOURCLASS" to get back you instance.

You can leave out the compression if you like, but depending on the data stored it makes a HUGE difference. In a project I am currently working on, the uncompressed file takes about 6MB, the compressed version only 178KB.

codingbuddha
  • 707
  • 5
  • 16