Skip to content

Commit 4c9cfb5

Browse files
committed
move to inetworkserializable section
1 parent 6c43349 commit 4c9cfb5

File tree

2 files changed

+52
-52
lines changed

2 files changed

+52
-52
lines changed

com.unity.netcode.gameobjects/Documentation~/advanced-topics/serialization/inetworkserializable.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,55 @@ public struct MyStructB : MyStructA
181181
}
182182
}
183183
```
184+
185+
## Generic IEquatable network variables
186+
187+
Generic `INetworkSerializable` types with generic `IEquatable` are not supported, implemented as `public class NotSupported<T> : INetworkSerializable, IEquatable<NotSupported<T>>` where the type would be passed in during declaration like `NetworkVariable<NotSupported<int>> myVar;`.
188+
189+
The recommended workaround for this would be to create the generic class as usual but add a virtual method for handling the serialization of the type. Then wrap this generic `INetworkSerializable` in a derived class which then needs to have a serializable type defined where the implementation for the serialization is provided.
190+
191+
For example:
192+
193+
```csharp
194+
public class MyGameData<T> : INetworkSerializable
195+
{
196+
// This needs to be a serializable type according to what network variables support
197+
public T Data;
198+
199+
protected virtual void OnNetworkSerialize<T2>(BufferSerializer<T2> serializer) where T2 : IReaderWriter
200+
{
201+
}
202+
203+
public void NetworkSerialize<T2>(BufferSerializer<T2> serializer) where T2 : IReaderWriter
204+
{
205+
OnNetworkSerialize(serializer);
206+
}
207+
}
208+
209+
public class GameDataWithLong : MyGameData<long>, IEquatable<GameDataWithLong>
210+
{
211+
// Potential additional data
212+
public int AdditionalData;
213+
214+
protected virtual bool OnEquals(GameDataWithLong other)
215+
{
216+
return other.Data.Equals(other);
217+
}
218+
public bool Equals(GameDataWithLong other)
219+
{
220+
return OnEquals(other);
221+
}
222+
223+
protected override void OnNetworkSerialize<T2>(BufferSerializer<T2> serializer)
224+
{
225+
serializer.SerializeValue(ref AdditionalData);
226+
serializer.SerializeValue(ref Data);
227+
}
228+
}
229+
```
230+
231+
Then declare this network variable like so:
232+
233+
```csharp
234+
NetworkVariable<GameDataWithLong> myVar = new NetworkVariable<GameDataWithLong>();
235+
```

com.unity.netcode.gameobjects/Documentation~/basics/networkvariable.md

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -621,55 +621,3 @@ public class TestFixedString : NetworkBehaviour
621621

622622
> [!NOTE]
623623
> The above example uses a pre-set list of strings to cycle through for example purposes only. If you have a predefined set of text strings as part of your actual design then you would not want to use a FixedString to handle synchronizing the changes to `m_TextString`. Instead, you would want to use a `uint` for the type `T` where the `uint` was the index of the string message to apply to `m_TextString`.
624-
625-
## Generic IEquatable network variables
626-
627-
Generic `INetworkSerializable` types with generic `IEquatable` are not supported, implemented as `public class NotSupported<T> : INetworkSerializable, IEquatable<NotSupported<T>>` where the type would be passed in during declaration like `NetworkVariable<NotSupported<int>> myVar;`.
628-
629-
The recommended workaround for this would be to create the generic class as usual but add a virtual method for handling the serialization of the type. Then wrap this generic `INetworkSerializable` in a derived class which then needs to have a serializable type defined where the implementation for the serialization is provided.
630-
631-
For example:
632-
633-
```csharp
634-
public class MyGameData<T> : INetworkSerializable
635-
{
636-
// This needs to be a serializable type according to what network variables support
637-
public T Data;
638-
639-
protected virtual void OnNetworkSerialize<T2>(BufferSerializer<T2> serializer) where T2 : IReaderWriter
640-
{
641-
}
642-
643-
public void NetworkSerialize<T2>(BufferSerializer<T2> serializer) where T2 : IReaderWriter
644-
{
645-
OnNetworkSerialize(serializer);
646-
}
647-
}
648-
649-
public class GameDataWithLong : MyGameData<long>, IEquatable<GameDataWithLong>
650-
{
651-
// Potential additional data
652-
public int AdditionalData;
653-
654-
protected virtual bool OnEquals(GameDataWithLong other)
655-
{
656-
return other.Data.Equals(other);
657-
}
658-
public bool Equals(GameDataWithLong other)
659-
{
660-
return OnEquals(other);
661-
}
662-
663-
protected override void OnNetworkSerialize<T2>(BufferSerializer<T2> serializer)
664-
{
665-
serializer.SerializeValue(ref AdditionalData);
666-
serializer.SerializeValue(ref Data);
667-
}
668-
}
669-
```
670-
671-
Then declare this network variable like so:
672-
673-
```csharp
674-
NetworkVariable<GameDataWithLong> myVar = new NetworkVariable<GameDataWithLong>();
675-
```

0 commit comments

Comments
 (0)