Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Relay server and matchmaking: How to enable relay service?

Discussion in 'Multiplayer' started by AllMightyNico, Feb 4, 2016.

  1. AllMightyNico

    AllMightyNico

    Joined:
    Jun 12, 2015
    Posts:
    14
    Hello dear community,

    I am currently developing an RTS and I wonder how to use the relay server. I looked into the docs right here http://docs.unity3d.com/Manual/UNetInternetServicesOverview.html , where it says that you have to use a singleton. Even as I did some research about Singleton patterns, I wasnt able to figure out how to make use of that variable, as it does not even seem to exist anymore. When I take a peek on the classes' definition of NetworkMatch in VS2015, it doesnt have that Networking.Match.NetworkMatch variable anymore.

    How do I simply set up a match for using the relay server? How do I query for a list of matches? How do I join and disconnect properly? I did my research in the docs for days now, but I just can't piece it together. I simply want to create a match with the matchmaker using the relay server but I, at some point, find the docs misleading.

    Edit: Yes, I am an owner of a Pro version. And I set up the UPID in the Editor already.

    Can anyone help me out here?

    Best wishes
    Nicolas
     
  2. JeremyUnity

    JeremyUnity

    Unity Technologies

    Joined:
    Mar 4, 2014
    Posts:
    147
    Hi @AllMightyNico,
    UnityEngine.Networking.Match.NetworkMatch should still work, but the documentation does look wrong and i'm going to be updating it soon as well. The variable that needs to be set is NetworkManager.matchMaker, though you don't need to set it directly.

    You should only need to call StartMatchMaker() on the NetworkManager before using any matchmaking or relay functions and it will set up and allocate that state, then you can call matchMaker.CreateMatch() to create a match, or matchMaker.ListMatches() to list open matches and then matchMaker.JoinMatch() to join one.
     
    Last edited: Feb 4, 2016
    AllMightyNico likes this.
  3. AllMightyNico

    AllMightyNico

    Joined:
    Jun 12, 2015
    Posts:
    14
    Hello Jeremy,

    thank you for your reply and thank you for correcting the documentation, later on. I think, I got it now :)
     
    JeremyUnity likes this.
  4. AllMightyNico

    AllMightyNico

    Joined:
    Jun 12, 2015
    Posts:
    14
    @JeremyUnity
    Then again, I have some other questions. I created my own NetworkLobbyManager class by deriving from it and I want to handle the matchmaker inside this lobby class. My question now is, what exactly do I have to do after I recieved the CreateMatchResponse from matchMaker.CreateMatch()? What do I have to put inside the callback function to make the NetworkLobbyManager work accordingly? Does the Relay server work automatically?

    This is what I got so far. The matchMaker is started in the Awake() function.

    Code (CSharp):
    1.  void CreateMatch()
    2.     {
    3.         CreateMatchRequest _createMatchRequest = new CreateMatchRequest();
    4.         _createMatchRequest.name = "New Game";
    5.         _createMatchRequest.password = "";
    6.         _createMatchRequest.size = 2;
    7.         Dictionary<string, long> matchAttributes = new Dictionary<string, long>();
    8.         matchAttributes.Add("mapID", 0);
    9.         matchMaker.CreateMatch(_createMatchRequest, OnCreateMatchResponse);
    10.  
    11.  
    12.     }
    13.  
    14.     private void OnCreateMatchResponse(CreateMatchResponse _createMatchResponse)
    15.     {
    16.         if (_createMatchResponse.success)
    17.         {
    18.             Utility.SetAccessTokenForNetwork(_createMatchResponse.networkId, new NetworkAccessToken(_createMatchResponse.accessTokenString));
    19.          
    20.  
    21.  
    22.         }
    23.         else
    24.         {
    25.             Debug.Log("Could not create match: " + _createMatchResponse.extendedInfo);
    26.         }
    27.     }
     
  5. JeremyUnity

    JeremyUnity

    Unity Technologies

    Joined:
    Mar 4, 2014
    Posts:
    147
    Hi @AllMightyNico

    The code from the documentation looks correct there,

    Code (CSharp):
    1. public void OnMatchCreate(CreateMatchResponse matchResponse)
    2.     {
    3.         if (matchResponse.success)
    4.         {
    5.             Debug.Log("Create match succeeded");
    6.             Utility.SetAccessTokenForNetwork(matchResponse.networkId,
    7.                 new NetworkAccessToken(matchResponse.accessTokenString));
    8.  
    9.             m_MatchCreated = true;
    10.             m_MatchInfo = new MatchInfo(matchResponse);
    11.  
    12.             StartServer(matchResponse.address, matchResponse.port, matchResponse.networkId,
    13.                 matchResponse.nodeId);
    14.         }
    15.         else
    16.         {
    17.             Debug.LogError ("Create match failed");
    18.         }
    19.     }
     
  6. Goodeddie

    Goodeddie

    Joined:
    Aug 3, 2016
    Posts:
    79
    @JeremyUnity After I add the NetworkManager component, I can start using the StartMatchMaker() method?