Search This Blog

Friday, April 15, 2016

Load & Save

Persistence in a game is something mandatory but is often not enough thought. At least in my opinion.

Also persistence is split between player state and world persistence. In many games the player had little to no influence to the world or at least the influence is only temporary. For example you break a lamp save quit the game and reload and the lamp will be as new. What you wear and your stats are usually saved and kept.

Nothing wrong with this, I mean, you have to think about how much it would take to store every little changes you do on the world.

So how could the persistence be implemented for a web game? First you need to think about what you want to do with the data. For example, if we work like me with a 2D grid of tiles, the server may not need to even handle them, and I quite certainly don't want to make odd queries on those data. Therefore a straight forward serialization like JSON.stringify will do the trick.

On the other side, if you would like to make reports on some data, for example find the best players, or those which has more money in bank, serializing the player class and storing it "as is" is not a good idea. Better then to store the values in a format you can then query correctly.

Another thing to think of is if you want to compress your data somehow or you agree to use all the space as needed. Compressing can either be done with libraries like ZIP, GZIP or such or simply do run length encoding (RLE for short). RLE is a simple method which counts how many times you have the same thing coming, and then having a number plus the thing. For example the string "AAAAABBBBCAAAD" could be written like "5A4B1C3A1D", reducing the string from 14 character to 10 which means a compression of 28% less space. Some times it works some times it doesn't specially if each character is used only once or very few times.

The compression could be made on the client such that the data would also take less space over the network and ideally be faster, or could be done on the server if it is something too complex to do on the client side and what you want is just save space on the your server disk.

No comments:

Post a Comment