Creating a Lobby System
This past week has presented the greatest challenges in my game development journey on this project so far.
As part of our networking solution, we decided to go with PUN 2. This enabled us to create rooms and enter matches on numerous instances of the game at once without crashes. Furthermore, another reason why we chose to go with Photon is that even when the host exits the match early, clients are still able to continue the round. In order to tackle the process of how to do this SAEED BASHEER and I created a rough flow diagram together regarding the steps that would be required within the lobby system code using Lucidchart.com. Here's a screenshot of the flow diagram we created as part of our plan.
I have currently created a very basic lobby system with the help of a video by 'Blackthornprod' on YouTube. Players can enter their desired nickname and create a room. Each room can hold up to four players, however the game can run as long as you have at least one opponent to go up against. We decided to set a four player limit since that was recommended to us by the design team based on the map size being created. This will also help to balance out matches and gameplay.
In my lobby system, players are able to see their opponents select their vehicles before the game begins along with their usernames. The host of the match is able to then start the game. In order to add bonus functionality to the game, I also created a back button so that players are able to exit the room they have joined and return back to the lobby. Here is a clip of the current functionality of the lobby system.
As you can see at the end of the video the vehicles the players have selected currently do not match up with the images - this will be resolved later when I finish remodelling the wheels in blender for all vehicles and importing them into unity. Additionally, later on I will implement an authentication system to players to enter the game with a username and password.
Below you can see the 'ConnectToServer' script I created. It is called when clicking the connect button after a player enters their username, allowing for photon to run. Players are only able to enter the server once they have created a username that is at least one character long.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using TMPro;
public class ConnectToServer : MonoBehaviourPunCallbacks
{
public TMP_InputField usernameInput;
public TMP_Text buttonText;
public void OnClickConnect()
{
if (usernameInput.text.Length >= 1)
{
PhotonNetwork.NickName = usernameInput.text;
buttonText.text = "Connecting...";
PhotonNetwork.AutomaticallySyncScene = true;
PhotonNetwork.ConnectUsingSettings();
}
}
public override void OnConnectedToMaster()
{
SceneManager.LoadScene("Lobby");
}
}
I had also created a lobby manager script for the lobby scene which instantiated labelled rooms with an OnClickCreate event I had created. The created room is displayed for all players within the lobby scene.
For most of the testing I was only using a single camera within the main game scene which was parented to no one. However after parenting the camera to the player vehicles, I noticed issues with camera ownership when players joined the match. For some reason all player views would reflect the position of the newest person to enter the lobby. I realised this was an issue regarding the photon view and therefore with the help of ThirteeNov Coding Vlog on YouTube, I was able to resolve this issue by creating a ‘DeactiveIfNotMine’ script.
Conclusion
As part of my bucket list I hope to upgrade to a more complex lobby system with an option for team mode and free for all. Additionally, I would like to connect the game to an sql database in order to store player login information - and hopefully in game stats such as number of game wins etc. This is something I am planning to implement in future so stay tuned!