Jump to content

  • Log In with Google      Sign In   
  • Create Account

How to write game-data into file?

  • You cannot reply to this topic
2 replies to this topic

#1 Aliii   Members   -  Reputation: 1446


Posted 01 September 2014 - 11:32 AM

When saving game data, how should one write floats for example into the file? Someone quicksaves the game then later loads it back. Should I assume that the save-load will be on the same device or on devices with the same float format/size, same endianness, ...and write it in binary? Or divide it into two integers - mantissa and exponent - and save those in binary? Or convert it to text and parse back to float when loading?


#2 haegarr   Crossbones+   -  Reputation: 4299


Posted 01 September 2014 - 11:49 AM

Saving a floating point number can losslessly be done only without conversion to a decimal number format. This means: Store it either binary "as is" or else the equivalent bit pattern, perhaps hex number, as text. The latter one can be done by reading a float as uint32_t, and a double as uint64_t, perhaps by using a union.


It is very likely that a stored game will be reloaded on the same machine. If you want to be safe, you can write an endianess mark at the beginning of the file (so do I). The format of binary floating point number should be no problem, as long as you stick with the 32 and 64 bit numbers and don't try to use any extended format (e.g. the 80 bit internal format).


My preference is saving binary data, if possible even loadable "in-place".

#3 Glass_Knife   Moderators   -  Reputation: 4412


Posted 01 September 2014 - 11:50 AM

See this for a discussion of testing endianness:




Since many languages like Java always assume big-endian, and everything sent over the network is assumed big-endian, I would always save the file in big-endian, the then check if the current machine needs to convert the values.  

I think, therefore I am. I think? - "George Carlin"
Indie Game Programming