Skip to main content
5 of 7
added 328 characters in body; edited body
Iain
  • 6.6k
  • 3
  • 35
  • 46

XML is a good choice if you're not limited by size and it is supported natively (eg in .NET and Flash) but if you want a slim format you can create your own format and parser quite easy. I normally use 1 character eg. comma to seperate each object. To decode the string do a split on comma. Now each object needs different properties so seperate these with a different character eg semi colon, and use another character to seperate the property names from property vales, eg. Colon. All thus can be decoded easily without regex just by using string.split. Here is an example:

id:1;x:5;y:45.2;angle:45,id:28;x:56;y:89;angle:12;health:78

you can save even more space by keeping property names down to 1 character, eg h for health. Eg.

i:1;x:5;y:45.2;a:45,i:28;x:56;y:89;a:12;h:78

Compare to JSON alternative:

{"o":[{"i":1, "x":5, "y":45.2, "a":45}, {"i":28, "x":56, "y":89, "a":12, "h":78}]}

Also, if you want to get the size of your numbers down, you can encode them into ASCI. If you use a standard 256 asci charset you can encode an integer between 0 and 255 into one character. If you use 2 that's 256x256 = 65536 possible numbers, which should be enough to store a relative position in your play area.

To get a further size reduction, you can use read/write order to determine which value is which, so the first two characters represent the id, the next two are the x position, the next two the y, then the angle, then health, etc. So:

F5DGP@%&002DFTK#OP1F

could store all the same information as the other examples.

Tile grids can be stored as just a string with each character representing a different type of tile eg:

i789pog5h3kl

where I might mean lava, 9 mean grass etc

Iain
  • 6.6k
  • 3
  • 35
  • 46