Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Bug FastBufferWriter.WriteValue() won't accept unmanaged generic type T anymore?

Discussion in 'Netcode for GameObjects' started by Senshi, Jul 17, 2022.

  1. Senshi

    Senshi

    Joined:
    Oct 3, 2010
    Posts:
    545
    EDIT: Of course I figure it out 2 seconds after posting this... unmanaged does not automatically mean the type satisfies the required constraints. Changing the function signature to
    static FastBufferWriter CreateMessageWriter<T>(int bufferSize, T value) where T : unmanaged, IComparable, IConvertible, IComparable<T>, IEquatable<T> {}
    does the trick. Carry on!

    --------------------------

    So I just came back to a project that required a reinstall of the editor, and it gave me an error. I updated Netcode for GameObjects to v1.0.0 and now I'm here. I had written the following helper method:

    Code (CSharp):
    1. static FastBufferWriter CreateMessageWriter<T>(int bufferSize, T value) where T : unmanaged {
    2.     using FastBufferWriter writer = new FastBufferWriter(bufferSize, Allocator.Temp);
    3.     writer.TryBeginWrite(bufferSize);
    4.     writer.WriteValue(value);
    5.  
    6.     return writer;
    7. }
    I'm 99% sure this used to work, and indeed even the current docs imply this:

    However, I am still getting the following error on my
    writer.WriteValue(value)
    line:

    Code (CSharp):
    1. error CS0314: The type 'T' cannot be used as type parameter 'T' in the generic type or method 'FastBufferWriter.WriteValue<T>(in T, FastBufferWriter.ForNetworkSerializable)'. There is no boxing conversion or type parameter conversion from 'T' to 'Unity.Netcode.INetworkSerializable'.
    What gives? Am I missing something, or did something break at some point? Gave this post the Bug flair for now, but happy to change to Question if that's more appropriate.

    Thanks!
     
    Last edited: Jul 17, 2022