-
Notifications
You must be signed in to change notification settings - Fork 216
update: using IEquatable with serialization #1475
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approving with minor wording tweaks
|
||
### Using with NetworkVariable | ||
|
||
In order to use an `INetworkSerializable` implementation with `NetworkVariable`, you must also implement the `IEquatable<T>` interface in order to determine whether a value has been updated or not. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In order to use an `INetworkSerializable` implementation with `NetworkVariable`, you must also implement the `IEquatable<T>` interface in order to determine whether a value has been updated or not. | |
To use an `INetworkSerializable` implementation with `NetworkVariable`, you must also implement the `IEquatable<T>` interface to determine whether a value has been updated or not. |
#### IEquatable and generics | ||
You might think it would be much simpler to have a single class that could handle all of your serialization in order to avoid replicating some of the base script requirements. | ||
|
||
This is an example of what will not work: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#### IEquatable and generics | |
You might think it would be much simpler to have a single class that could handle all of your serialization in order to avoid replicating some of the base script requirements. | |
This is an example of what will not work: | |
#### `IEquatable` and generics | |
It might seem simpler to have a single class that handles all of your serialization to avoid replicating some of the base script requirements, however there are some issues with this implementation. | |
The following is an example of what won't work: |
There are two issues with the above approach: | ||
- The Netcode for GameObjects ILPP script will be looking for the actual `IEquatable<MyBaseSerializer<int>>` and throw an error. | ||
- The generic `T` is nullable and cannot be nullable. | ||
|
||
However, there are some things you can do if you would like to use this kind of design pattern. The alternative is to not try and make the base class implement `IEquatable` but to implement it relative to each derived class. | ||
|
||
One example of this would be: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are two issues with the above approach: | |
- The Netcode for GameObjects ILPP script will be looking for the actual `IEquatable<MyBaseSerializer<int>>` and throw an error. | |
- The generic `T` is nullable and cannot be nullable. | |
However, there are some things you can do if you would like to use this kind of design pattern. The alternative is to not try and make the base class implement `IEquatable` but to implement it relative to each derived class. | |
One example of this would be: | |
There are two issues with the approach above: | |
- The Netcode for GameObjects ILPP script will be looking for the actual `IEquatable<MyBaseSerializer<int>>` and throw an error. | |
- The generic `T` is nullable and cannot be nullable. | |
However, there are some things you can do if you want to use this kind of design pattern. The alternative is to not try and make the base class implement `IEquatable` but to implement it relative to each derived class. | |
For example: |
} | ||
``` | ||
|
||
Where `MyExtendedData` implements `IEquatable<MyExtendedData>` and overriding the `OnNetworkSerialize` allows one to stack serialization in an ascending or descending order. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where `MyExtendedData` implements `IEquatable<MyExtendedData>` and overriding the `OnNetworkSerialize` allows one to stack serialization in an ascending or descending order. | |
Where `MyExtendedData` implements `IEquatable<MyExtendedData>` and overriding the `OnNetworkSerialize` allows you to stack serialization in an ascending or descending order. |
|
||
::: caution | ||
|
||
While the above example is using a class it is recommended to stick with using `struct` when implementing `INetworkSerializable` due to its lower memory footprint and it helps to avoid other serialization related issues when using a class. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While the above example is using a class it is recommended to stick with using `struct` when implementing `INetworkSerializable` due to its lower memory footprint and it helps to avoid other serialization related issues when using a class. | |
The example above uses a class, but it's generally recommended to use a `struct` when implementing `INetworkSerializable` due to its lower memory footprint. It also helps to avoid other serialization-related issues when using a class. |
Adding some additional information about using IEquatable with serialization.
Select the type of change:
Purpose of the Pull Request:
To provide additional information about using IEquatable with INetworkSerializable.
Issue Number:
MTTB-1306