FacebookTwitterGoogle+Share

SharedObject example with haXe+nme for flash and android

It crossed my mind that occasionally storing data is useful. Sometimes it’s not, but that’s really no reason to avoid it.

And so began my journey to discover a way to save data at least in Flash, and on my Android. As far as journeys go, it was a short one.

I tried a few methods but only managed getting one to work (I didn’t try super hard, but that’s not to say that if I had, I would have been successful).

They are called SharedObjects, which you may remember from when we used Actionscript instead of haXe, and they are located at nme.net.SharedObject.

The pertinent excerpt of my test code is as follows:

var so = SharedObject.getLocal( "storage-test" );
// Load the values
// Data.message is null the first time
trace('data loaded: ' + so.data.message );
var count:Int = Std.parseInt( so.data.count );
// Set the values
so.data.message = 'oh hello! [' + count + ']';
so.data.count = count + 1 ;
// Prepare to save.. with some checks
#if ( cpp || neko )
	// Android didn't wanted SharedObjectFlushStatus not to be a String
	var flushStatus:SharedObjectFlushStatus = null;
#else
	// Flash wanted it very much to be a String
	var flushStatus:String = null;
#end
try {
	flushStatus = so.flush() ;	// Save the object
} catch ( e:Dynamic ) {
	trace('couldn't write...');
}
if ( flushStatus != null ) {
	switch( flushStatus ) {
		case SharedObjectFlushStatus.PENDING:
			trace('requesting permission to save');
		case SharedObjectFlushStatus.FLUSHED:
			trace('value saved');
	}
}

Pretty easy, right?! You set properties on your instance of the SharedObject’s data field, and that object gets serialized and saved when you call flush().

Perhaps you’re wondering, what’s up with this #if ( cpp || neko ) stuff? I know I would be, in your shoes.
It turns out, as far as I can tell, that the SharedObjectFlushStatus is some sort of (I don’t want to say magical, but I wouldn’t argue if you claimed at least some manner of sorcery was involved) fake enum and resolves to different types according to your compile-target. This may be inaccurate, but that’s what I took from the fairly clear and unbelievably short code in it’s source file.

I figured, if they can use macros for conditional compilation, well why can’t I? Long story still long, and so I did.

Now I can save data and you can too, for which I take full and well-deserved credit!

Your friend,
Brad

 

Comments

  1. Hi Brad,
    Have you tested it on iOS? or have you tried to modify the saved data on Android?
    I just tried doing the same on iOS and got data saved once, but couldn’t update it on further executions of the code.
    Thanks,
    Emiliano

  2. Brad says:

    Hi,
    Sorry, I don’t have access to an iOS device. 🙁

    Modifying saved data worked on my android. (I just made simple test project to verify). You can get the main code here if you want to give it a try: Main.hx (Button.hx)

  3. jfroco says:

    Hello Brad,

    Thank you very much. It worked flawlessly in my game in flash, cpp and android targets.

    Best regards

  4. Eric says:

    Hey Brad – Thanks … it is amazing how fast it was to get this incorporated into my app and begin saving and retrieving data. Much appreciated.

    Have you or anyone else been able to try this on IOS yet? It should work right?

  5. Brad says:

    I haven’t been able to check iOS (no device), but as far as I know it should work

You must be logged in to post a comment.