com.unity.netcode.gameobjects icon indicating copy to clipboard operation
com.unity.netcode.gameobjects copied to clipboard

Cannot implement derived class from NetworkVariableSerialization as Read/Write are marked as internal

Open brainwipe opened this issue 3 years ago • 2 comments

Description

Documentation implies that you can create your own custom network variable structures such as NetworkDictionary in the community package:

DISCLAIMER The NetworkVariable and NetworkList and implementations are primarily designed as samples showing how to create NetworkVariableBase structures. The NetworkVariable<T> container is however considered production ready for simple unmanaged types.

However, you cannot override Write and Read in NetworkVariableSerialization<T> because the methods are marked as internal.

https://github.com/Unity-Technologies/com.unity.netcode.gameobjects/blob/98d190e70932f95d87a0679ac4278b0e393bd403/com.unity.netcode.gameobjects/Runtime/NetworkVariable/NetworkVariableSerialization.cs#L238

https://github.com/Unity-Technologies/com.unity.netcode.gameobjects/blob/98d190e70932f95d87a0679ac4278b0e393bd403/com.unity.netcode.gameobjects/Runtime/NetworkVariable/NetworkVariableSerialization.cs#L243

I would expect these to be public or protected.

Environment

  • Netcode Version: 1.0.0-pre.10

brainwipe avatar Jun 27 '22 13:06 brainwipe

Hi @brainwipe thanks for reporting. We will take a look and consider making them public if they don't cause any issues Or fix the documentation.

ashwinimurt avatar Jul 06 '22 19:07 ashwinimurt

Backlog MTT-4261

ashwinimurt avatar Aug 01 '22 20:08 ashwinimurt

Hi Rob Lang,

There has been some changes to those interfaces, which may have contributed to the confusion. The usage you are describing is not the intended one. We are just about to release 1.1. What I'll describe below applies to both 1.1, and current develop branch.

If you want to simply create a NetworkVariable of some new type, please use NetworkVariable<SomeNewType>. The requirement is that SomeNewType implements INetworkSerializable (or is blittable). As long as this holds, internally, the correct serialization will happen. For new types, you'll need to implement the interface yourself.

If you rather want to create a new type of network container, please derive your container from NetworkVariableBase. Then, you can implement WriteDelta, ReadDelta, WriteField and ReadField using FastBufferWriter and FastBufferReader:

        public override void WriteDelta(FastBufferWriter writer)
        {
            writer.WriteValueSafe((ushort)1); // example, write the real data to serialize
        }

Please feel free to re-open this issue, if the above explanation doesn't meet your needs.

Thanks, Jeff.

jeffreyrainy avatar Oct 11 '22 17:10 jeffreyrainy