I’ve been using the NetworkManagerHUD to test my game over a network and with matchmaking. So far, so good!
Now I would like to create my own menu, instead of using NetworkManagerHUD. However, this has proven to be more difficult than I thought. I’m having trouble finding documentation that I can actually understand & use, and I haven’t found a simple example either.
Perhaps you could help me out, give me some ideas or starting points?
I would like to give the player 3 options:
Create a match (Using matchmaker, with a name, and optional password)
Join a match (Using matchmaker, which would return a list of existing matches for the player to join)
Quick join (which would use matchmaker to join the first match that isn’t password protected)
A basic rundown of what kind of magic NetworkManagerHUD is using to create / join a match would also be very useful.
Thank you very much for your help! I’m stuck here, so I really appreciate your help!
You should be able to do this entirely without subclassing NetworkManager, though you’ll probably want to do that eventually. Create a UI and a script called say, MatchmakingHelper, to act as the connection between the NetworkManager and the UI.
Then, your MatchmakingHelper class just needs a reference to your NetworkManager.
On tapping “Create”, your MatchmakingHelper will call myNetworkManager.StartMatchMaker(), and then immediately call NetworkManager.matchMaker.CreateMatch().
On tapping “Join”, your MatchmakingHelper will call myNetworkManager.StartMatchMaker(), and then immediately call myNetworkManager.matchMaker.ListMatches(), and pass in a callback function called OnMatchList.
Now, as long as your MatchmakingHelper class is on the same object as your NetworkManager object, “OnMatchList” will be called, so you need to create the OnMatchList function with a signature of public void OnMatchList(ListMatchResponse matchListResponse).
You’ll then need to look up how to handle a ListMatchResponse object, which you can find in the docs, and then decide which match you want to connect to with myNetworkManager.matchMaker.JoinMatch. Probably display a server list or something.
Thank you very much, Eshan! With your explanation, setting it up was quite easy
My helper class wasn’t on the same object though, but NetworkManager has a singleton instance that I could access.
I haven’t replaced the NetworkManagerHUD entirely just yet though, because although I can connect 2 devices (confirmed by debug.log), the game doesn’t load the right scene and spawn the players just yet. Tips for this last part are very welcome!
Spawning players and loading scenes should be fairly well explained in the docs. You should be able to drag in a player prefab (which must have a NetworkIdentity component on it) to the spawn list on NetworkManager and see it spawn.
I had configured my NetworkManger correctly from inspector (and NetworkManagerHUD works flawlessly)… but it turns out I forgot something very silly: to actually kickstart NetworkManager.StartHost()
So creating a match is working just fine now, and I can connect to a game that has been created via my menu. However, I can’t join a game using the menu I created. Joining a game using NetworkManagerHUD works, but when I use my menu I get a CRCMismatch error (even when I disable the CRC check in the inspector settings of NetworkManager).
Here is my script:
public void OnMatchJoined(JoinMatchResponse matchJoin)
{
Utility.SetAccessTokenForNetwork(matchJoin.networkId, new NetworkAccessToken(matchJoin.accessTokenString));
var matchInfo = new MatchInfo(matchJoin);
NetworkManager.singleton.client = new NetworkClient();
NetworkManager.singleton.client.RegisterHandler(MsgType.Connect, OnConnected);
NetworkManager.singleton.client.Connect(matchInfo);
}
}
I don’t get the error when I remove the last line (client.Connect()), so maybe that helps narrow it down? I hope you have some ideas!
In any case, thank you very much for your help so far, and merry Christmas
Why bother overriding OnMatchJoined? I wouldn’t do that unless you need to do something special in it.
Put a script on your Player object and override OnStartLocalPlayer instead. It’s called when the client successfully connects to the server. Let the NetworkManager handle actually doing the connection.