Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Bug NetworkString script gives an error CS0315

Discussion in 'Netcode for GameObjects' started by KristenBoyGames, May 14, 2022.

  1. KristenBoyGames

    KristenBoyGames

    Joined:
    May 23, 2021
    Posts:
    38
    out of nowhere the NetworkString script started giving an error saying
    "CS0315: The type 'Unity.Collections.FixedString32Bytes' cannot be used as type parameter 'T' in the generic type or method 'BufferSerializer<T>.SerializeValue<T>(ref T, FastBufferWriter.ForPrimitives)'. There is no boxing conversion from 'Unity.Collections.FixedString32Bytes' to 'System.IComparable'."
    Code (CSharp):
    1. using Unity.Collections;
    2. using Unity.Netcode;
    3.  
    4. public struct NetworkString : INetworkSerializable
    5. {
    6.     private FixedString32Bytes info;
    7.     public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
    8.     {
    9.         serializer.SerializeValue(ref info); // SerializeValue(ref info) is giving the error
    10.     }
    11.  
    12.     public override string ToString()
    13.     {
    14.         return info.ToString();
    15.     }
    16.  
    17.     public static implicit operator string(NetworkString s) => s.ToString();
    18.     public static implicit operator NetworkString(string s) => new NetworkString() { info = new FixedString32Bytes(s) };
    19. }
    20.  
     
    inakidiezgalarza and leosurf111 like this.
  2. luke-unity

    luke-unity

    Joined:
    Sep 30, 2020
    Posts:
    306
    replace
    public struct NetworkString : INetworkSerializable
    with
    public struct NetworkString : INetworkSerializable, INetworkSerializeByMemcpy
     
  3. leosurf111

    leosurf111

    Joined:
    Apr 8, 2015
    Posts:
    12
    Hi I have the same issue and replacing
    public struct NetworkString : INetworkSerializable
    with
    public struct NetworkString : INetworkSerializable, INetworkSerializeByMemcpy

    is not working,
    how can I fix this?
     
  4. luke-unity

    luke-unity

    Joined:
    Sep 30, 2020
    Posts:
    306
    Code (CSharp):
    1. using Unity.Collections;
    2. using Unity.Netcode;
    3.  
    4. public struct NetworkString :  INetworkSerializeByMemcpy
    5. {
    6.     private ForceNetworkSerializeByMemcpy<FixedString32Bytes> _info;
    7.     public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
    8.     {
    9.         serializer.SerializeValue(ref _info);
    10.        
    11.     }
    12.  
    13.     public override string ToString()
    14.     {
    15.         return _info.Value.ToString();
    16.     }
    17.  
    18.     public static implicit operator string(NetworkString s) => s.ToString();
    19.     public static implicit operator NetworkString(string s) => new NetworkString() { _info = new FixedString32Bytes(s) };
    20. }
    This should work.
     
  5. Boolable

    Boolable

    Joined:
    Jun 28, 2015
    Posts:
    20
    @luke-unity can you please explain why we should use ForceNetworkSerializeByMemcpy? (It worked well with 1.0.0-pre.7)
     
  6. luke-unity

    luke-unity

    Joined:
    Sep 30, 2020
    Posts:
    306
    It is because we made a breaking change which requires to use the ForceNetworkSerializeByMemcpy now. We are looking in bringing back the previous workflow in a future release.
     
    wokamoka likes this.
  7. wokamoka

    wokamoka

    Joined:
    Jul 3, 2012
    Posts:
    10
    Thanks a lot for the answer.
    It helped me fix an issue when upgrading to latest version of Netcode for gameobjects.
    :)