Search code examples
c#unity-game-enginemultiplayer

Creating private "rooms"?


I am trying to find out how to set a password on individual rooms on the MasterServer. There is a method passwordProtected on the hostData that is returned by the MasterServer but I cannot find out how to set one.

I could use the game comment or gamename when I am registering the server since I won't be using them anyway but that does not feel right.

If it is not possible to set a password on individual rooms then how would I create a private room for people that want to be left alone?


Solution

  • Setting a password

    The password is not sent to the Master Server, the password is set locally on the game server using the Network.incomingPassword property.

    Network.InitializeServer

    using UnityEngine;
    using System.Collections;
    
    public class ExampleClass : MonoBehaviour {
        void LaunchServer() {
            Network.incomingPassword = "HolyMoly";
            bool useNat = !Network.HavePublicAddress();
            Network.InitializeServer(32, 25000, useNat);
        }
    }
    

    Then when the clients connect, they pass in a password in the Network.connect method. See the Network class for more network-related stuff. It's pretty well documented.

    On private rooms...

    Note that having more than one "room" on a single server will not work very well. Your clients and server must be on the same scene when connected, otherwise bad things happen. The idea here is that you have one or more "hosts" creating their private rooms. Each room is registered individually on the Master Server. It's all the same game code, but they are separate servers that people can connect to. This structure also allows you to use the built in Network.incomingPassword field to password-protect your rooms.