This section covers the addition of features to handle the various loading of levels based on the number of players currently playing in the room.
Loading Arena Routine
We've created 4 different rooms, and we named them all with the convention that the last character is the number of players, so it's now very easy to bind the current number of players in the room and the related scene. It's a very effective technique known as "convention over configuration", a "Configuration" based approached would have been, for example, maintaining a lookup table list of the scene name for a given number of players in a room. Our scripts would have then looked in that list and be returned a scene in which the name doesn't matter at all. "Configuration" requires more scripting in general, that's why we'll go for "Convention" here which lets us get to working code faster without polluting our code with unrelated features.
Let's add a new method within a new region dedicated to private methods we'll create for the occasion. Don't forget to save
When we'll call this method, we are going to load the appropriate room, based on the PlayerCount property of the room we are in.
There are two things to watch out for here, it's very important.
- PhotonNetwork.LoadLevel() should only be called if we are the master. So we check first that we are the master using PhotonNetwork.isMasterClient. It will be the responsibility of the caller to also check for this, we'll cover that in the next part of this section.
- We use PhotonNetwork.LoadLevel() to load the level we want, we don't use Unity directly, because we want to rely on Photon to load this level on all connected clients in the room, since we've enabled PhotonNetwork.automaticallySyncScene for this Game.
Now that we have our function to load the right level, let's bind this with players connection and disconnections.
Watching Players Connection
GameManager Script is a regular MonoBehaviour, we've studied the various ways to get Photon callbacks in previous part of the tutorial, and now the
GameManager needs to listen to players connection and disconnection.
Let's implement this.
Let's add the following Photon callbacks messages and save
OnLeftRoomsignature as follow, since it is now defined in Photon.PunBehaviour
Now, we have a complete setup. Every time a player joins or leaves the room, we'll be informed, and we'll call the
LoadArena() method we've just built above.
However, we'll call
LoadArena() ONLY if we are the master using PhotonNetwork.isMasterClient.
Let's now come back to the Lobby to finally be able to load the right scene when joining a room.
Loading Arena from the Lobby
- Edit the Script
Append the following to the
- Save the Script
Let's test this, open the scene
Launcher, and run it. Click on "Play", and let the system connect and join a room.
That's it, we have now or lobby working. But if you leave the room, you'll notice that when coming back to the lobby, it automatically rejoins... oops, let's address this.
If you don't why yet, "simply" analyze the logs. I put simply in quote, because it takes practice and experience to acquire the automatism to overview an issue and know where to look and how to debug it.
Try yourself now and if you are still unable to find the source of the problem, let's do this together.
- Run the
- Hit the "Play" button, and wait until you've joined the room and that "Room for 1" is loaded
- Clear the Unity Console
- Hit "Leave Room"
- Study the Unity Console, notice that "DemoAnimator/Launcher: OnConnectedToMaster() was called by PUN" is logged
- Stop the
- Double Click on the log entry "DemoAnimator/Launcher: OnConnectedToMaster() was called by PUN" the script will be loaded and point to the line of the debug call.
- Uhmmm... so, every time we get informed that we are connected, we automatically join a JoinRandomRoom. but that's not what we want.
To fix this, we need to be aware of the context. When the User clicks on the "Play" button, we should raise a flag to be aware that the connection procedure originated from the user. Then we can check for this flag to act accordingly within the various Photon callbacks.
- Edit the script
Create a new property within the Private Variables regions
At the beginning of the
connect()method add the following
OnConnectedToMaster()method, surround the
ifstatement as follow
- Save the script
Now if we test again and run the Launcher Scene, and go back and forth between the Lobby and the Game, all is well :) In order to test the automatic syncing of Scenes, you'll need to publish the application ( publish for desktop, it's the quickest for running tests), and run it along side Unity, so you have effectively two players that will connected and join a room. If The Unity Editor creates the Room first, it will be the MasterClient and you'll be able to verify in Unity Console that you get "PhotonNetwork : Loading Level : 1" and later "PhotonNetwork : Loading Level : 2" as you connect with the published instance.
Good! we have covered a lot, but this is only half of the job... :) we need to tackle the Player itself, let's do that in the next section. Don't forget to take breaks away from the computer from time to time, to be more effective in absorbing the various concepts explained.
Don't hesitate to ask questions on the forum if you are in doubt of a particular feature or if you have trouble following the tutorial and hit an error or an issue not covered here, we'll be happy to help :)