What is the simplest way to send FlatBuffers between computers using SendMatchStateAsync and ReceivedMatchState?

What is the simplest way to send FlatBuffers (specifically vector3s containing player positions) between computers using SendMatchStateAsync and ReceivedMatchState?

FlatBuffers and ProtocolBuffers are the preferable methods according to the documentation, but there doesn’t seem to be anything outlining their use with Nakama.
The problem isn’t that Nakama requires more than the FlatBuffers tutorials teach. Rather, the FlatBuffers tutorials focus on so much that Nakama handles for you. I don’t need to worry about uniquely identifying the senders from one another because Nakama does that for me already, I don’t need to build RPC functions because Nakama already handles that. I simply need to know how to set up FlatBuffer objects so that they can be sent between clients using Nakamas built in functionality and for the life of me I can’t find out how.

Any help is appreciated, thanks in advance.

P.S, This question has been asked before (here), but it fell off the map before it was really answered.

2 Likes

You didn’t mention which engine, but in my experience with Unity, I agree, Heroiclabs should put a simple example usage of Protobuf with Unity and Nakama.

First thing to know it’s not easy to use Protobuf with Unity, there is two versions, Google.Protobuf (C++ bindings) and protobuf-net, both are on NuGet (c# package manager) and Unity doesn’t support NuGet.

So the trick is to go on nuget website, download Google.Protobuf and its dependencies, unzip and put in your project anywhere, unity will likely complain saying multiple DLL with same name, look if you use .NET 4.x or net standard 2.0, delete the DLL you’re not using accordingly.
I encountered some issues with Protobuf + Unity, see this issue (beware code stripping, conflicts with com.unity.collections and System.Memory …), but it can be worth the price, Protobuf is really easy to use on server side.

About the usage, you just have to write some .proto messages, compile then in code:

message Foo {
	string bar = 1;
}
var p = new Foo {Bar = "baz"}.ToByteString().ToStringUtf8();
Socket.RpcAsync("for_example_some_rpc", p);

It’s quite the same for matches:

Socket.SendMatchStateAsync(0, 0, p.ToByteArray());

I use Protobuf in niwrad for basically everything even serializing some data to files, you can try it, I have Nakama running in my Kubernetes cluster.

Code examples

2 Likes

@louis030195 This is a good answer. We do have plans to provide examples for Protobuf and FlatBuffers to be used to represent netcode in a game. We actually use Protobuf with all game teams that we work with on commercial projects and the approach works really well. Either Protobuf or FlatBuffers are great.

both are on NuGet (c# package manager) and Unity doesn’t support NuGet.

This is not quite true. Microsoft has an Asset for Unity engine which integrates the engine with Nuget for .NET packages. - https://github.com/microsoft/MSBuildForUnity

We’re just about to launch Nakama 3.0 in the new year and afterwards we’ll share our project template that uses Protobuf for a simple netcode example.

2 Likes

Thank you, that is exactly what I was looking for.

2 Likes

Tell me, are FlatBuffers any better? if both are a lot of trouble I suppose it would be best to go with the faster one.

I went for Protobuf for simplicity first, but now for a real project I was wondering too, but apparently lot of game teams use Protobuf.
I think if you really want high performance you have to implement your own protocol

But I suppose it add a LOT of friction and maintainance cost compared to Protobuf, it’s always a trade-off.
Personally will stick to Protobuf (for real time fast-paced game) and if there is a performance bottleneck it’s not super hard to refactor.

2 Likes

:+1: for this asset. Thanks, man.

1 Like

That makes a lot of sense, but considering the trouble I’ve had with Protobuffers and FlatBuffers with unity and how Nakama handles everything except serializing and deserializing I think I’ll try making a custom serialization protocol using that article.

Thanks again for the help.

@GTG101 I stumbled across this post the other day which might also be worth a read:

https://blog.extrawurst.org/gamedev/unity/programming/rust/2020/12/26/unity-flatbuffers.html

Thanks, but with how long I’ve spent trying to get FlatBuffers to work with Unity I think I’ll try the custom approach.

Speaking of which, I assume Nakama handles any security and transfer concerns when I send data using socket.SendMatchStateAsync?

@GTG101 What do you mean by “security and transfer concerns”?