Saving serialized objects to disk with Apollo (simple example)

Well, I am still getting things in order after spending the weekend in SF for Apollo Camp. It was an awesome event with a pretty great turnout. I saw some familiar faces and met some new peeps as well. Nice presentation by all. The teknision crew really stood out to me. The effective UI crew was representing as well. I think everyone lost it when they broke out the WII remote. Nice work guys!

Moving along… I traded a few hours of sleep for some time to put together this really simple Apollo example. Kevin Hoyt gave an awesome presentation at Apollo Camp about working with reading and writing to the file system. One item he presented was the ability to write serialized data to disk. I checked his blog and also looked through the Apollo API docs looking for some sample code to review this concept and could not locate anything. Kevin should have his examples posted soon but in the meantime here is the example code that I put together tonight. Nothing too fancy here really. Just wanted to get this down in code for future reference.

import flash.filesystem.*;
import GameData;

private var _fileStream:FileStream;
private var _file:File;
private var _gameDataFileName:String;
private var _gameData:GameData;
private function init():void
{
_gameDataFileName = “savegame.dat”;
createExampleGameData();
createGameDataFile();
writeGameDataToFile();
readGameDataFromFile();
}

private function createExampleGameData():void
{
// Create the data to save
_gameData = new GameData();
_gameData.playerName = “Seanzie”;
_gameData.level = 2;
_gameData.score = 300;
}

private function createGameDataFile():void
{
// appResourceDirectory is the app’s installation dir
_file = File.appResourceDirectory.resolve(_gameDataFileName);
}

private function writeGameDataToFile():void
{
_fileStream = new FileStream();
_fileStream.open(_file, FileMode.WRITE);
_fileStream.writeObject(_gameData);
_fileStream.close();
}

private function readGameDataFromFile():void
{
_fileStream.open(_file, FileMode.READ);
var _gameDataFromFile:Object = _fileStream.readObject();
trace(”_gameDataFromFile.playerName=”+_gameDataFromFile.playerName);
trace(”_gameDataFromFile.level=”+_gameDataFromFile.level);
trace(”_gameDataFromFile.score=”+_gameDataFromFile.score);
}

Here are the source files.

GameData.as
ValueObjects.mxml
ValueObjects-app.xml

4 Responses to “Saving serialized objects to disk with Apollo (simple example)”

  1. [...]  Saving serialized objects to disk with Apollo [...]

  2. [...] Sean Moore: Saving serialized objects to disk with Apollo (simple example) [...]

  3. Exactly what I needed when I needed it!

    I’d say I love you, but then… I hardly know you, don’t I?

  4. [...]  Saving serialized objects to disk with Apollo [...]

Leave a Reply