Sharing structs across client and server

Let’s say we have a struct on go that looks like this

type PlayerData struct {
    Amount int `json:"amount"`
    Count uint `json:"count"`
    Id string `json:"id"`
}

and also a c# counterpart for unity client

[Serializable]
class PlayerData
{
    public int amount;
    public int count;
    public string id;
}

Above are a simplified example, but in reality there are a couple of “messages” that needs to be coded on the go module in order to reflect the message that could be received client side, some enums that needs to be coded both on client and server side.

Imagine a send reward system from nakama console that could be send via notification, it can be set for a particular user, change the amount of the reward, set the reward types, and then finally sent to the player.

In order to prevent error and reduce bugs, what is the preferred / the usual way to make a struct defines that could be compiled / transcribed into go and c# code? similar to what protoc compiler did to .proto files, but leaner as in only generate the required struct/classes?

@cloudAle The usual approach is to use FlatBuffers or Protocol Buffers to define your shared code between client and server. We’ve used Protobuf on a lot of game projects and it works exceptionally well. These technologies were specifically designed to solve the problem you mention.

similar to what protoc compiler did to .proto files, but leaner as in only generate the required struct/classes?

What do you mean you want it to be leaner?

@novabyte

This probably looks dumb for me not knowing how protobuf / flatbuffers really work but a generated protobuf file, like https://github.com/protocolbuffers/protobuf/blob/master/csharp/src/AddressBook/Addressbook.cs

have a lot of reflection code generated, whereas in unity you could just simply JsonUtility.FromJson(json) not needing all those generated code, just the c# serializable class

Looks like it just a matter of running the protoc with a lean flags then?