How can I know when the PhotonView values are done syncing after joining a room?

Options
I have a component that syncs its value through a PhotonView, thanks to the OnPhotonSerializeView method + stream.SendNext()/stream.ReceiveNext() method.

My problem is that when a player joins the room and the Start() method of the component is called, these values are not synced yet. it takes a few frames (?) before I have the MasterClient data.

I work around it by adding an arbitrary number of seconds before attempting to read the data.

Is there an event or callback that could let me know when all the data have been synced so that I don't rely on an arbitrary delay of a few seconds?

Answers

  • S_Oliver
    S_Oliver ✭✭✭
    Options

    Nope, there isn't any callback for that. You have to wait till the data is received.

  • Divone
    Options

    Is there something that I could check regularly that guarantees me that the sync is complete? Like a bool somewhere?

  • Divone
    Divone
    edited March 2020
    Options
    Okay I solved it with this code:
    private bool WasSynced = false;
    private bool Synced = false;
    
        private void Start()
        {
            // Set synced to true immediately if I don't have to receive anything from the network
            if (PhotonNetwork.IsMasterClient)
            {
                Synced = true;
            }
        }
    
        public void Update()
        {
            // Synced will only become true once the Network data is received
            if (!WasSynced && Synced)
            {
                WasSynced = true;
                 // Do whatever you need to do after the Network data is synced here
                Init();
            }
        }
    
    
        public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
        {
            if (stream.IsWriting)
            {
                stream.SendNext(Synced);
            }
            else
            {
                this.Synced = (bool)stream.ReceiveNext();
            }
        }