In this blog I don’t want to just ‘praise’ my games, but I would like to give some technical insights in how they technically work. Mind you, the solutions that I’m using are not in any way the perfect solution to certain problems. They work for my situation and if you’re looking to copying it, that’s fine but a different context might require a different solution.
Another warning, I’m neither a great writer nor is my first language English. So I might become a bit incoherent as I jump from one subject to another.
Today I want to talk about save games. There are different ways to implement saving/loading in your game. One way to do it is to save all the current agents (characters and other ingame objects), but this means you’ll have to save all the effects and the current state of them as well. Like an invisibility spell that wears off in one more turn.
Battlelance saves its games by first of all saving the initial setup, which is just an object with a few arrays to record per playable character the player ID, player name, class, team number and some game master attributes like spawns, traps and abilities. With this information and the map the game can be set to its start position.
Once the players start the game all kind of data is being transferred over the network, to make sure this all happens in the right order and no threads access the same resources at once, each event that the game receives (movement, damage, summons, end turn, etc. etc.) is being push into a queue and afterwards processed. I’ll go more into this in another blog about the multiplayer aspect of the game. Sufficient to say is that these events constitute the consequences off all the actions that have taken place. So if somebody attacks, only the damage (if any) that’s done is enough to reconstruct the events that have happened.
Once the event is processed it’s stored in a savegame queue and once the game is saved this entire queue with the initial setup, and by replaying each event in turn the game is progressed to the same state as before.
Here is a snipplet of code of how the Save game object is written to a file and how it’s retrieved.
public static void SaveGame(GameContainer container, ConcurrentLinkedQueue<Object> saveGame)
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
SaveGame game = new SaveGame(saveGame, container.getInitialState(), container.getMap2(), container.getGameName());
byte binary = baos.toByteArray();
String text = Base64.encodeBase64String(binary);
PrintWriter out = new PrintWriter("save/" + container.getGameName() + ".btl");
catch (Exception e)
public static SaveGame LoadGame(String file)
String text = readFile(file);
byte binary = Base64.decodeBase64(text);
ByteArrayInputStream baos = new ByteArrayInputStream(binary);
ObjectInputStream oos = new ObjectInputStream(baos);
catch (Exception e)