Skip to content

When Overloading FastBuffer methods, The wrong reflection is called for List<T> #1582

@WeLikeIke

Description

@WeLikeIke

Describe the bug
While trying to implement custom serialization of data to be given to Rpcs, I found myself implementing methods:

public static void ReadValueSafe(this FastBufferReader reader, out List<int> value);
public static void WriteValueSafe(this FastBufferWriter writer, in List<int> value);

public static void ReadValueSafe(this FastBufferReader reader, out List<string> value);
public static void WriteValueSafe(this FastBufferWriter writer, in List<string> value);

However when invoked by Rpcs that use List<int> and List<string>, only one at the time works.

Specifically,

  • if both implementations are present, the List<int> overload will be called for both, leading to an error when using List<string>.
  • if only the List implementation is present, then you cannot call Rpcs that use List<string>, but List<int> ones are fine.
  • if only the List implementation is present, then you cannot call Rpcs that use List<int>, but List<string> ones are fine.

I have attached a test scene that has a 100% reproducibility rate.

BadReflectionIssue.zip

To Reproduce
Steps to reproduce the behavior:

  1. Open UnityEditor with the latest Netcode Package.
  2. Drag and Drop the Scene provided in the attachment.
  3. Build and Run.
  4. In the Editor, Start the runtime and click on the "Start Server" button.
  5. In the Build, click on the "Start Client" button.
  6. In the Build, click on the "Send Int List" button and then on the "Send String List" button.
  7. Observe the Console.

Actual outcome

[Netcode-Server Sender=0] The ReadValueSafe overload for List<int> has just been called
[ServerRpc]: Received int list containing: |313|339|335|
[Netcode-Server Sender=0] The ReadValueSafe overload for List<int> has just been called
Unhandled RPC Exception: System.NullReferenceException: Object reference not set to an instance of an object

Expected outcome

[Netcode-Server Sender=0] The ReadValueSafe overload for List<int> has just been called
[ServerRpc]: Received int list containing: |313|339|335|
[Netcode-Server Sender=0] The ReadValueSafe overload for List<string> has just been called
[ServerRpc]: Received int list containing: |uqb|lzy|dwa|

Environment:

  • OS: Windows 10
  • Unity Version: 2020.3.15f2
  • Netcode Version: 1.0.0-pre.4

Additional context
Note that my serialization methods work when used exclusively. It looks like a Unity reflection problem and I encourage you to comment out the overload for List<int> to see that, in the next build, the "Send String List" button would fill the console correctly.

Also, the numbers and letters in the output are irrelevant, they are randomly generated

Metadata

Metadata

Assignees

Labels

priority:mediumThis issue has medium priority and may take some time to be resolvedstat:importedStatus - Issue is tracked internally at Unitytype:bugBug Report

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions