I’m creating a multiplayer turn based game in which 2 players will be playing against each other. One will be server and the other will connect to it. There will be multiple instances of the game running at the same time.
connectionError = Network.InitializeServer (1, 25001, !Network.HavePublicAddress ());
if (connectionError == NetworkConnectionError.TooManyConnectedPlayers) {
DebugText.text += "/nToo many connected players";
}
else
{
MasterServer.RegisterHost (gameType, gameName, "helloo, this is a comment.");
}
My problem is whenever 2 players are connected and are playing, when a third one tries to connect, it shows that host is available and so it waits for the server to accept. I don’t want that. When the connection has got the maximum players, the host should be unavailable. How can I do that? I’m doing it using default Unity networking and Master Server (which I’ll host on my own).
I’m not entirely sure if it’s the best way to go about it, but one way I ‘disabled’ a host, was by adding an if statement in the section where I display the match information and the Join button. That if statement would check if the number of connected players in the hostdata was less than the player limit.
Here’s an example:
if (hostData != null)
{
for (int i = 0; i < hostData.Length; i++)
{
// If the game session is at max capacity, don't display it in the list of games.
if (hostData_.connectedPlayers < hostData*.playerLimit)*_
* {*
* GUILayout.BeginHorizontal(“box”, GUILayout.Height(25));*
* if (GUILayout.Button(“Join”, GUILayout.Height(25), GUILayout.Width(50)))*
* {*
* // Ensure that the player can’t join a game with an empty name. If the player left the name field*
* // empty, assign “Player 2” to the playerName variable.*
* if(playerName == “”)*
* {*
* playerName = “Player 2”; *
* } *
* //If the player has a name that isn’t empty then attempt to join*
* //the server.*
* if(playerName != “”)*
* {*
_ // Connect to a server contained in the hostData*.*_
_ Network.Connect(hostData*);*_
* PlayerPrefs.SetString(“playerName”, playerName);*
* // Call the function to apply the playerName across the Network and to other scripts.*
* StartCoroutine(ChangePlayer2Name());*
* }*
* }*
* GUILayout.Space(10);*
_ GUILayout.Label(hostData*.gameName, GUILayout.Height(25));
GUILayout.EndHorizontal();
}
}
}*_