synchronize components state

Hello, i think i understood how to synchronize positions using photonTransformView, but now i need to synchronize components state. F.e., i'm trying to pass turn by switching off my control script (enabled=false) and switching on other's control script (and vise versa), and it works fine offline, but network player sees no changes.
So, what exactly should i pass through the stream in OnPhotonSerializeView?

Best Answer

  • Tobias
    Tobias admin
    Accepted Answer
    Well, we can't decide that for you. We try to help with the components we came up with and general help. If you decide this doesn't fit, we can't really solve that specific case.

Answers

  • For turns, I would recommend using the Custom Room Properties. They can easily store who's turn it is.
    There are even a PunTurnManager.cs in the package which could be a basis for your case.
  • Miha4406
    Miha4406
    edited September 3
    Thanks, but I'd want to know how to make instance of player1 on other's computer know that i've disabled some component of instance of player1 on my computer. I'm currently using method for this:
    public class MouseMove1 : MonoBehaviourPunCallbacks, IPunObservable
    {
        RaycastHit hit;
        Vector3 clPos;
        Vector3 newDir;
    
        public float rotSpeed = 2f;
        public float mSpeed = 1f;
    
        public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
        {
            if (stream.IsWriting) 
            {
               //?
            }
            else 
            {
                //?
            }
        }
    
        
        void Update()
        {
            if (!GetComponent<PhotonView>().IsMine)
            {
                return;
            }
    
            if (Input.GetKeyDown(KeyCode.Return))
            {
                photonView.RPC("passTurn12", RpcTarget.All);   
            }
            
    
            Debug.DrawRay(transform.position, transform.forward*10, Color.green, 0.2f);
    
            Ray mRay = Camera.main.ScreenPointToRay(Input.mousePosition);
    
            if (Input.GetMouseButtonDown(0)) {
                
                if(Physics.Raycast(mRay, out hit))
                {
                    clPos = hit.point;                
                }           
            }
            mMove();
        } 
    
    
        void mMove()
        {    
            newDir = Vector3.RotateTowards(transform.forward, clPos, rotSpeed * Time.deltaTime, 0.0f);
            transform.rotation = Quaternion.LookRotation(newDir);
    
            transform.position = Vector3.MoveTowards(transform.position, clPos+Vector3.up, mSpeed*Time.deltaTime);
        }
    
    
        [PunRPC] public void passTurn12()
        {
            GameObject.FindGameObjectWithTag("player2").GetComponent<MouseMove2>().enabled = true;
            gameObject.GetComponent<MouseMove1>().enabled = false;
        }
    }
    
  • Well, it sounds like this derives from the turn, no?
    So if the properties tell you it's player 2's turn, then it should be clear which components 2 is using and what the others will use?
  • Player2's turn starts when player1 enables player2's control script and disables his own one.
    So now I'm using photonView.RPC("passTurn12", RpcTarget.All) to synchronize it.
    But what should I put in OnPhotonSerializeView?
  • Tobias
    Tobias admin
    Accepted Answer
    Well, we can't decide that for you. We try to help with the components we came up with and general help. If you decide this doesn't fit, we can't really solve that specific case.