I’ve now got an idea on how to make my Tile editor but I dont know how to save the map data to a file and then read them from the file. Any ideas? (Maps are 2D arrays)
Well, it really depends on the context… If you need metadata about each tile, I’d go with a simple XML parser. Although you could probably get away with just using serialization or basic file IO if it’s just tile indexes or something.
We may need a bit more information than just a sentence
EDIT: Also this little bugger: http://jnbt.sourceforge.net/ was made by Notch and is very nice to use.
EDIT: just saw “text file” :\
4 bytes: width
4 bytes: height
x bytes for every tile, w * h tiles
Where x is most easily 4, but is probably a “waste” given that you probably don’t need to have 4 billion different tile types.
Can be written via DataOutputStream, read via DataInputStream or better, Scanner.
EDIT: for text files: loop around write(Arrays.toString(…));
I don’t have a good looking example with me right now, but If I’m correct, you would just have to save the array of map coordinates in to a file (assuming you know some file i/o), then just load that file’s map data (If, I’m any help at all).
Really you don’t need to save coordinates, but if the tile size / map size is variable per map, you should just put it some tile info at the start of the file. You can really just interoperate the tile positions within the application.
EDIT: Or even new lines (\n) would be acceptable to denote a new tile row. But make sure to check for the rows size being consistent throughout the map. So that breakes it down to holding tile IDs and tile dimensions.
I was looking more for a for loop that would go through the data and save it to a file that could then be read from another for loop to make it back into a 2d array. Any ideas?