: D
But then again, I need a simple way to open the maps for showcasing and testing. So, I implemented an option to join a specific room (numbered in the menu). Now I want players who just choose 'Survival' to join a non empty room if possible (otherwise, pick a random room)
It's possible to just try joining rooms (since the names are known in advance) but this isn't gonna play nice with my workflow where loading the target level and joining a room are the same thing.
So, I connect to the network from the game menu and check rooms.
Before starting a game, we need to disconnect from the server, or vary connection logic to avoid a redundant connection attempt.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class RoomSelectionHelper : MonoBehaviour { | |
public bool onMasterServer = false; | |
public bool inTheLobby = false; | |
public int roomCount = 0; | |
public int selectedIndex = -1; | |
void Start(){ PhotonNetwork.ConnectUsingSettings("v4.2"); } | |
void OnConnectedToMaster(){ | |
onMasterServer = true; | |
PhotonNetwork.JoinLobby (); | |
} | |
// You could select a room here but it should be safer | |
// To keep checking since room state change over time. | |
void OnJoinedLobby(){ inTheLobby = true; } | |
void Update(){ | |
inTheLobby = PhotonNetwork.insideLobby; | |
if (inTheLobby) SelectRoom (); | |
} | |
void SelectRoom(){ | |
RoomInfo[] rooms = PhotonNetwork.GetRoomList (); | |
roomCount = rooms.Length; | |
foreach (RoomInfo room in rooms) { | |
if (room.playerCount > 0 && room.open) { | |
if (room.playerCount < room.maxPlayers || room.maxPlayers == 0) { | |
DoSelectRoom (room.name); | |
} | |
} | |
} | |
} | |
// This part is custom. In our case we parse | |
// the room name to extract the level index. | |
void DoSelectRoom(string name){ | |
int i = name.IndexOf('-'); | |
string indexString = name.Substring (i + 1); | |
selectedIndex = int.Parse (indexString); | |
GetComponent<MainMenuActions> ().survivalLevelIndex = selectedIndex; | |
} | |
} |
Try the latest build [here]
No comments:
Post a Comment