Skip to content

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

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

NoelStephensUnity
Copy link
Contributor

Adding some additional information about using IEquatable with serialization.

Select the type of change:

  • Small Changes - Typos, formatting, slight revisions
  • New Content - New features, sections, pages, tutorials
  • Site and Tools - Updates, maintenance, and new packages for the site and Docusaurus

Purpose of the Pull Request:
To provide additional information about using IEquatable with INetworkSerializable.

Issue Number:
MTTB-1306

Adding some additional information about using IEquatable with serialization.
Copy link
Collaborator

@jabbacakes jabbacakes left a 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.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

Comment on lines +229 to +232
#### 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:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#### 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:

Comment on lines +256 to +262
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:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants