RPC behaviour of new netcode

Hi,

I am using the new netcode of Unity but there is something I don’t understand using IRpcCommandSerializer interface.

The interface provide us the Serialize() and Deserialize() method and the 2nd argument is the RPC itself. But why the Unity team decided to do it this way ? The thing I don’t undertsand is that they don’t use directly the base RPC instead of the argument.

For example

public void Serialize(ref DataStreamWriter writer)
{
    writer.WriteInt(intData);
    writer.WriteShort(shortData);
}

Instead of

public void Serialize(ref DataStreamWriter writer, in OurDataRpcCommand data)
{
    writer.WriteInt(data.intData);
    writer.WriteShort(data.shortData);
}

What is the interest of using the argument way ? Is it for a performance issue ?

The RPC and the serializer for the RPC does not have to be the same struct, they can be two completely different structs. The code-gen for RPCs relies on this so we can create the serialization for an RPC without modifying it, just relying on adding new code for a separate struct.

When we serialize/deserialize we are not assuming the serializer and the rpc are the same struct, so serialization is something like default(MyRPCSerializer).Serialize(myRpc). As such you have to use the input or the values will all be zero.

Why do you need to write custom serialization instead of using the code-gen?

I don’t need it, I was just curious about the netcode.