/// <summary> /// Creates a room but fails if this room is existing already. Can only be called on Master Server. /// </summary> /// <remarks> /// When successful, this calls the callbacks OnCreatedRoom and OnJoinedRoom (the latter, cause you join as first player). /// If the room can't be created (because it exists already), OnPhotonCreateRoomFailed gets called. /// /// If you don't want to create a unique room-name, pass null or "" as name and the server will assign a roomName (a GUID as string). /// /// Rooms can be created in any number of lobbies. Those don't have to exist before you create a room in them (they get /// auto-created on demand). Lobbies can be useful to split room lists on the server-side already. That can help keep the room /// lists short and manageable. /// If you set a typedLobby parameter, the room will be created in that lobby (no matter if you are active in any). /// If you don't set a typedLobby, the room is automatically placed in the currently active lobby (if any) or the /// default-lobby. /// /// Call this only on the master server. /// Internally, the master will respond with a server-address (and roomName, if needed). Both are used internally /// to switch to the assigned game server and roomName. /// /// PhotonNetwork.autoCleanUpPlayerObjects will become this room's autoCleanUp property and that's used by all clients that join this room. /// </remarks> /// <param name="roomName">Unique name of the room to create. Pass null or "" to make the server generate a name.</param> /// <param name="roomOptions">Common options for the room like maxPlayers, initial custom room properties and similar. See RoomOptions type..</param> /// <param name="typedLobby">If null, the room is automatically created in the currently used lobby (which is "default" when you didn't join one explicitly).</param> /// <param name="expectedUsers"></param> private static bool CreateRoom(string roomName, RoomOptions roomOptions, TypedLobby typedLobby, string[] expectedUsers) { if (offlineMode) { if (offlineModeRoom != null) { Debug.LogError("CreateRoom failed. In offline mode you still have to leave a room to enter another."); return false; } EnterOfflineRoom(roomName, roomOptions, true); return true; } if (networkingPeer.server != ServerConnection.MasterServer || !connectedAndReady) { Debug.LogError("CreateRoom failed. Client is not on Master Server or not yet ready to call operations. Wait for callback: OnJoinedLobby or OnConnectedToMaster."); return false; } typedLobby = typedLobby ?? ((networkingPeer.insideLobby) ? networkingPeer.lobby : null); // use given lobby, or active lobby (if any active) or none LoadbalancingPeer.EnterRoomParams opParams = new LoadbalancingPeer.EnterRoomParams(); opParams.RoomName = roomName; opParams.RoomOptions = roomOptions; opParams.Lobby = typedLobby; //opParams.ExpectedUsers = expectedUsers; return networkingPeer.OpCreateGame(opParams); }
/// <summary>Lets you either join a named room or create it on the fly - you don't have to know if someone created the room already.</summary> /// <remarks> /// This makes it easier for groups of players to get into the same room. Once the group /// exchanged a roomName, any player can call JoinOrCreateRoom and it doesn't matter who /// actually joins or creates the room. /// /// The parameters roomOptions and typedLobby are only used when the room actually gets created by this client. /// You know if this client created a room, if you get a callback OnCreatedRoom (before OnJoinedRoom gets called as well). /// </remarks> /// <param name="roomName">Name of the room to join. Must be non null.</param> /// <param name="roomOptions">Options for the room, in case it does not exist yet. Else these values are ignored.</param> /// <param name="typedLobby">Lobby you want a new room to be listed in. Ignored if the room was existing and got joined.</param> /// <returns>If the operation got queued and will be sent.</returns> public static bool JoinOrCreateRoom(string roomName, RoomOptions roomOptions, TypedLobby typedLobby) { if (offlineMode) { if (offlineModeRoom != null) { Debug.LogError("JoinOrCreateRoom failed. In offline mode you still have to leave a room to enter another."); return false; } EnterOfflineRoom(roomName, roomOptions, true); // in offline mode, JoinOrCreateRoom assumes you create the room return true; } if (networkingPeer.server != ServerConnection.MasterServer || !connectedAndReady) { Debug.LogError("JoinOrCreateRoom failed. Client is not on Master Server or not yet ready to call operations. Wait for callback: OnJoinedLobby or OnConnectedToMaster."); return false; } if (string.IsNullOrEmpty(roomName)) { Debug.LogError("JoinOrCreateRoom failed. A roomname is required. If you don't know one, how will you join?"); return false; } typedLobby = typedLobby ?? ((networkingPeer.insideLobby) ? networkingPeer.lobby : null); // use given lobby, or active lobby (if any active) or none LoadbalancingPeer.EnterRoomParams opParams = new LoadbalancingPeer.EnterRoomParams(); opParams.RoomName = roomName; opParams.RoomOptions = roomOptions; opParams.Lobby = typedLobby; opParams.CreateIfNotExists = true; opParams.PlayerProperties = player.customProperties; return networkingPeer.OpJoinRoom(opParams); }
/// <summary>Join room by roomname and on success calls OnJoinedRoom(). This is not affected by lobbies.</summary> /// <remarks> /// On success, the method OnJoinedRoom() is called on any script. You can implement it to react to joining a room. /// /// JoinRoom fails if the room is either full or no longer available (it might become empty while you attempt to join). /// Implement OnPhotonJoinRoomFailed() to get a callback in error case. /// /// To join a room from the lobby's listing, use RoomInfo.name as roomName here. /// Despite using multiple lobbies, a roomName is always "global" for your application and so you don't /// have to specify which lobby it's in. The Master Server will find the room. /// In the Photon Cloud, an application is defined by AppId, Game- and PUN-version. /// </remarks> /// <see cref="PhotonNetworkingMessage.OnPhotonJoinRoomFailed"/> /// <see cref="PhotonNetworkingMessage.OnJoinedRoom"/> /// <param name="roomName">Unique name of the room to join.</param> public static bool JoinRoom(string roomName) { if (offlineMode) { if (offlineModeRoom != null) { Debug.LogError("JoinRoom failed. In offline mode you still have to leave a room to enter another."); return false; } EnterOfflineRoom(roomName, null, true); return true; } if (networkingPeer.server != ServerConnection.MasterServer || !connectedAndReady) { Debug.LogError("JoinRoom failed. Client is not on Master Server or not yet ready to call operations. Wait for callback: OnJoinedLobby or OnConnectedToMaster."); return false; } if (string.IsNullOrEmpty(roomName)) { Debug.LogError("JoinRoom failed. A roomname is required. If you don't know one, how will you join?"); return false; } LoadbalancingPeer.EnterRoomParams opParams = new LoadbalancingPeer.EnterRoomParams(); opParams.RoomName = roomName; return networkingPeer.OpJoinRoom(opParams); }
/// <summary>Can be used to return to a room after a disconnect and reconnect.</summary> /// <remarks> /// After losing connection, you might be able to return to a room and continue playing, /// if the client is reconnecting fast enough. Use Reconnect() and this method. /// Cache the room name you're in and use ReJoin(roomname) to return to a game. /// /// Note: To be able to ReJoin any room, you need to use UserIDs! /// You also need to set RoomOptions.PlayerTtl. /// /// <b>Important: Instantiate() and use of RPCs is not yet supported.</b> /// The ownership rules of PhotonViews prevent a seamless return to a game. /// Use Custom Properties and RaiseEvent with event caching instead. /// /// Common use case: Press the Lock Button on a iOS device and you get disconnected immediately. /// </remarks> public static bool ReJoinRoom(string roomName) { if (offlineMode) { Debug.LogError("ReJoinRoom failed due to offline mode."); return false; } if (networkingPeer.server != ServerConnection.MasterServer || !connectedAndReady) { Debug.LogError("ReJoinRoom failed. Client is not on Master Server or not yet ready to call operations. Wait for callback: OnJoinedLobby or OnConnectedToMaster."); return false; } if (string.IsNullOrEmpty(roomName)) { Debug.LogError("ReJoinRoom failed. A roomname is required. If you don't know one, how will you join?"); return false; } LoadbalancingPeer.EnterRoomParams opParams = new LoadbalancingPeer.EnterRoomParams(); opParams.RoomName = roomName; opParams.RejoinOnly = true; opParams.PlayerProperties = player.customProperties; return networkingPeer.OpJoinRoom(opParams); }