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

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:
1462726--79920--$1.JPG

with the SetBool Function looking like this:
1462726--79921--$2.JPG

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.

Still lost… been working on this for a few hours…

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.

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).

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:

void OnJoinedRoom()
{ 
if (PhotonNetwork.countOfPlayers == 2) 
{
// do something
// remember you already instantiated your avatars!
instantiatedAvatars = true;
// close the room
}
}

void OnPhotonPlayerConnected(PhotonPlayer newPlayer)
{
if (instantiatedAvatars == false  PhotonNetwork.countOfPlayers == 2) 
{
// do something
// close the room
}
}

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.

2 Likes