Skip to content

Commit 7915bd4

Browse files
fix: Added support for byte on NetworkVariable through codegen (#2953)
* fix: Added support for byte on NetworkVariable through codegen the codegen wasn't picking byte - it's because a variable of a type other than NetworkVariable<byte> (such as NetworkVariable<FixedString32Bytes>) is serializing a byte value as part of its delta serialization. Delta serialization was added in NGO 1.9, which is why it doesn't happen in previous versions. This code assumes the existence of a byte serializer, but if the user doesn't have a NetworkVariable<byte> in their code, the byte serializer won't be initialized by the codegen pass. credits: Kitty Drapper * fix: Converted serialization operation to their appropriate Safe counterparts Fixed safety issue that would trigger exception on runtime * fix Using WriteByteSafe as opposed to WriteValueSafe. * update adding changelog entry --------- Co-authored-by: NoelStephensUnity <[email protected]>
1 parent cd154af commit 7915bd4

File tree

4 files changed

+5
-2
lines changed

4 files changed

+5
-2
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
1515

1616
### Fixed
1717

18+
- Fixed issue where internal delta serialization could not have a byte serializer defined when serializing deltas for other types. Added `[GenerateSerializationForType(typeof(byte))]` to both the `NetworkVariable` and `AnticipatedNetworkVariable` classes to assure a byte serializer is defined. (#2953)
1819
- Fixed issue with the client count not being correct on the host or server side when a client disconnects itself from a session. (#2941)
1920
- Fixed issue with the host trying to send itself a message that it has connected when first starting up. (#2941)
2021
- Fixed issue where in-scene placed NetworkObjects could be destroyed if a client disconnects early and/or before approval. (#2923)

com.unity.netcode.gameobjects/Runtime/NetworkVariable/AnticipatedNetworkVariable.cs

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public enum StaleDataHandling
5151
#pragma warning restore IDE0001
5252
[Serializable]
5353
[GenerateSerializationForGenericParameter(0)]
54+
[GenerateSerializationForType(typeof(byte))]
5455
public class AnticipatedNetworkVariable<T> : NetworkVariableBase
5556
{
5657
[SerializeField]

com.unity.netcode.gameobjects/Runtime/NetworkVariable/NetworkVariable.cs

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace Unity.Netcode
99
/// <typeparam name="T">the unmanaged type for <see cref="NetworkVariable{T}"/> </typeparam>
1010
[Serializable]
1111
[GenerateSerializationForGenericParameter(0)]
12+
[GenerateSerializationForType(typeof(byte))]
1213
public class NetworkVariable<T> : NetworkVariableBase
1314
{
1415
/// <summary>

com.unity.netcode.gameobjects/Runtime/NetworkVariable/NetworkVariableSerialization.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,7 @@ public unsafe void WriteDelta(FastBufferWriter writer, ref T value, ref T previo
717717
writer.WriteValueSafe(value);
718718
return;
719719
}
720-
writer.WriteByte(0);
720+
writer.WriteByteSafe(0);
721721
BytePacker.WriteValuePacked(writer, value.Length);
722722
writer.WriteValueSafe(changes);
723723
unsafe
@@ -766,7 +766,7 @@ public unsafe void ReadDelta(FastBufferReader reader, ref T value)
766766
{
767767
if (changes.IsSet(i))
768768
{
769-
reader.ReadByte(out ptr[i]);
769+
reader.ReadByteSafe(out ptr[i]);
770770
}
771771
}
772772
}

0 commit comments

Comments
 (0)