FirstGearGames / FishNet

FishNet: Unity Networking Evolved.
Other
1.38k stars 148 forks source link

SyncVar OnChange prev value incorrect on clients #800

Closed maxkratt closed 3 weeks ago

maxkratt commented 4 weeks ago

General Unity version: Unity 6 (6000.0.23f1) Fish-Networking version: 4.5.2 Discord link: https://discord.com/channels/424284635074134018/1034477094731784302/1299363640188731393

Description When a SyncVar is changed the OnChange event triggers with the previous value being the same as the next value. This only happens with asServer as false. You may need to create a blank project to replicate this as it was not happening in my old testing project.

Replication Steps to reproduce the behavior:

  1. Create new project
  2. Import FishNet
  3. Import example package
  4. Open example scene and press play
  5. Press the K key a few times to change the SyncVar
  6. The prev value will print as the same as the next value when asServer is false

Expected behavior It should be the correct previous value like happens when asServer is true.

Screenshot Screenshot 2024-10-25 162550

Example Package SyncVarIssue.zip

FirstGearGames commented 4 weeks ago

This is intended behavior as clientHost. We moved away from having separate values for clientHost as it was creating more complex code with very little benefit.

You can switch to stable mode to get the old syncVars back but at some point this will become the new default.

FirstGearGames commented 4 weeks ago

As stated above it's normal for 'asServer false' to show the same value when clientHost, in the newest SyncTypes.

ClientOnly seeing the same values however was an error.

The resolution is to go into SyncVar.cs and find this bit...

                if (!base.NetworkManager.IsServerStarted)
                    UpdateValues(nextValue);

                T prev = _value;

                InvokeOnChange(prev, nextValue, asServer: false);

Replace with this (moving prev above if check)

                T prev = _value;
                /* If also server do not update value.
                 * Server side has say of the current value. */
                /* Only update value if not server. We do not want
                 * clientHost overwriting servers current with what
                 * they just received.*/
                if (!base.NetworkManager.IsServerStarted)
                    UpdateValues(nextValue);

                InvokeOnChange(prev, nextValue, asServer: false);

Resolved in 4.5.3