Serialization stuff

Serialization

Everybody wants their game to be optimized as much as possible. While developing a game I’ve faced with an issue when I had to synchronize something very large (let’s assume a List of Entity classes which have their own parameters inside, such as Age, BirthDate, Name, etc.); or I had to write save and load system. Of course you can say, why do I need to serialize that, if I just can pass the raw data of Entity. Well, theoretically you can, but what if Entity class has 10+ parameters and it is not just a single Entity but a bunch of them, would you write something similar to ” void SendEntityData(par1, par2, par3, par4, par5, par6, par7, par8, par9, par10, entity2par1, entity2par2 …” ? Apparently, not.

So what’s Serialization? It is the method of compressing your data into the smaller data which is nothing more than byte data. That byte array can be deserialized into the original data which has been compressed previously.  There are a lot of serializators, you probably know some of them: JSON, BinaryFormatter, MessagePack, ProtoBuf etc.

Before choosing a serialization method you have to determine you needs, in case if you need human-readability of data; I suggest to use JSON or XML, but be careful with data size and perfomance do not pass this stuff through the network, especially in a Fast-paced multiplayer games =). If human-readability is not the factor and you the data to be encrypted and you need high performance and smallest data size; I suggest ProtoBuf-net. You can and read more about it at: https://github.com/mgravell/protobuf-net

If you previously use the BinaryFormatter then I want to show you a little difference between it and ProtoBuf-net. In the following example List of Entities has been written into the two different files over 1000+ times.

Screenshot_6.png
Full size image

As you can see, protobuf-net is 5x times faster and its file size 10x times less than BinaryFormatter.

Continue reading “Serialization stuff”

Advertisements

C# custom attributes

Suppose you do not want always check the distance between player and some object and instead of it you just want to mark your function like [DistanceAttribute(2f)] where 2f is allowable distance and your function will be runned only if that distance between player and object will be Less or Equal of 2 and method will run automatically without any “if” over and over.

Continue reading “C# custom attributes”

C# preprocessor directives or how to split your code (free project)

I’m pretty sure you do not want to include Server code to the Client build, for instance – your server code contains important SQL passwords or any sort of account infromation, instead of lead two different projects you could simply mark your code #if Server and #if Client.

Continue reading “C# preprocessor directives or how to split your code (free project)”