- Home /
How to detect number of players in a room?
Hi,
I'm making a 2-player game over the Internet.
1) How can first player create a room? 2) How can 2nd player see that room and join him? 3) no other player can join? 4) How can I know when to start the game? For this we need to know number of players in current room.
Thanks
Answer by correia55 · Feb 25, 2018 at 12:12 PM
As sugested by @FortisVenaliter, you should first follow some tutorials or read the manual. This should be enough to cover all of the questions you asked in the the description.
As for the other users who end up here because of the question in the title, you can get the number of players from the NetworkManager.
NetworkManager nm = FindObjectOfType<NetworkManager>();
nm.numPlayers
Answer by FortisVenaliter · May 04, 2016 at 05:14 PM
You should really check out the Learn section of this site. There are 18 networking tutorials there that answer all of your questions.
I've done those, but honestly they are towards beginners. What I want is intermediate tutorials to let me learn about Lobbies, game logic on server, etc.
Really unhelpful comment. There are 18 sub standard / no fit for purpose / non functional 'tutorials'. If you have such a distinct grasp on the subject, perhaps you could offer a real world solution?
Answer by meat5000 · May 07, 2016 at 09:49 AM
Here's a simple implementation. Its ripped from a script I wrote so its not concise. When my players enter the game they are added to List. The Player Setup script on each player determines if the object is the local player or remote player and changes the tag to reflect this. So the server simply Finds the objects and adds them to a List, firstly so I can keep a good handle on each player and secondly, so I can count them. In this case "Player" is the local Client on the Host machine and PlayerRemote is every other connected player. I've not shown declarations but I'm sure you'll get the idea.
@Server
function ScanForPlayers()
{
yield WaitForSeconds(1);
if(isServer)
{
this.playerList.Clear();
var myPlayer = GameObject.FindWithTag("Player");
this.playerList.Add(myPlayer);
var myObject = GameObject.FindGameObjectsWithTag("PlayerRemote");
for(var i=0;i<myObject.Length;i++)
{
this.playerList.Add(myObject[i]);
yield;
}
yield;
timer = 0; //Reset search timer after last search completes.
playerNumbers = playerList.Count;
if(playerNumbers > 0)
{
playersReady = true;
}
else { playersReady = false; }
canScan = true;
}
}