- Home /
Question by
ankitpsaraogi · Jun 04, 2020 at 03:36 PM ·
unity 5networkinglobbymatchmaking
How to create a matchmaking lobby
Hi, I have been trying to create a matchmaking lobby since a long time but I am getting some or the other error which I am unable to figure out.
I do not want to make use of database like sqlite or mysql.
I am stuck. Someone please suggest me a way to do so.
In desperation, I am attaching the entire script and relevant files of the lobby manager. Please help.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mirror;
using System;
using UnityEngine.SceneManagement;
using System.Diagnostics;
public class ConnectionManager : NetworkBehaviour
{
public GameObject PlayerClientSpace2;
public GameObject PlayerServerSpace2;
public GameObject PlayerMidSpace2;
public bool joinserver2;
public bool createserver2=true;
public bool joinclient2;
public List<int> UsedPorts = new List<int>();
int port2;
public override void OnStartServer()
{
PlayerClientSpace2 = GameObject.Find("PlayerClientSpace2");
PlayerServerSpace2 = GameObject.Find("PlayerServerSpace2");
PlayerMidSpace2 = GameObject.Find("PlayerMidSpace2");
//LogicManager = GameObject.Find("LogicManager").GetComponent<LogicManager>();
//LogicManager.createserver2 = true;
base.OnStartServer();
}
public override void OnStartClient()
{
base.OnStartClient();
if (hasAuthority)
{
if (GlobalScript.PlayerTotal == 2)
{
if (GlobalScript.StartAs == "Client" && GlobalScript.PlayerTotal==2)
{
CmdPositionme(GlobalScript.StartAs, GlobalScript.PlayerTotal);
}
else if (GlobalScript.StartAs == "Server" && GlobalScript.PlayerTotal==2)
{
if (GlobalScript.ServerMatchMakingMode == "Ended")
{
CmdRemovePort(GlobalScript.Port);
}
else
{
CmdPositionme(GlobalScript.StartAs, GlobalScript.PlayerTotal);
}
}
}
}
}
[Command]
void CmdPositionme(string StartAs, int PlayerTotal)
{
if (PlayerTotal == 2)
{
if (StartAs == "Client")
{
transform.SetParent(PlayerClientSpace2.transform);
}
else if (StartAs == "Server")
{
transform.SetParent(PlayerServerSpace2.transform);
}
}
}
[Command]
void CmdRemovePort(uint port)
{
UsedPorts.Remove(Convert.ToInt32(port));
TargetShutDownServer(connectionToClient);
}
[TargetRpc]
void TargetShutDownServer(NetworkConnection conn)
{
Application.Quit();
}
// Start is called before the first frame update
void Start()
{
}
[TargetRpc]
void TargetJoinServer2(NetworkConnection conn, int port)
{
GlobalScript.PlayerTotal = 2;
GlobalScript.WinningScore = 7;
GlobalScript.Port = Convert.ToUInt16(port);
GlobalScript.StartServerClient = true;
SceneManager.LoadScene("MainGame");
}
[TargetRpc]
void TargetJoinClients2(NetworkConnection conn, int port)
{
GlobalScript.PlayerTotal = 2;
GlobalScript.WinningScore = 7;
GlobalScript.Port = Convert.ToUInt16(port);
GlobalScript.networkAddress = "localhost";
GlobalScript.StartServerClient = true;
SceneManager.LoadScene("MainGame");
}
// Update is called once per frame
void Update()
{
if (isServer)
{
if (PlayerClientSpace2.transform.childCount >= 2 && PlayerMidSpace2.transform.childCount == 0 && PlayerServerSpace2.transform.childCount>=1)
{
GameObject G1 = PlayerClientSpace2.transform.GetChild(0).gameObject;
GameObject G2 = PlayerClientSpace2.transform.GetChild(1).gameObject;
GameObject G3 = PlayerServerSpace2.transform.GetChild(0).gameObject;
G1.tag = "Client";
G2.tag = "Client";
G3.tag = "Server";
G3.transform.SetParent(PlayerMidSpace2.transform);
G1.transform.SetParent(PlayerMidSpace2.transform);
G2.transform.SetParent(PlayerMidSpace2.transform);
[link text][1]
}
if(PlayerMidSpace2.transform.childCount==3 && joinserver2)
{
joinserver2 = false;
for (var i=0; i<PlayerMidSpace2.transform.childCount; i++)
{
if(PlayerMidSpace2.transform.GetChild(i).gameObject.tag=="Server")
{
port2=70000;
while(!UsedPorts.Contains(port2))
{
port2++;
}
UsedPorts.Add(port2);
createserver2 = true;
TargetJoinServer2(PlayerMidSpace2.transform.GetChild(i).gameObject.GetComponent<ConnectionManager>().connectionToClient, port2);
}
}
}
if (PlayerMidSpace2.transform.childCount == 2 && joinclient2)
{
joinclient2 = false;
bool checkserver = false;
for (var i = 0; i < PlayerMidSpace2.transform.childCount; i++)
{
if (PlayerMidSpace2.transform.GetChild(i).gameObject.tag == "Server")
{
checkserver = true;
}
}
if(!checkserver)
{
joinserver2 = false;
for (var i = 0; i < PlayerMidSpace2.transform.childCount; i++)
{
TargetJoinClients2(PlayerMidSpace2.transform.GetChild(i).gameObject.GetComponent<ConnectionManager>().connectionToClient, port2);
}
}
else
{
joinclient2 = true;
}
}
if (PlayerMidSpace2.transform.childCount==0)
{
joinserver2 = true;
joinclient2 = true;
}
if(PlayerServerSpace2.transform.childCount==0 && createserver2)
{
createserver2 = false;
Process startserver = new Process();
startserver.StartInfo.FileName = "C:\\Users\\ankit\\TopCat\\Builds\\TopCat.exe";
startserver.StartInfo.Arguments = "server2";
startserver.Start();
}
}
}
}
[1]: /storage/temp/161023-files.zip
unityerror.png
(116.1 kB)
files.zip
(6.0 kB)
Comment
Your answer
Follow this Question
Related Questions
Unity Unet Lobby problem 0 Answers
How to connect to a match by a string 0 Answers
Can Lobby Player have authority? 0 Answers
How to call NetworkClient.Ready() ? 1 Answer