Unity-Technologies / com.unity.netcode.gameobjects

Netcode for GameObjects is a high-level netcode SDK that provides networking capabilities to GameObject/MonoBehaviour workflows within Unity and sits on top of underlying transport layer.
MIT License
2.15k stars 435 forks source link

NetworkList<T> initialization and Docs version: 1.0.0-pre.9 #1993

Closed Valentineed closed 2 years ago

Valentineed commented 2 years ago

Description

The problem is similar to https://github.com/Unity-Technologies/com.unity.netcode.gameobjects/issues/1396

I can not serialize a FixedString32Bytes or FixedString64Bytes and if I use string is not possible to use a NetworkList because is not nullable

public struct LobbyPlayerState : INetworkSerializable, IEquatable { public ulong ClientId; public FixedString32Bytes PlayerName; public bool IsReady;

public LobbyPlayerState(ulong clientId, FixedString32Bytes playerName, bool isReady)
{
    ClientId = clientId;
    PlayerName = playerName;
    IsReady = isReady;
}

public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
{
    serializer.SerializeValue(ref ClientId);
    serializer.SerializeValue(ref PlayerName);
    serializer.SerializeValue(ref IsReady);
}

public bool Equals(LobbyPlayerState other)
{
    return ClientId == other.ClientId &&
        PlayerName.Equals(other.PlayerName) &&
        IsReady == other.IsReady;
}

}

Additional Context

The log is: "The type 'Unity.Collections.FixedString32Bytes' cannot be used as type parameter 'T' in the generic type or method 'BufferSerializer.SerializeValue(ref T, FastBufferWriter.ForPrimitives)'. There is no boxing conversion from 'Unity.Collections.FixedString32Bytes' to 'System.IComparable'."

Expected Outcome

Next I try initialize NetworkList type of LobbyPlayerState:

private NetworkList lobbyPlayers;

Environment

pwaradowx commented 2 years ago

Since Netcode for GameObjects version 1.0.0-pre.8 you should use ForceNetworkSerializeByMemcpy(FixedString32Bytes) instead of just FixedString32Bytes. This behaviour also exist in version 1.0.0-pre.9. In future versions it can be brought back to just using FixedString. FixedString32Bytes should be transferred as a generic parameter to ForceNetworkSerializeByMemcpy.