Tower Defense Save File

Alright, this is a biggie for me. I need to create a save file that writes the data of all the blocks in the game to the save. The blocks are like this: airBlocks can contain towers which also have upgrades. If the airBlock doesn’t contain a tower its value is 0 (Value.airAir) if it’s a tower, than it is another int (Value.airTower"insert-tower-name-here"). So what’s the best way to write all the values of the current blocks including tower upgrades to the file?
Thanks,
-cMp

A good way to save things to files is to create two functions on every items you want to save. One for creating a byte[] of the values (exporting its values), and another one for decoding an byte[] (importing its values).

Example :


public byte[] getBytes() {
    return this.name.getBytes();
}

public void fromBytes() {
    this.name = new String(bytes, "iso-8859-1"); // remplacer l'encodage au besoin

}

it’s a very abstract example but you’re probably going to need to add lists or arrays with a correct size for storing your data.

I’m afraid I don’t fully understand what you mean here. Would I put this method everywhere, or in one place and just call it tons of times every auto-save? In all honesty I have no CLUE what I have to do for this, so you may have to walk me through this if you don’t mind

Let’s say you have an Object(class) called “Player” and this player has value(String name, int life).

You need to make an array of bytes to save to the file. The best way is to make an interface (let’s say we call it “ISavable”).

this interface forces you to implement the two methods shown previously. when you save your game, you itterate over all your game elements that are “ISavable” and you append the byte array of each object to the file. when you load the game, it’s gonna go over all the file byte by byte and put those values in the right place using the fromBytes(byte[] b) method.

hope this helps

Alright, makes sense. Way less code than I was thinking of doing originally. And I’m guessing that this willl work for a Tower Defense game that is block based? And probably that the fact it is block based will make it easier?