MirrorNetworking / MirrorNetworking/Mirror

Constructors of custom class/struct do not use syncvar getter/setter for GO/NI/NB.

Open
#3,797 0 comments 0 reactions 2 assignees View on GitHub

@miwarnec is already working on this.

Since Mar 28, 2024.

bug Needs Research Weaver
Dominant language
C#
Stars
6.3k
Forks
870
PR merge metrics
No merged PRs in 30d

Description

Describe the bug
Normally when using a GO/NI/NB syncvar it will check the netid field first and get the object from the spawned dictionary, it will then set the real field and return the value. There seems to be a bug where if you use that field inside a class/struct before it has been set by either OnDeserialize or another method using it then it will stay null

This seems like a minor issue in Mirror since it will use the real field and not cause any errors. It will mostly cause timing issues where the GO/NI/NB field will stay null for longer than it needs to

[IMPORTANT] How can we reproduce the issue, step by step:
something like this should replicate it:

  • NB1 has field targeting NI2
  • Spawn NI1 and NI2 on server
  • Send NI1 to client
    • NB1 will store netid of NI2, but field will be null
  • Send NI2 to client
  • expected: using NB1 field will get the NI2 object from spawned dictionary
  • bug: using NB1 field will stay null when called inside class/struct constructor

Additional context
This should be the code in Mirror:
https://github.com/MirrorNetworking/Mirror/blob/f2291fc5a894eac53f8683972910c6328d52ef79/Assets/Mirror/Editor/Weaver/Processors/SyncVarAttributeAccessReplacer.cs#L135-L168

Tracking the code back, it seems like the ctor check is from UNET with no explanation.

After some testing it seems that setting default values for syncvar will cause errors when using the setter in a NetworkBehaviour constructor. So it seems like the check if only there to avoid it for NetworkBehaviour (and maybe other unity types).

This fix would be something like this, where you only skip the constructor for unity types:
https://github.com/MirageNet/Mirage/commit/2f5db3de45cd985ba6af428c9cf521dfd4b3cfc3

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.