Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Third Party Wait to start the game until 2 players are connected? Photon Networking

Discussion in 'Multiplayer' started by Vectorizing, Dec 23, 2013.

  1. Vectorizing


    Nov 19, 2011
    So basically I have a script that loads my level and spawns my player. The level is procedurally loaded. So the first person who connects becomes the master client. If they are the master client they trigger the level to render when they connect. However if I open another instance of this game and connect, my player will just spawn in before the rendering is done. How can I make it so that the players spawn when 2 of them are connected??

    Here is what I have tried:

    Setting an int to 0 or 1 while the level is rendering by calling an RPC like so:

    with the SetBool Function looking like this:

    Then I know that I need to check if this number equals 1 when players connect. But the issue is if a player connects and the level is still rendering, it says the integer is still zero. I cannot check again if that integer equals 1 because the function OnJoinedRoom() is only called when you Join. I have tried the Update() function and had no luck either because it is called every frame.

    So what I think I need it someway to keep checking every 1 or 2 seconds if the integer is set to 1.. But I'm not sure how to go about that in networking. I'm not even sure if the other players client even accepts the current integer change, meaning if client 1 says it equals 1, how do I know that client's is getting the same thing, what if client 2 is still getting 0...

    To many things going on.....

    Sometimes I think it may be easier to write code for a lobby system, but I feel like that it is going to add a lot more headaches and I'm not 100% sure how.

    Thanks for reading I hope this makes sense.
  2. Vectorizing


    Nov 19, 2011
    Still lost.. been working on this for a few hours..
  3. unityuser1324235141


    Apr 5, 2013
    There is a variable which states the PlayerCount within Photon. I haven't used it in a while so I can't accurately say where... Try Typing "PhotonNetwork." and check what the auto-suggest says, there is a list which you can check the length of. If this is two or more, start the game.
  4. nastasache


    Jan 2, 2012
    On Unity build-in network it's a function named OnLevelWasLoaded(). Probably Photon have something like this. I think you have to do the spawn inside of this function with the condition as luckruns0ut said (PlayerCount, count of players == 2).
  5. tobiass


    Apr 7, 2009
    OnLevelWasLoaded is called when a new scene is loaded.

    What you need is to check if you are the second player that joins a room. If there is no one else, you need to wait until the second player joins.
    PUN will call void OnJoinedRoom when you enter a room and OnPhotonPlayerConnected on the other clients when someone else joins (after you did).

    In some script, implement something like this incomplete/pseudo code:

    Code (csharp):
    2. void OnJoinedRoom()
    3. {
    4. if (PhotonNetwork.countOfPlayers == 2)
    5. {
    6. // do something
    7. // remember you already instantiated your avatars!
    8. instantiatedAvatars = true;
    9. // close the room
    10. }
    11. }
    13. void OnPhotonPlayerConnected(PhotonPlayer newPlayer)
    14. {
    15. if (instantiatedAvatars == false  PhotonNetwork.countOfPlayers == 2)
    16. {
    17. // do something
    18. // close the room
    19. }
    20. }
    Edit: The methods that PUN might call (the ones you CAN implement to do something in specific situations) are listed in enum PhotonNetworkingMessage in file Enums.cs. You should check it out.
    otgretz and Dhicci like this.