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.12k stars 430 forks source link

NetworkVariable<FixedStringXBytes> sync issue on Ownership change #2981

Open Fofanius opened 1 month ago

Fofanius commented 1 month ago

Description

We faced problem with syncing FixedStringXBytes of the NetworkBehaviour when changing ownership of the corresponding NetworkObject

There are no probmlems with other supported type (float, bool, INetworkSerializable struct)

Reproduce Steps

  1. Add next component to the project:
using Unity.Collections;
using Unity.Netcode;
using UnityEngine;

public class Sample : NetworkBehaviour
{
    [SerializeField] private NetworkVariable<FixedString64Bytes> _string1 = new NetworkVariable<FixedString64Bytes>(string.Empty);
    [SerializeField] private NetworkVariable<FixedString64Bytes> _string2 = new NetworkVariable<FixedString64Bytes>(string.Empty);

    public override void OnNetworkSpawn()
    {
        if(IsServer)
        {
            _string1.Value = "QWERTY";
            _string2.Value = "QWERTYASDFG";
        }
    }
}
  1. Add this component to any NetworkObject on scene
  2. Start Host/Server (optionaly connect with a few clients; clients count doesn't matter)
  3. Change the ownership of the object (you can use server itself as an argument)
  4. Observe OverflowException: Attempted to write without first calling TryBeginWrite() each frame

Actual Outcome

Error each frame on server side:

OverflowException: Attempted to write without first calling TryBeginWrite()
Unity.Netcode.FastBufferWriter.WriteByte (System.Byte value) (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.9.1/Runtime/Serialization/FastBufferWriter.cs:655)
Unity.Netcode.FixedStringSerializer`1[T].WriteDelta (Unity.Netcode.FastBufferWriter writer, T& value, T& previousValue) (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.9.1/Runtime/NetworkVariable/NetworkVariableSerialization.cs:720)
Unity.Netcode.NetworkVariableSerialization`1[T].WriteDelta (Unity.Netcode.FastBufferWriter writer, T& value, T& previousValue) (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.9.1/Runtime/NetworkVariable/NetworkVariableSerialization.cs:1642)
Unity.Netcode.NetworkVariable`1[T].WriteDelta (Unity.Netcode.FastBufferWriter writer) (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.9.1/Runtime/NetworkVariable/NetworkVariable.cs:198)
Unity.Netcode.NetworkVariableDeltaMessage.Serialize (Unity.Netcode.FastBufferWriter writer, System.Int32 targetVersion) (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.9.1/Runtime/Messaging/Messages/NetworkVariableDeltaMessage.cs:111)
Unity.Netcode.NetworkBehaviour.NetworkVariableUpdate (System.UInt64 targetClientId, System.Int32 behaviourIndex) (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.9.1/Runtime/Core/NetworkBehaviour.cs:909)
Unity.Netcode.NetworkBehaviour.VariableUpdate (System.UInt64 targetClientId) (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.9.1/Runtime/Core/NetworkBehaviour.cs:860)
Unity.Netcode.NetworkBehaviourUpdater.NetworkBehaviourUpdate () (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.9.1/Runtime/Core/NetworkBehaviourUpdater.cs:57)
Unity.Netcode.NetworkBehaviourUpdater.NetworkBehaviourUpdater_Tick () (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.9.1/Runtime/Core/NetworkBehaviourUpdater.cs:129)
Unity.Netcode.NetworkTickSystem.UpdateTick (System.Double localTimeSec, System.Double serverTimeSec) (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.9.1/Runtime/Timing/NetworkTickSystem.cs:102)
Unity.Netcode.NetworkTimeSystem.UpdateTime () (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.9.1/Runtime/Timing/NetworkTimeSystem.cs:141)
Unity.Netcode.NetworkManager.NetworkUpdate (Unity.Netcode.NetworkUpdateStage updateStage) (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.9.1/Runtime/Core/NetworkManager.cs:57)
Unity.Netcode.NetworkUpdateLoop.RunNetworkUpdateStage (Unity.Netcode.NetworkUpdateStage updateStage) (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.9.1/Runtime/Core/NetworkUpdateLoop.cs:192)
Unity.Netcode.NetworkUpdateLoop+NetworkPreUpdate+<>c.<CreateLoopSystem>b__0_0 () (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.9.1/Runtime/Core/NetworkUpdateLoop.cs:239)

Expected Outcome

Same behaviour as for other supported types

Environment

NoelStephensUnity commented 1 month ago

Hi @Fofanius,

I think this could have been resolved in #2953. Could you update your project's Packages\Manifest.json com.unity.netcode.gameobjects entry with the below modifications and let me know if you no longer experience this issue?

"com.unity.netcode.gameobjects": "https://github.com/Unity-Technologies/com.unity.netcode.gameobjects.git?path=com.unity.netcode.gameobjects#develop",