Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Third Party Photon pun 2 tell all players present in room to execute a function

Discussion in 'Multiplayer' started by prabhurakshit, Mar 20, 2023.

  1. prabhurakshit

    prabhurakshit

    Joined:
    Jan 1, 2021
    Posts:
    2
    Hello,

    I am trying to create a multiplayer snake game using Photon Pun 2. i have a multiplayer manager script, now i want to start the game only after 2 players have joined sending a message to all the players present that they can move now. how would i achive this? here is my multiplayer manager code which checks for players, its being invoked every second:

    Code (CSharp):
    1. void CheckNumberOfPlayers()
    2.     {
    3.         if (twoPlayersPresent == false)
    4.         {
    5.             if (PhotonNetwork.CurrentRoom.PlayerCount == 2)
    6.             {
    7.                 twoPlayersPresent = true;
    8.                 view.RPC("EnableSnakeController", RpcTarget.AllViaServer);
    9.                 plsWait.gameObject.SetActive(false);
    10.             }
    11.             else
    12.             {
    13.                 plsWait.text = "Please wait for players to join";
    14.             }
    15.         }
    16.         else
    17.         {
    18.             return;
    19.         }
    20.     }
    i have an rpc funtion inside the snake script which enables the movement.
     
  2. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,079
    Use the callback OnPlayerEnteredRoom(). In it, let the Master Client start the match...
     
  3. prabhurakshit

    prabhurakshit

    Joined:
    Jan 1, 2021
    Posts:
    2
    No, my doubt is that how do i send a message to both the players that should tell them to execute a function inside their script?

    this is inside my snake script:
    Code (CSharp):
    1. [PunRPC] void EnableSnakeController()
    2.     {
    3.         canMove = true;
    4.         //PhotonNetwork.Destroy(gameObject);
    5.     }
    the function
    view.RPC("EnableSnakeController", RpcTarget.AllViaServer);
    should execute the code above which is inside the snake script.
     
  4. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,079
    Is the question how to send this? Or is it "how to execute some code when the full player count is reached"?

    Because the latter question is answered by using OnJoinedRoom() and OnPlayerEnteredRoom(). Whenever OnJoinedRoom() is being called, check if the player count is 2. If it is: start. Is it not, the client has to wait for someone else to join. PUN then calls OnPlayerEnteredRoom() when someone else joins, so then the player count should be two and .. you can execute that code directly. No need to signal this via RPC. It's just .. fluff.

    Yes, it should. On one networked object. The one that has this particular view component. Every other controller that waits to wake up won't. Because RPCs target a specific networked object / view.