Search Unity

RPC behaviour of new netcode

Discussion in 'NetCode for ECS' started by Pandoux, Sep 21, 2020.

  1. Pandoux

    Pandoux

    Joined:
    Jun 23, 2016
    Posts:
    6
    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

    Code (CSharp):
    1. public void Serialize(ref DataStreamWriter writer)
    2. {
    3.     writer.WriteInt(intData);
    4.     writer.WriteShort(shortData);
    5. }
    Instead of

    Code (CSharp):
    1. public void Serialize(ref DataStreamWriter writer, in OurDataRpcCommand data)
    2. {
    3.     writer.WriteInt(data.intData);
    4.     writer.WriteShort(data.shortData);
    5. }
    What is the interest of using the argument way ? Is it for a performance issue ?
     
  2. timjohansson

    timjohansson

    Unity Technologies

    Joined:
    Jul 13, 2016
    Posts:
    473
    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?
     
  3. Pandoux

    Pandoux

    Joined:
    Jun 23, 2016
    Posts:
    6
    I don't need it, I was just curious about the netcode.