Search Unity

Help Wanted Question on Mirror/Unity Network integration

Discussion in 'Multiplayer' started by gtramnull, May 22, 2020.

  1. gtramnull

    gtramnull

    Joined:
    Apr 23, 2020
    Posts:
    5
    Greetings everyone,

    First of all, I'm new to unity's network layer/HLAPI concepts and all that jazz.
    I've been playing with Mirror (which looks slick tbh. Kudos!) but have been struggling to understand the concept of multiplayer as a whole.

    Here's the scenario:
    - 2-player game, one mobile device as a host
    - 2nd player connects to 1st player's device

    I've been reading up on Mirror examples but they all seem to sample its own matchmaking/lobby feature all across, which is defaulted to localhost. I did get it to work giving the Network Address in NetworkManager a static one (which is my computer's IP) but I still don't seem to understand how that "localhost" becomes the actual IP address that the 2nd player's device will be connecting to once the game is live. Before, I suppose, Unity had (or still has) a cloud service called Multiplayer where you'd hook things up (as in UNET + Multiplayer service). Now, given that scenario, will I have to have some sort of cloud service(AWS or something) to be able to achieve that using Mirror?


    Sorry if my question is bit convoluted but I'm trying my best to be as clear as possible and I would reaaaaally appreciate any help.


    Cheers!
     
    MilenaRocha likes this.
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,574
    Examples for many networking solutions use localhost or 127.0.0.1 because generally you do your first testing by connecting on your own computer, and it removes other potential problems from the mix which aren't actually related to the networking API itself. Specifically firewalls and potential NAT issues. Besides those issues, technically all you have to do is type the actual IP address of the host in where it currently has localhost.
     
    gtramnull likes this.
  3. u-doug

    u-doug

    Joined:
    Jun 6, 2017
    Posts:
    11
    You probably want some kind of cloud matchmaking service to get your players playing together. I have just integrated the PlayFab matchmaker (free as far as I can tell) into my game alongside their dedicated multiplayer severs 2.0 (with a Mirror build) and it works well - here's what the unity integration looks like once you've authenticated the player: https://docs.microsoft.com/en-us/gaming/playfab/features/multiplayer/matchmaking/quickstart. The general workflow is that anyone who wants to play together submits a 'ticket' to their service, and then when two people on the same queue are compatible (you can just make it so anyone is compatible with anyone) the status of each ticket changes and allows each player to get the other's details. In my case the IP and Port of one of my dedicated servers gets returned, but using this method you could instead get your host client to send their IP with the attributes field: https://docs.microsoft.com/en-us/gaming/playfab/features/multiplayer/matchmaking/peer-to-peer

    If you've not used PlayFab before it would take a few days to setup as you'd need to learn about authenticating and logging in the player, but once you do it's great - free for up to 100,000 unique players. If you need player profile storage their services work very well.

    The above said, pure peer-to-peer networking does bring some challenges, as highlighted by Joe - you will need to use NAT punchthrough to get around firewall issues and even then that doesn't work 100% of the time. Services like Photon get around this by having all communication go through their relays, and with Mirror a dedicated server also gets around this problem by being something both clients can connect to.

    Hope that helps, feel free to ask any more questions!
     
    gtramnull and Joe-Censored like this.
  4. u-doug

    u-doug

    Joined:
    Jun 6, 2017
    Posts:
    11
    Is that not $20 a month, and provided by the Mirror team, not unity? I haven't used the list server but it may fit his needs.

    That said he was asking how player A gets player B's IP details once the game is live; manually typing the IP and Port is great for prototyping but in most cases less useful after your game has launched. The list server or a matchmaking service like PlayFab are two ways to solve that.
     
  5. gtramnull

    gtramnull

    Joined:
    Apr 23, 2020
    Posts:
    5
    Wow thank you so much everyone!


    Yes, this is pretty much what I was wondering. After a day of research I found out that I'd need some sort of service like Playfabs which looks pretty cool tbh.


    For now I have implemented a simple 2-player game that connect to the host via IP address. I'm using Mirror as I mentioned above.

    Now, a new question I have is, when it comes to Playfabs or any sort of service like that. How does one get a "server build" up on Playbabs server? Since Mirror does server and client on the same device/class/script, how does it actually work regarding having the server code on the cloud?


    Thanks again folks. I really appreciate your help!
     
  6. u-doug

    u-doug

    Joined:
    Jun 6, 2017
    Posts:
    11
    Mirror can work in one of two modes;
    1) The way you have it at the moment, where one of the clients is also a host.
    2) Create a separate server build using StartServer() which all of your hosts connect to.

    However, I wasn't necessarily suggesting that you upload a server build to PlayFab - although good for a certain type of game, you usually need to host one in each region which gets expensive fast.

    You can use their matchmaking system separately to their dedicated server hosting, so each player looks for a match, and then PlayFab lets your clients know when another player has matched with them. At that point you exchange IP details, decide who is host and who is client, and tell the client to connect to the host.

    If you don't plan on using PlayFab's backend to store player profiles it's quite a lot to learn just for matchmaking! However, if you do want to have player profiles online I suggest you check out some of their tutorials, and once you've created and logged in a player you could then move on to trying out their matchmaking service. If you do go this route and get stuck after logging in a player, I can help with your matchmaking code.
     
    Tyrmuzari and gtramnull like this.
  7. tungqlvip

    tungqlvip

    Joined:
    May 7, 2018
    Posts:
    5
    Playfab say : 100.000 userdata / 30 days, i don't understand ??
     
  8. Honorsoft

    Honorsoft

    Joined:
    Oct 31, 2016
    Posts:
    75
    I also am having problems getting Mirror to connect to other clients/servers that are not on the "localhost" AND not running on PC (my PC builds connect to localhost fine, the Android builds just timeout(Client) or start their own game alone(Server, Server-Client))... however I was able to get the clients and servers on Android to connect, like the OP was, by entering in my IP into the field.

    -There might be a bug though in the Mirror examples code (or Mirror), but when I click on any HUD button other than "Client" or "Server Only" it resets the address back to "localhost" and the Android devices no longer connect to each other.

    So, to summarize, my PC builds seem to work fine, although their HUD's also reset the address to "localhost" when you press "Server-Client" or any of the "Host" or "Start Server" buttons from the "Network Discovery" component. The Android builds do not seem to connect to eachother using "localhost" (which is a bigger problem because Mirror seems to be switching back to localhost automatically when some buttons are pressed).

    Any advice? I am currently adding more debugging code for PC and Android builds, and I am adding some 3rd party IP management and detection code to Mirror. I will run some more tests and do more research, but any help would be appreciated.
     
    haifaasayeh likes this.
  9. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,574
    The network HUD is just to give you something to use when you're getting your project started, not for actual use in your game. So if your issues are all with the HUD, I'd just write your own scripts to do what you were using the HUD to do and only worry about it if that has a problem too. As far as your 3rd party IP management, I'd first verify you can get Mirror to work in the way you need it and on the platforms you need it before customizing it. That way you'll know if your 3rd party management stuff is causing problems later or not. Good luck.
     
    Honorsoft and qbvbsite like this.
  10. ThiroSmash

    ThiroSmash

    Joined:
    Aug 11, 2019
    Posts:
    2
    I'm in the same situation as OP - I have a multiplayer game set up with mirror and I'm looking for something to let players actually connect through internet, basically a matchmaking system.

    PlayFab is a platform that should let me do that from what I understand. But if you say it's hard to learn (if you just want something simple, which I do), what other free options are there?
    I will learn PlayFab if I must, but considering how bad I am at the online aspect of game development (and the limited time that I have to spend on this) I'd rather start with something easy if I can.
     
    MilenaRocha likes this.
  11. Kamzoki

    Kamzoki

    Joined:
    Apr 25, 2016
    Posts:
    8
    I'm looking to implement something similar where the server hosts the game and players can connect to it to play a match. But I'm struggling to know what to do after I get the server details from GetMatch(). Can you help me out?

    Ultimately, I want players to connect to a match, play the match, after the match is done they disconnect from the server and I've handled disconnecting as well as server shut down.
     
  12. u-doug

    u-doug

    Joined:
    Jun 6, 2017
    Posts:
    11
    Looks like you've had some help in another thread, but if you've managed to get an IP and Port from the PlayFab matchmaker you've already done most of the hard work - it's been a while since I used this Mirror / PlayFab server setup, but I don't think you get IP details unless you've setup the server build with the GSDK correctly. For starters, just print the received IP / Port to the console and stick it in Mirror's default connection HUD, and you should be able to connect.

    If not you've probably setup your server build wrong. As long as it calls StartServer() and properly implements the GSDK you should be good to go - joining via the HUD just calls StartClient(), which will try and connect to the host with the input IP / Port details.

    Let us know what you've implemented so far if you need more help.
     
    Kamzoki likes this.
unityunity