Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Bug [Fishnet] fishnet doesn't recognize custom serializer for inherited members

Discussion in 'Multiplayer' started by ParsaRt, Feb 8, 2024.

  1. ParsaRt

    ParsaRt

    Joined:
    Jun 30, 2013
    Posts:
    9
    hey, imagine (abstract ClassA):ClassB:ClassC and i have a custom serializer for ClassA and i'm already handling all the inherited members serialization in the ClassA serializer, i don't have any compiler exceptions about a missing serializer, but as soon as one of the inherited types are being used on the network, i get an exception saying that Read or Write method was not found for the inherited type, what i don't understand is how it doesn't use the custom serializer provided in the base class ?
     
  2. Punfish

    Punfish

    Joined:
    Dec 7, 2014
    Posts:
    442
    From the sounds of it this maybe is not a bug. FishNet is writing the inherited types, when you are sending ClassC.
    I'm guessing here as I cannot see your serializer, but I assume you have something like this?
    Code (CSharp):
    1. public static void WriteClassC(this Writer w, ClassC value)
    The easiest way to resolve this is create a serializer for the base type, and then serialize/deserialize based on type. Here's an example...

    Code (CSharp):
    1.  
    2.  
    3. public static void WriteClassA(this Writer w, ClassA value)
    4. {
    5.     /* Bytes written are indicating the type on the receiving end. */
    6.     if (value is ClassC c)
    7.     {
    8.         w.WriteByte(1);
    9.         w.Write(c);
    10.     }
    11.     else if (value is ClassB b)
    12.     {
    13.         w.WriteByte(2);
    14.         w.Write(b);
    15.     }
    16. }
    17. public static void ReadClassA(this Reader r)
    18. {
    19.     byte id = r.ReadByte();
    20.     if (id == 1)
    21.        return r.Read<ClassC>();
    22.     else if (id == 2)
    23.        return r.Read<ClassB>();
    24. }
    25.  
    PS: Something good for our documentation as I noticed it did not discussed this situation.
     
  3. ParsaRt

    ParsaRt

    Joined:
    Jun 30, 2013
    Posts:
    9
    Hey, thanks for the reply
    I am indeed doing this, its not a casting problem or losing data because of inheritance, the problem is that as soon as ClassC is being used for network, i get an exception saying that "Write method not found for ClassC. Use a supported type or create a custom serializer." which is confusing since ClassC is in fact a ClassA as well, and ClassA has a custom serializer handling the inheritance
     
  4. Punfish

    Punfish

    Joined:
    Dec 7, 2014
    Posts:
    442
    Well that doesn't sound right then. Show me your breaking 'for network' code and what FN version please?
     
  5. ParsaRt

    ParsaRt

    Joined:
    Jun 30, 2013
    Posts:
    9
    all the concrete inherited types of abstract ClassA are being used in RPC calls, SyncLists, etc.
    I'm using 4.0.6
     
  6. ParsaRt

    ParsaRt

    Joined:
    Jun 30, 2013
    Posts:
    9
    sorry it appears that I was wrong about the RPC call usage, the error is thrown when call i call this
    Code (CSharp):
    1. writer.Write(ClassC)
     
  7. Punfish

    Punfish

    Joined:
    Dec 7, 2014
    Posts:
    442
    I need to see full code please.
    Ideally the RPC signature, what you are passing into it, reader and writer.

    Also if you could update to 4.1.0, the lower v4 were beta releases where 4.1.0 is our first stable for v4.