I am new to Photon pun2 development, i want to create a muliplayer game where when HOST create room it will come with a random String, which the other players have to insert to get into room. But in my implementations it is not working.
Also what is difference between a room and lobby
Here’s the code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using Photon.Realtime;
public class OnlineLauncher : MonoBehaviourPunCallbacks
{
public static OnlineLauncher instance;
public string roomIDToJoin;
private byte maxPlayersPerRoom = 2;
public bool notConnected = true;
string gameVersion = "1.0";
void Awake()
{
if (instance == null )
{
instance = this;
Debug.Log("OnlineLauncher instance filled");
}
PhotonNetwork.AutomaticallySyncScene = true;
}
void Update()
{
}
public void ConnectToMasterServer()
{
PhotonNetwork.ConnectUsingSettings();
}
public void JoinRoomByRoomID(string roomID)
{
roomIDToJoin = roomID;
PhotonNetwork.JoinLobby();
}
public override void OnRoomListUpdate(List<RoomInfo> roomList)
{
foreach(RoomInfo room in roomList)
{
if(room.CustomProperties.ContainsKey("roomID") && (string)room.CustomProperties["roomID"]== roomIDToJoin)
{
PhotonNetwork.JoinRoom(room.Name);
Debug.LogError("Found the room!!");
return;
}
}
Debug.LogWarning("No room Found with given ID!!");
}
public override void OnConnectedToMaster()
{
Debug.Log("Connected to the Photon Cloud");
notConnected = false;
}
public override void OnDisconnected(DisconnectCause cause)
{
Debug.LogWarningFormat("Disconnected from cloud", cause);
}
public override void OnJoinRandomFailed(short returnCode, string message)
{
Debug.Log("Failed to join a room, creating new room!");
//Opens a new ui window to get room name and on button click starts the new room
}
public void CreateRoom(string roomName, string roomID)
{
RoomOptions roomOptions = new RoomOptions
{
MaxPlayers = maxPlayersPerRoom,
CustomRoomProperties = new ExitGames.Client.Photon.Hashtable
{
{ "roomID", roomID }
}
};
//Creates a room
Debug.Log("Room Created");
PhotonNetwork.CreateRoom(roomName, roomOptions);
PhotonNetwork.GameVersion = gameVersion;
}
}