Search Unity

I need help with String Network Variables

Discussion in 'Netcode for GameObjects' started by supertobi, May 17, 2022.

  1. supertobi

    supertobi

    Joined:
    Mar 29, 2022
    Posts:
    3
    I want to create a String network variable. I read online i could use FixedString64Bytes but when i try

    Code (CSharp):
    1. private NetworkVariable<FixedString64Bytes> playerName = new NetworkVariable<FixedString64Bytes>();
    i get following error message:
    Type Unity.Collections.FixedString64Bytes is not serializable - it must implement either INetworkSerializable or ISerializeByMemcpy

    I also tried creating a custom struct, but i get an error in the SerializeValue line(the constraints for type arguments are not satisfied), which is weird because i copied this code from a youtube tutorial)

    Code (CSharp):
    1. public struct NetworkString : INetworkSerializable
    2. {
    3.     private FixedString32Bytes info;
    4.     public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
    5.     {
    6.         serializer.SerializeValue(ref info);
    7.     }


    What is the best way to send strings through network variables?
    thank you in advance
     
  2. CosmoM

    CosmoM

    Joined:
    Oct 31, 2015
    Posts:
    204
  3. supertobi

    supertobi

    Joined:
    Mar 29, 2022
    Posts:
    3
  4. ClearDark

    ClearDark

    Joined:
    Aug 25, 2013
    Posts:
    14
    Or you could try the much simpler solution:


    Code (CSharp):
    1.     [SerializeField]
    2.     public NetworkVariable<ForceNetworkSerializeByMemcpy<FixedString64Bytes>> stringVar = new NetworkVariable<ForceNetworkSerializeByMemcpy<FixedString64Bytes>>();
     
  5. CasualT_Bossfight

    CasualT_Bossfight

    Joined:
    Oct 26, 2016
    Posts:
    8
    That is so ugly @ClearDark :eek: But if it works I'll give it a try :p