Search Unity

Third Party Photon PUN2 UI data in Quiz app not syncing from master to client completely?

Discussion in 'Multiplayer' started by murtaza_imrn, Jan 4, 2023.

  1. murtaza_imrn

    murtaza_imrn

    Joined:
    Jan 4, 2017
    Posts:
    4
    I have a 2 player quiz game using PUN2 Photon in unity and I am just trying to sync both players lives string over the network for now which only gets sync from master to client and not the other way . I have followed all steps mentioned in the documentation , I am new to multiplayer could someone please help out in this maybe I am doing a basic mistake

    -> I have Photon View Component on my canvas and a script which has its reference to send RPC call.
    -> while sending lives tring over RPC call I check if PhtonView.IsMine and while populating it for oponent UI text I am checking if (!Photon.isMine)

    here is the code , please ignore the code quality for now I am just trying to sync first thing on PUN2 first then I will cleanup the code and make proper Data models and classes



    PhotonView PV;


    void Start()
    {
    Debug.Log("Level Started");
    PV = GetComponent<PhotonView>();
    }


    void Update()
    {
    Timer();
    if(PV.IsMine)
    MPUIManager.Instance.Lives.text = "Your Lives : " + totalLives.ToString();
    }


    public void UpdateLives(float lives)
    {
    PV.RPC("RPC_UpdateLives", RpcTarget.All, lives);
    }

    [PunRPC]
    void RPC_UpdateLives(float lives, PhotonMessageInfo info)
    {
    if (!PV.IsMine)
    {
    Debug.Log("Other Users Lives are " + lives);
    MPUIManager.Instance.OtherLives.text = "Opponent Lives :" + lives.ToString();
    }
    }


    I call the Update Lives function everytime a new Question is loaded and there I check again

    void LoadRandomQuestion()
    {
    /// local functionality for populating questions here and m
    }

    void MatchAns() called when a user selects any option
    {
    // functionality , if answer is wrong deduct 1 live and update on RPC
    if (PV.IsMine)
    {
    UpdateLives(totalLives);
    }
    }
    }

    Note: Also attached a screenshot in which I am trying to sync lives of both players in header.
    Anyone's help would be really appreciated , regards :)
     
  2. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,066
    Each PhotonView is controlled by one player only. For that client, PV.IsMine is true.
    You use this in an RPC method and this makes sure the method only runs on the controller of said PV. However, you likely want all client's UI updated.

    I guess, you will be better off by using Custom Properties. To update your UI, you can use properties update callbacks and each client simply loops over each player's ui elements and fetches the current values.
     
  3. murtaza_imrn

    murtaza_imrn

    Joined:
    Jan 4, 2017
    Posts:
    4
    Thank you @tobiass for your response, I think I understand what you are saying as I only have 1 photon view that is on canvas of the game so its not updating for both players also I am not instantiating any players on network so there wont be multiple photon views so cant check Photonview.Ismine properly here.

    I will try Custom Properties and check if it works in my case. Thank you