Savegames - how to?

@dranonymous:
Yes my image load code works. And thanks for the idea with the binary file. But I am not that familar with retrieving byte informations from a file and convert them to their needed data types.

I took a look at the DataInput/OutputStream example in the java tutorial, but that wasn’t that good. I got the informations stored, but retrieving failed in some manners.

Can you give a piece of example code? Please.

Evil

ED - Sorry about not getting back to you. I forgot about this thread until today when someone asked about XML serialization.

To easily serialize out your game state I’d look here -

xStream

It works really well.

As for the example about storing your map as a binary mapped to bytes or ints, I’ll see what I can work up.

Again, sorry for the delay!

Dr. A>

Thx for replay.
I got a workaround running, but have some problems left.
My mapsize is an integer value, and iam not sure if i should save it with writeInt() or with writeByte(). I hope that there is no big deal with those to functions and i can use them to avoid dataloss from converting int to byte.

So i did my mapfileformat.
I saved the mapnames characters each as byte in the file, but the name stands there in plaintext when i open the file. Is there a way to avoid this?

questions over questions :wink:

Since you changed your image, I cna’t tell its you. :slight_smile:

You could take each byte of the map name and do a bit shift on it. Something like -


byte newByte = 0;

newByte = originalByte << 1;

if(originalByte > 128)  // This wraps the top bit to the beginning
     newByte += 1;


Then just write out newByte.

To ‘decode’ the bytes do this -


byte originalByte;

originalByte = newByte >>> 1;

if(originalByte & (byte)0x01 == 1) // Adds the top bit back on
     originalByte = originalByte | (byte)(0x80);


Does that make sense?

Dr. A>

Yea its still me. I just changed my avatar to the one i use in most forums. My rave pant ^^

@code: Sounds fine. I just shift the bit and a textedit won’t display the mapname anyway :slight_smile:

Thx.

But what about my integer values? Can i store them as byte too? Or does i have to use writeInt()?

Well, it depends. This might be a bit confusing, unless you a comfortable with number formats.

If you ints don’t go above 255, then you can store them as bytes. To store them just cast the int to a byte. Reading them back in won’t be so straightforward. If the original number was greater than 127, you’ll need to mask off the top 24 bits, to remove the sign change that will occur. You can do this by -


int realInt = myByte & 0xff00;

On the other hand, if your ints are greater than 255, you’re ‘stuck’ and have to write out 4 bytes per int. The only way around that would be to test for how large your numbers are and see if they will fit into a 2 byte short.

Dr. A>