Custom Comparers
Fish-Networking generates comparers for prediction data to perform internal optimizations, but on occasion certain types cannot have comparers automatically generated.
/* For example, this will create an error stating
* byte[] needs a custom comparer. */
public struct MoveData : IReplicateData
{
public Vector2 MoveDirection;
public byte[] CustomData;
// rest omitted..
}[CustomComparer]
public static bool CompareByteArray(byte[] a, byte[] b)
{
bool aNull = (a is null);
bool bNull = (b is null);
// Both are null.
if (aNull && bNull)
return true;
// One is null, other is not.
if (aNull != bNull)
return false;
// Not same lengths, cannot match.
if (a.Length != b.Length)
return false;
// Both not null and same length, compare bytes.
int length = a.Length;
for (int i = 0; i < length; i++)
{
// Differs.
if (a[i] != b[i])
return false;
}
// Fall through, if here everything matches.
return true;
}Last updated