Search Unity

Question How to accomplish this [RPC] call with photonnetwork

Discussion in 'Scripting' started by JD_FVTC, Oct 19, 2020.

  1. JD_FVTC

    JD_FVTC

    Joined:
    Oct 20, 2017
    Posts:
    54
    In my LaunchAnswers [RPC] function I do a check to see if user is the master client.
    I want to spawn an array of answers in two different places in my scene depending if they are first player in the experience or second.
    This code is also spawning 16 instances of answers instead of 8. Some of the instances don't contain any text either.
    Its like the LaunchAnswers function is being called 4 times instead of once on each of the 2 players.
    Is checking for master client the right thing here? I want to know if they were the first in the room.
    Can you help straiten me out?
    Code (csharp):
    1.  
    2. //This part works
    3. public override void OnJoinedRoom()
    4.     {
    5.         if (PhotonNetwork.CurrentRoom.PlayerCount > 1)
    6.         {
    7.             this.photonView.RPC("PlayStartGameAudio", RpcTarget.AllBuffered);
    8.         }
    9.     }
    10. //this part works
    11.     [PunRPC]
    12.     public IEnumerator PlayStartGameAudio()
    13.     {
    14.         StartGame.Play();
    15.         yield return new WaitForSeconds(2.0f);
    16.         this.photonView.RPC("StartTheGame", RpcTarget.AllBuffered);
    17.     }
    18. //this works
    19.     [PunRPC]
    20.     public void StartTheGame()
    21.     {
    22.         questionNumber = 0;
    23.         scoreBoardManagr_player1.ResetScore();
    24.         scoreBoardManagr_player2.ResetScore();
    25.         theCurrentQuestion = gameObject.GetComponent<GetTheQuestions>().GetTheQuestion(questionNumber);
    26.         //SHow the question
    27.         int i = 0;
    28.         foreach (TextMeshPro questionboard in questionboards)
    29.         {
    30.             questionboards[i].SetText(theCurrentQuestion.Text);
    31.             i++;
    32.         }
    33.         this.photonView.RPC("LaunchAnswers", RpcTarget.AllBuffered);
    34.     }
    35. //Issues with this function
    36.     [PunRPC]
    37.     public IEnumerator LaunchAnswers()
    38.     {
    39.         answers.Clear();
    40.         CancelInvoke();
    41.         int answerCount = 0;
    42.         var angle = .01f;
    43.         if (PhotonNetwork.IsMasterClient)
    44.         {
    45.             foreach (GetTheQuestions.Answer answer in theCurrentQuestion.Answers)
    46.             {
    47.                 answerGo = PhotonNetwork.Instantiate(answerfab.name, launcher_Player1[answerCount].transform.position, Quaternion.identity);
    48.                 answerGo.GetComponent<TextMeshPro>().text = theCurrentQuestion.Answers[answerCount].Text + "_Master";
    49.                 answers.Add(answerGo);
    50.                 answerCount++;
    51.                 answerGo.GetComponent<RotateAroundAnObject>().angle += angle;
    52.                 angle += .1f;
    53.                 yield return new WaitForSeconds(0.1f);
    54.             }
    55.             InvokeRepeating("GetAndRemoveBadAnswer", 7, 7F);
    56.         }
    57.         else
    58.         {
    59.             foreach (GetTheQuestions.Answer answer in theCurrentQuestion.Answers)
    60.             {
    61.                 answerGo = PhotonNetwork.Instantiate(answerfab.name, launcher_Player2[answerCount].transform.position, Quaternion.Inverse(Quaternion.identity));
    62.                 answerGo.GetComponent<TextMeshPro>().text = theCurrentQuestion.Answers[answerCount].Text + "_Sub";
    63.                 answers.Add(answerGo);
    64.                 answerCount++;
    65.                 answerGo.GetComponent<RotateAroundAnObject>().angle += angle;
    66.                 angle += .1f;
    67.                 yield return new WaitForSeconds(0.1f);
    68.             }
    69.             InvokeRepeating("GetAndRemoveBadAnswer", 7, 7F);
    70.         }
    71.     }
     
  2. Cyber-Dog

    Cyber-Dog

    Joined:
    Sep 12, 2018
    Posts:
    352
    Where is your check to see if you are the master client? I can't see it in your code.
    From this it looks like when both players join, they tell everyone to call the script. Meaning each player calls the function twice.
     
  3. JD_FVTC

    JD_FVTC

    Joined:
    Oct 20, 2017
    Posts:
    54
    I check for master here. What am i doing wrong that causes it to be called twice?
    Code (CSharp):
    1.  [PunRPC]
    2.     public IEnumerator LaunchAnswers()
    3.     {
    4.         answers.Clear();
    5.         CancelInvoke();
    6.         int answerCount = 0;
    7.         var angle = .01f;
    8.         if (PhotonNetwork.IsMasterClient)
    9.         {
     
  4. Cyber-Dog

    Cyber-Dog

    Joined:
    Sep 12, 2018
    Posts:
    352
    OnJoinedRoom() is being called to both players right.

    So that means both players will call the RPC function LaunchAnswers(), which will then esentially run twice.

    So...

    Player one enters, he calls LaunchAnswers() for himself. He then tells the other players to call LaunchAnswers().

    So right now both player one and player two have called LaunchAnswers()

    When Player Two calls OnJoinedRoom(), he is then going to call LaunchAnswers() both on himself and send that to Player one.

    See whats happening....

    Also. Ass its PhotonNetwork.Instantiate(), your essentially getting twice the ammount of spawns ontop of your duplicate calls.


    -----

    What you should be doing. Is checking if master on OnJoinedRoom(), and have the master spawn the game objects for both players using PhotonNetwork.Instantiate().


    Hope this help :)
     
  5. JD_FVTC

    JD_FVTC

    Joined:
    Oct 20, 2017
    Posts:
    54
    Thanks for your help. I got it down to 8 answers 4 per player which is great!

    However now the text for the 8 answers only shows on the master client and not on the second players view.
    The spawned answer objects are in the second players scene but the text isn't synced. Just shows the default placeholder text not the answers like the master shows.

    Does my Photon View need to observe the text component? I tried that but it wouldn't stick in GUI for observed components.

    Code (CSharp):
    1.  
    2.    [PunRPC]
    3.     public void StartTheGame()
    4.     {
    5.         if (PhotonNetwork.IsMasterClient)
    6.         {
    7.             questionNumber = 0;
    8.             scoreBoardManagr_player1.ResetScore();
    9.             scoreBoardManagr_player2.ResetScore();
    10.             theCurrentQuestion = gameObject.GetComponent<GetTheQuestions>().GetTheQuestion(questionNumber);
    11.      
    12.             int i = 0;
    13.             foreach (TextMeshPro questionboard in questionboards)
    14.             {
    15.                 questionboards[i].SetText(theCurrentQuestion.Text);
    16.                 i++;
    17.             }
    18.  
    19.             this.photonView.RPC("LaunchAnswers", RpcTarget.AllViaMaster);
    20.         }
    21.     }
    22.     [PunRPC]
    23.     public void LaunchAnswers()
    24.     {
    25.         if (PhotonNetwork.IsMasterClient)
    26.         {
    27.  
    28.             int answerCount = 0;
    29.             var angle = .01f;
    30.             debugger.text += "Is mine";
    31.             foreach (GetTheQuestions.Answer answer in theCurrentQuestion.Answers)
    32.             {
    33.  
    34.                 answerGo = PhotonNetwork.Instantiate(answerfab.name, launcher_Player1[answerCount].transform.position, Quaternion.identity);
    35.                 answerGo.GetComponent<TextMeshPro>().text = theCurrentQuestion.Answers[answerCount].Text ;
    36.                 answers.Add(answerGo);
    37.               answerGo.GetComponent<RotateAroundAnObject>().angle += angle;
    38.  
    39.                 answerGop2 = PhotonNetwork.Instantiate(answerfab.name, launcher_Player2[answerCount].transform.position, Quaternion.Inverse(Quaternion.identity));
    40.                answerGop2.GetComponent<TextMeshPro>().text = theCurrentQuestion.Answers[answerCount].Text;
    41.                 answers.Add(answerGop2);
    42.                 answerGop2.GetComponent<RotateAroundAnObject>().angle += angle;
    43.  
    44.                 answerCount++;
    45.                 angle += .1f;
    46.    
    47.             }
    48.         }
    49.         else
    50.         {
    51.             int answerCount = 0;
    52.             foreach (GameObject answer in answers)
    53.             {
    54.                 answer.GetComponent<TextMeshPro>().text = theCurrentQuestion.Answers[answerCount].Text + "_Subadded";
    55.              
    56.                 answerCount++;
    57.                 if (answerCount == 4) answerCount = 0;
    58.  
    59.             }
    60.         }
    61.      
    62.     }