Creating a custom NetworkManagerHUD
I started out by debugging the stock JS example of the network connection script then tried adding some more functions. I believe what I am failing to achieve is setting the "Client Ready" state of the players. I would like this to be set automatically when possible, if possible. It appears to be capable of creating and joining a server using multiple clients based upon the console logs, although no players get spawned in. Console when creating a server:
MatchMakingClient Create :https://eu1-mm.unet.unity3d.com/json/reply/CreateMatchRequest
UnityEngine.Networking.Match.NetworkMatch:CreateMatch(CreateMatchRequest, ResponseDelegate`1)
SimpleMatchMaker___:CreateInternetMatch(String) (at Assets/Scripts/SimpleMatchMaker___.js:15)
UnityEngine.EventSystems.EventSystem:Update()
JSON Response: [[UnityEngine.Networking.Match.CreateMatchResponse]-success:True-extendedInfo:]-address:52.28.9.224,port:9999,networkId:0x0640000000020E42,nodeId:0x037F,usingRelay:True
UnityEngine.Networking.Match.<ProcessMatchResponse>c__Iterator0`1:MoveNext()
Similarly recognisable result when joining the server too.
OnMatchCreate, OnServerReady and OnServerAddPlayer do not generate Debug.Logs and therefore I'm fairly sure they're not being called.
From reading the documentation I understand if the server is running at least one of those is supposed to be called? Can somebody talk me through making this work?
Code:
#pragma strict
public class SimpleMatchMaker___ extends NetworkBehaviour {
var playerPrefab1 : GameObject;
function Start() {
NetworkManager.singleton.StartMatchMaker();
}
//call this method to request a match to be created on the server
public function CreateInternetMatch(matchName: String) {
var create: UnityEngine.Networking.Match.CreateMatchRequest = new UnityEngine.Networking.Match.CreateMatchRequest();
create.name = matchName;
create.size = 4;
create.advertise = true;
create.password = "";
NetworkManager.singleton.matchMaker.CreateMatch(create, OnInternetMatchCreate);
}
//this method is called when your request for creating a match is returned
private function OnInternetMatchCreate(matchResponse: UnityEngine.Networking.Match.CreateMatchResponse) {
if (matchResponse != null && matchResponse.success) {
//Debug.Log("Create match succeeded");
var hostInfo: UnityEngine.Networking.Match.MatchInfo = new UnityEngine.Networking.Match.MatchInfo(matchResponse);
NetworkServer.Listen(hostInfo, 9000);
NetworkManager.singleton.StartHost(hostInfo);
}
else {
Debug.LogError("Create match failed");
}
}
//call this method to find a match through the matchmaker
public function FindInternetMatch(matchName: String) {
NetworkManager.singleton.matchMaker.ListMatches(0, 20, matchName, OnInternetMatchList);
}
//this method is called when a list of matches is returned
private function OnInternetMatchList(matchListResponse: UnityEngine.Networking.Match.ListMatchResponse) {
if (matchListResponse.success) {
if (matchListResponse.matches.Count != 0) {
//join the last server (just in case there are two...)
NetworkManager.singleton.matchMaker.JoinMatch(matchListResponse.matches[matchListResponse.matches.Count - 1].networkId, "", OnJoinInternetMatch);
}
else {
Debug.Log("No matches in requested room!");
}
}
else {
Debug.LogError("Couldn't connect to match maker");
}
}
//this method is called when your request to join a match is returned
private function OnJoinInternetMatch(matchJoin: UnityEngine.Networking.Match.JoinMatchResponse) {
if (matchJoin.success) {
//Debug.Log("Able to join a match");
var hostInfo: UnityEngine.Networking.Match.MatchInfo = new UnityEngine.Networking.Match.MatchInfo(matchJoin);
NetworkManager.singleton.StartClient(hostInfo);
}
else {
Debug.LogError("Join match failed");
}
}
// public virtual function OnMatchCreate(conn: NetworkConnection){
// Debug.Log("OnMatchCreate");
// NetworkServer.SetClientReady(conn);
// }
public virtual function OnServerReady(conn: NetworkConnection){
Debug.Log("OnServerReady");
NetworkServer.SetClientReady(conn);
}
public function OnServerAddPlayer(conn: NetworkConnection, playerControllerId: short) {
Debug.Log("OnServerAddPlayer");
var playerPrefab = GameObject.Instantiate(playerPrefab1, Vector3.zero, Quaternion.identity);
NetworkServer.AddPlayerForConnection(conn, playerPrefab, playerControllerId);
}
}
Your answer
Follow this Question
Related Questions
Setting the playerPrefab of network manager dynamically 1 Answer
Replacing NetworkManagerHUD button functionality 1 Answer
Unet - whats the best approach for a global game manager? 0 Answers
Unity network manager assign playerprefab at runtime 0 Answers
how to spawn a player prefab on a server 0 Answers