- Home /
How to make host unavailable - Unity Networking
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).
Do you use a list of HostData to display the available matches available or do you connect manually through IP and port number?
Answer by crodriguez · Feb 17, 2015 at 07:04 AM
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[i].connectedPlayers < hostData[i].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[i].
Network.Connect(hostData[i]);
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[i].gameName, GUILayout.Height(25));
GUILayout.EndHorizontal();
}
}
}
Thanks, but this is not what I'm looking for. I want multiple pairs of players to play at the same time. For example, a game like chess. One player will check for open servers, he won't find any. So he starts a server and waits for someone to join. Another player comes and searches for servers, and he finds one. They start playing. At the same time, if another player comes, he should be able to start a new server. That's what I want to achieve. :)
Just so you're aware, that piece of code only applies to players attempting to join a game. For players who desire to create one, it would not go through this code. If that still doesn't help, then show me more of your code to see better what you're trying to do.