FirstGearGames / FishNet

FishNet: Unity Networking Evolved.
Other
1.32k stars 139 forks source link

SyncVar OnChange event not firing twice for Client and Server #775

Open shelbyjuno opened 4 days ago

shelbyjuno commented 4 days ago

General Unity version: 6000.0.18f1 Fish-Networking version: 4.4.5R (does NOT repro on 4.4.4R) Discord link 1: https://discord.com/channels/424284635074134018/1034477094731784302/1284362548635893760 Discord link 2: https://discord.com/channels/424284635074134018/1034477094731784302/1284577274552647795

Description When subscribing to the OnChange event on a SyncVar, only one event (where asServer == true) is fired when acting as both server and client.

Replication Steps to reproduce the behavior:

  1. Import OnChangeBug.zip
  2. Open scene, set the Player Prefab on the PlayerSpawner to the provided Player prefab if not already assigned
  3. Start server and client
  4. Press O and P to fire SyncVar changes and note debug logs
  5. Notice that asServer only fires once.

Alternatively, create a new project and use a script such as:

public class Player : NetworkBehaviour
{
    readonly SyncVar<int> health = new SyncVar<int>(100);
    [ServerRpc] void RpcUpdateHealth(int newHealth) => health.Value = newHealth;

    private void Awake()
    {
        health.OnChange += OnHealthChange;
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.O)) RpcUpdateHealth(health.Value - 10);
        if (Input.GetKeyDown(KeyCode.P)) RpcUpdateHealth(health.Value + 10);
    }

    private void OnHealthChange(int prev, int next, bool asServer)
    {
        Debug.Log($"Health changed from {prev} to {next}. ({asServer})");
    }
}

Expected behavior When Server and Client, OnChange should fire two events, one with asServer == true and one asServer == false

Screenshots

4.4.5 4 4 5

4.4.4 4 4 4

FirstGearGames commented 3 days ago

I deleted the zip, it was too large of a project to investigate. Please check out these guidelines here and upload another if you could https://fish-networking.gitbook.io/docs/manual/guides/creating-bug-reports#guidelines

shelbyjuno commented 3 days ago

I deleted the zip, it was too large of a project to investigate. Please check out these guidelines here and upload another if you could https://fish-networking.gitbook.io/docs/manual/guides/creating-bug-reports#guidelines

My bad! I've updated the link with a small .unitypackage! OnChangeBug.zip

maxkratt commented 3 days ago

@shelbyjuno I just tried the example you made, and it's working correctly on my end. Here's a video showing it: https://github.com/user-attachments/assets/708a1996-ffde-4478-88ce-478547c63059

maxkratt commented 3 days ago

My most likely guess is your client isn't observing the object. Did you try rebuilding the scene ids from the Tools->FishNetworking toolbar menu?

shelbyjuno commented 3 days ago

@maxkratt That is extremely odd, I've recorded a video of me first trying 4.4.4 and then 4.4.5, while rebuilding scene IDs both times just to make sure there is nothing stupid I am missing:

https://github.com/user-attachments/assets/86c030a8-2f3f-4f12-8b88-d7f7c7847b3a