[solved] Serializing save game data causes StackOverflow on Android

Hi all, I’m currently working the save game feature on a 2D space RTS game for desktop and Android systems. However, I’m having a hard time finding an approach that works well on Android.

The game state is contained in a single object (“galaxy” of class Galaxy). I write the galaxy object to a file using serialization. For this I both tried vanilla Java serialization and by using the Kryo library. The serialization code is pretty simple and standard, e.g.:

private static void writeFile(FileHandle fileHandle, Object object) throws Exception {
		ObjectOutputStream out = new ObjectOutputStream(fileHandle.write(false));
		out.writeObject(object);
		out.close();
}

This is all very elegant and works fast & smooth as butter on my desktop computer. However, testing this on my Galaxy S2 Android smartphone results consistently in a StackOverflow exception >:(

I kind of know what is causing this: the stack size on Android is smaller than on my desktop computer. Also, the galaxy state object is somewhat nested, e,.g. something like:

Galaxy.stars.get(0).fleets.get(0).get(0).velocity.x

Is a valid call (though somewhat silly) in my code base. However, the save game on my desktop is quite small (beteen 30k and 500k), so its not really a huge amount of data to handle.

There must be a better way to go at this. Do you guys have any ideas on how I could implement a save game feature which does not have this issue? Thanks.

Have you tried serializing in parts? I.e, serialize the first x stars, than save that. Then serialize the next x stars? It makes the process more complex because you would need to partition the list into different objects and append them into a single object for the game later - but you can probably achieve this.

Wow, 500kb? That’s A LOT of data, I don’t know what your game is like, but I think if you make that class a bit more ‘datafriendly’ I mean if you save a unit: type, x-position, y-position, xvelo, yvelo, targetx, targety, and bought upgrades, ‘001 and 002’. That is (average) 30-50??? bytes per unit, save also some money and some buildings, and game saved (not even close). You must have a lot of space ships (or money, heheh) to get 500.000 bytes.

However this is useful or not (its not), good luck :slight_smile:

@Herjan: Haha, you are right about the LOT of data. I accidently saved a helper array for each star (containing ordered distances to other stars, to speed up calculations). Without these helper arrays the data is reduced to below 50k (with hundreds of stars & ships). Thanks for the hint ;D

@Jeremy: Sounds like something worth a try. I think I can figure out how to make that work.

@Jeremy: I partitioned the save and load process and now it works like a charm! Thanks for the advice ;D