FishNet icon indicating copy to clipboard operation
FishNet copied to clipboard

SyncVars of Structs Do not Serialize NetworkBehaviours for Late Joiners.

Open alecpizz opened this issue 3 years ago • 6 comments

General Unity version: 2021.3.17 Fish-Networking version: 3.7.7

Description When attempting to serialize a custom struct using the sync var attribute, if the syncvar has a networkbehaviour or something that extends networkbehaviour, that variable will not be synced for late joiners (field will be null). This issue appears to have originated sometime after 3.6.9 (we're upgrading from 3.5.8).

Replication Steps to reproduce the behavior:

  1. Attach this script to a gameObject. https://pastebin.com/xyMTd1wR
  2. Start a host/client match, modify the 'behaviour' field of the struct syncvar somehow.
  3. Join with another client.
  4. Inspect the gameObject, see that the 'behaviour' field is null.

Expected behavior The networkbehaviour is not null and is useable.

alecpizz avatar Jul 04 '23 00:07 alecpizz

Bump. Observed similar problematic behavior in 3.7.10 Pro.

quentinleon avatar Jul 10 '23 18:07 quentinleon

Bump again, am I doing something wrong? I would really like to get some of the performance benefits of the newer fishnet.

alecpizz avatar Aug 08 '23 20:08 alecpizz

Didn't forget this is just a low priority atm, sorry.

FirstGearGames avatar Aug 08 '23 22:08 FirstGearGames

I'm going to review this issue after 3.11.0; ideally the release after.

FirstGearGames avatar Sep 11 '23 16:09 FirstGearGames

Status updated to priority.

FirstGearGames avatar Sep 17 '23 15:09 FirstGearGames

Unable to reproduce.

Setup:

  • Add NetworkManager prefab, ensure auto start is Server or disabled.
  • Add a scene object and attach StructureSV script (see below).
  • Make a new prefab and add EmptyNetworkBehaviour. Set as OtherPrefab in added StructureSV component.
  • Start one editor as server, and one as client.
    public class StructureSV : NetworkBehaviour
    {
        public NetworkObject OtherPrefab;

        public struct MyData
        {
            public bool Set;
            public NetworkObject Nob;
            public NetworkBehaviour Nb;
        }

        [SyncVar]
        private MyData _serverStartMd;
        [SyncVar]
        private MyData _spawnMd;


        public override void OnStartServer()
        {
            NetworkObject n = Instantiate(OtherPrefab);
            base.Spawn(n);

            _serverStartMd = new MyData()
            {
                Set = true,
                Nob = n,
                Nb = n.GetComponent<NetworkBehaviour>()
            };
        }

        public override void OnSpawnServer(NetworkConnection connection)
        {
            NetworkObject n = Instantiate(OtherPrefab);
            base.Spawn(n);
            _spawnMd = new MyData()
            {
                Set = true,
                Nob = n,
                Nb = n.GetComponent<NetworkBehaviour>()
            };
        }

        public override void OnStartClient()
        {
            StartCoroutine(__PrintMD(0f));
            StartCoroutine(__PrintMD(0.5f));
        }

        private IEnumerator __PrintMD(float delay)
        {
            if (delay > 0f)
                yield return new WaitForSeconds(delay);

            Debug.Log($"ServerStartMd Check: Delay {delay}. Set {_serverStartMd.Set}. Nob null? {_serverStartMd.Nob == null}. Nb null? {_serverStartMd.Nb == null}.");
            Debug.Log($"SpawnMd Check: Delay {delay}. Set {_spawnMd.Set}. Nob null? {_spawnMd.Nob == null}. Nb null? {_spawnMd.Nb == null}.");
        }
    }

Expected behavior: When client starts ServerStartMd will be set and Nob/Nb will not be null. This is expected because the values were set before the object was spawned in for the client. When client starts SpawnMd will be unset and have null Nob/Nb until the delay of 0.5f(as shown in script) passes. This is expected because the values are set in OnServerSpawn, so they are sent after the client calls OnStartClient. In result the client gets the values later and should show set after the 0.5f delay.

See screenshot for local results. image

Closing issue until I receive a test project / further information. Please re-open at that time.

FirstGearGames avatar Sep 22 '23 17:09 FirstGearGames