g_l
g_l

Reputation: 219

Looking for basic explanation on unity matchmaker (5.4) and how to use in scripts

Having decided to do Networking through Unity over Photon due to price (if there is a better way of networking let me know) I have been trying to create a simple unity matchmaker where when a user enters the game they first look for any other match (AKA rooms / games), if there is one they join it, if there isn't one they create one. However I am struggling to find a clear explanation of how to use the unity matchmaker, so I can develop code to get my desired behavior which seems even trickier as it seems unity is changing it's networking system a lot in recent updates so I believe anything before 5.4 is currently out-of-date. The only useful thing I have found is this example code but I want to understand the system more so I don't miss any key concepts, which is likely as I am fairly beginner-ish.

So what I am asking you today is can you direct me to a clear explanation of the matchmaking system (including scripting) which is relevant to the current unity or, admittedly much harder and more time consuming, could you try to do it yourself and it will serve as future reference for others like me.

Thanks for reading, of course any answers will be greatly appreciated.

[i work in c#]

EDIT:

I understand my question is very difficult to answer so I am going to test out the example code until i get the desired behavior and post any errors which i cannot solve.

Problem 1

  1. I have copied the exact code (Included below for easier reference) into a script which i have attached to my network manager. But I am getting errors due to protection levels -

enter image description here

I have two theories on how to solve this -

  1. Changing the method accessors
  2. Not deriving the script from monobehaviour

I am currently testing them out, any advice on how to solve this would be very useful. I wonder if I should report this to unity as to me it seems to be a wrong example in unity docs which say it is updated for 5.4. Thanks.

Problem 2

I have created this script and have run it in the editor attached to an empty GameObject with single network manager, I get no compiler errors but when I run it debugs "ERROR: Match Search Failure" which means the listmatches funtion was unsuccessful, my script is below -

using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.Networking.Match;
using UnityEngine.Networking.Types;
using System.Collections;
using System.Collections.Generic;

public class MyMatchMaker : MonoBehaviour {
    bool done;
    // Use this for initialization
    void Start () {
        NetworkManager.singleton.StartMatchMaker();
        NetworkManager.singleton.matchMaker.ListMatches(0, 20, "Match", false, 0, 1, OnMatchList);
        Debug.Log("Searching");
    }

    public virtual void OnMatchList(bool success, string extendedInfo, List<MatchInfoSnapshot> matchList)
    {
        if (success)
        {
            if (matchList.Count != 0)
            {
                Debug.Log("Matches Found");
                NetworkManager.singleton.matchMaker.JoinMatch(matchList[0].networkId, "", "", "", 0, 1, OnMatchJoined);
            }
            else
            {
                Debug.Log("No Matches Found");
                Debug.Log("Creating Match");
                NetworkManager.singleton.matchMaker.CreateMatch("Match", 2, true, "", "", "", 0, 1, OnMatchCreate);
            }
        }
        else
        {
            Debug.Log("ERROR : Match Search Failure");
        }
    }

    public virtual void OnMatchJoined(bool success, string extendedInfo, MatchInfo matchInfo)
    {
        if (success)
        {
            Debug.Log("Match Joined");
            MatchInfo hostInfo = matchInfo;
            NetworkManager.singleton.StartClient(hostInfo);

            OnConnect();
        }
        else
        {
            Debug.Log("ERROR : Match Join Failure");
        }

    }

    public virtual void OnMatchCreate(bool success, string extendedInfo, MatchInfo matchInfo)
    {
        if (success)
        {
            Debug.Log("Match Created");

            MatchInfo hostInfo = matchInfo;
            NetworkServer.Listen(hostInfo, 9000);
            NetworkManager.singleton.StartHost(hostInfo);

            OnConnect();
        }
        else
        {
            Debug.Log("ERROR : Match Create Failure");
        }
    }

    void OnConnect ()
    {
        if (Network.isServer)
        {
            Debug.Log("You are Server");
        }
        else if (Network.isClient)
        {
            Debug.Log("You are Client");
        }
        else
        {
            Debug.Log("ERROR : MatchMaking Failed, Peer type is neither Client nor Server");
        }
    }
}

Thanks, hopefully you may be able to see where i have gone wrong.

@Programmer

Upvotes: 0

Views: 1458

Answers (1)

g_l
g_l

Reputation: 219

Ah... Got this code working I debuged the information string in the call back and had to change a few project settings.

using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.Networking.Match;
using UnityEngine.Networking.Types;
using System.Collections;
using System.Collections.Generic;

public class MyMatchMaker : MonoBehaviour {
    bool done;
    // Use this for initialization
    void Start () {
        NetworkManager.singleton.StartMatchMaker();
        NetworkManager.singleton.matchMaker.ListMatches(0, 20, "Match", false, 0, 1, OnMatchList);
        Debug.Log("Searching");
    }

    public virtual void OnMatchList(bool success, string extendedInfo, List<MatchInfoSnapshot> matchList)
    {
        if (success)
        {
            if (matchList.Count != 0)
            {
                Debug.Log("Matches Found");
                NetworkManager.singleton.matchMaker.JoinMatch(matchList[0].networkId, "", "", "", 0, 1, OnMatchJoined);
            }
            else
            {
                Debug.Log("No Matches Found");
                Debug.Log("Creating Match");
                NetworkManager.singleton.matchMaker.CreateMatch("Match", 2, true, "", "", "", 0, 1, OnMatchCreate);
            }
        }
        else
        {
            Debug.Log("ERROR : Match Search Failure");
        }
    }

    public virtual void OnMatchJoined(bool success, string extendedInfo, MatchInfo matchInfo)
    {
        if (success)
        {
            Debug.Log("Match Joined");
            MatchInfo hostInfo = matchInfo;
            NetworkManager.singleton.StartClient(hostInfo);

            OnConnect();
        }
        else
        {
            Debug.Log("ERROR : Match Join Failure");
        }

    }

    public virtual void OnMatchCreate(bool success, string extendedInfo, MatchInfo matchInfo)
    {
        if (success)
        {
            Debug.Log("Match Created");

            MatchInfo hostInfo = matchInfo;
            NetworkServer.Listen(hostInfo, 9000);
            NetworkManager.singleton.StartHost(hostInfo);

            OnConnect();
        }
        else
        {
            Debug.Log("ERROR : Match Create Failure");
        }
    }
}

Upvotes: 1

Related Questions