- Home /
Unity Photon Engine JoinOrCreateGame
From my application's standpoint a room should have 2 players. When searching for a game I don't really care what other player joins it, just that we both get into a room.
When I do this:
PhotonNetwork.JoinOrCreateRoom ("RoomName", roomOptions, null);
The first two players that join get into the room appropriately but when a third or fourth player attempt to join (they should go into game 2) they both get an error saying room is full. Because the game with "RoomName" is full.
Additionally I can't really do a random number room name as all of the players end up in their own individual rooms for the most part.
What's the best way to create or join a game using Photon such that every 2 players go into their own room?
Answer by Ardaaytac · Jun 12, 2017 at 09:15 AM
Try using null rather than a specific string.You can use just PhotonNetwork.JoinRandomRoom() and if it fails than this method will be called. void OnPhotonRandomJoinFailed() { print("Joining to random room has failed!, Creating new room..."); PhotonNetwork.CreateRoom(null); }
public void SpawnPlayer (GameManager.GameType gameType)
{
switch (gameType)
{
case GameManager.GameType.TwoVersusTwo:
playerCount = 4;
break;
case GameManager.GameType.OneVersusOne:
playerCount = 2;
break;
case GameManager.GameType.AgainstAI:
playerCount = 1;
break;
default:
break;
}
StartCoroutine(LookForTotalPlayersInRoom(playerCount));
}
code from my cancelled game. You can just use a coroutine to wait for start the game. For example player selected 1v1? Join a room and wait for another player,if player count == 2, than lock the room.
You can always lock with PhotonNetwork.room.IsOpen = false; ,
IEnumerator LookForTotalPlayersInRoom(int playerCount)
{
yield return new WaitUntil(() => PhotonNetwork.playerList.Length == playerCount);//playerCount
GameManager.instance.waitingForPlayerPanel.SetActive(false);
//ROOM LOCKED
PhotonNetwork.room.IsOpen = false;
//ROOM LOCKED
GameManager.instance.matchmakingPanel.SetActive(true);
switch (playerCount)
{
case 4:
GameManager.instance.matchmakingPanelTexts[0].text = PhotonNetwork.playerList[0].NickName;
GameManager.instance.matchmakingPanelTexts[1].text = PhotonNetwork.playerList[1].NickName;
GameManager.instance.matchmakingPanelTexts[2].text = PhotonNetwork.playerList[2].NickName;
GameManager.instance.matchmakingPanelTexts[3].text = PhotonNetwork.playerList[3].NickName;
GameManager.instance.currentRoomTotalPlayers = 4;
StartCoroutine(Init2v2());
break;
case 2:
GameManager.instance.matchmakingPanelTexts[0].text = PhotonNetwork.playerList[0].NickName;
GameManager.instance.matchmakingPanelTexts[2].text = PhotonNetwork.playerList[1].NickName;
GameManager.instance.currentRoomTotalPlayers = 2;
StartCoroutine(Init1v1());
break;
case 1:
InitVSAI();
break;
default:
break;
}
}
Answer by ChristianSimon · Jun 12, 2017 at 07:55 AM
Hi,
each client can do a PhotonNetwork.JoinRandomRoom(); first. This either lets the client join an available room or results in an OnPhotonRandomJoinFailed callback which you have to implement. When this callback is executed, the clients knows, that there is no room available and he can create a new room himself. For the room name he can for example use his Name, UserID, another (unique) value or even null.
Your answer
Follow this Question
Related Questions
Connect() to 'ns.exitgames.com' failed: System.Net.Sockets.SocketException: No such host is known 1 Answer
Instantiate player cards copy online at particular positions 0 Answers
Why doesn't this work 2 Answers
How to get all created rooms and join any one of them in PHOTON 2 Answers
UNet, how to create Internet game (like via PhotonNetwork) 0 Answers