Skip to main content

Using MessagePack to transfer data between a Unity cleint and Go server

So this took me a lot longer than it should have.


GoLang
Vmihailenco's library is my favorite at the moment for GoLang. It's one line to encode and one line to decode. I simply create a struct to get the packet's Id and then a struct for the input. I'm just doing this to prepare for any other data coming into the server.

//GetPacketID Get's the packet id from a packet
type GetPacketID struct {
    ID int
}
//InputPacket Get's input data from packet
type InputPacket struct {
    ID int
    X int
    Y int
    entity *Entity
}

This now allows me to marshal data like this:

n, addr, err := conn.ReadFromUDP(buf)
CheckError(err)
//deseralize
var pID GetPacketID
err = msgpack.Unmarshal(buf[:n], &pID)

and unmarshal it like this:

var statePacket StatePacket
b, err := msgpack.Marshal(statePacket)
CheckError(err)

You can probably see why I like this library so much. One line serialization and it takes into account which variables I want to export and which I don't. Any variable in a struct that isn't capitalized will not be exported so the "entity"variable of the InputPacket class will not be serialized.

Unity

Now when I'm getting data on the client the nicest MessagePack library for Unity is Neuecc's library. It has a very similar model, but there is a really annoying step. Here is one of my classes:

[MessagePackObject(keyAsPropertyName: true)]
public class StatePacket
{
// Key is serialization index, it is important for versioning.
public EntityPacket[] Entities { get; set; }
}

The first line is VERY important. Every library for Go that serializes data will use the variable names as keys. Most of the examples on Neuecc's GitHub page say to let the key be a specified index, but this won't work. Data can now be serialized with one line:

var bytes = MessagePackSerializer.Serialize(statePacket);

and deserialized like this

var mc2 = MessagePackSerializer.Deserialize<StatePacket>(inputStream);

And now we are officially sending data between a Unity client and Go server!

Comments

Popular posts from this blog

It's 1:28 AM and I have two midterms tomorrow

My name is Zac Holland and I'm a student at University of Denver. For the past few years I have been trying to figure out how to create an MMO using Unity and GO. I have gone through a ridiculous amount of versions that ultimately got trashed. Through trial and error, I think this iteration will be the best and most efficient version. First of all, let me catch you up. Server: All I have is a simple authoritative server that will receive movement data, adjust the entity, and report the game state back to the user. Client: The client is basically just displaying information. It will receive a game state about 20 times a second and adjust each entity accordingly. Note: Every post will be linked to a specific commit in each repo. I'll link to the source of each commit at the bottom or something. These first commits don't have much though. Like at all, there is really no difference. Repos Client Server