Unity-Technologies / com.unity.netcode.gameobjects

Netcode for GameObjects is a high-level netcode SDK that provides networking capabilities to GameObject/MonoBehaviour workflows within Unity and sits on top of underlying transport layer.
MIT License
2.15k stars 435 forks source link

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

Closed brainwipe closed 2 years ago

brainwipe commented 2 years ago

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 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

ashwinimurt commented 2 years ago

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 commented 2 years ago

Backlog MTT-4261

jeffreyrainy commented 2 years ago

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.