Search Unity

Showcase Mirror - Open Source Networking for Unity

Discussion in 'Multiplayer' started by mischa2k, Aug 11, 2016.

  1. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Feel free to ask in our Mirror discord (see my signature)
     
    Johan_Valectric likes this.
  2. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Hi, I'm currently integrating the great Mirror (thanks!!) for the first time into an existing game, and I have a question that I feel is so stupid that nobody has asked it or already knows the answer to. I googled this first (I don't want to waste your time), but came up empty.

    Here's the setup:
    The game uses a Lobby built on Unity's own 'Lobby' asset.As such, it 'using UnityEngine.Networking.Match' and some others (e.g.*.Types). I noticed that following your Transition guide, the global replacement will leave these untouched, merely exchanging the UnityEngine.Networking for Mirror.

    Stoopid Questions:
    1. Should I leave these 'using' clauses referring UnityEngine.Networking in place, or are there Mirror equivalents? Currently, there is no Mirror.Match nor Mirror.Types
    2. One of the reasons I'm migrating to Mirror (besides better code quality in Mirror :) ) is that i want to go to 2019 which removed UNet. Will those using clauses (.Match and .Types) still work there?
    3. The Lobby demo from Unity never really worked well (requiring re re-start of the game after finishing a network game, returning to the lobby always returned network errors that prevented another connection). Do you have a recommendation for a better working lobby system? Ripping the lobby out will hurt, but I've lost so much hair over that lobby that I'm open to suggestions.
    Thanks for any suggestions, and for a great asset!

    -ch
     
    Last edited: Dec 16, 2019
    FaberVi likes this.
  3. MrG

    MrG

    Joined:
    Oct 6, 2012
    Posts:
    368
    What was called Lobby in UNet was renamed to Room system in Mirror, and the bugs were fixed and it does work. A new example is included with Mirror for that.

    There is however no Match service integration since Unity will be taking that down.

    For faster response, please join our Discord.

    Thanks!
     
    csofranz likes this.
  4. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Thanks!

    Since that game is sold via steam, I'll see if I can use Steam's matchmaking instead (more precisely: the P2P networking, which hopefully also will allow us to stop throwing money at Unity's deteriorating server infrastructure).

    -ch
     
  5. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    We already have a steam transport for Mirror. Check out our discord!
     
    csofranz and Joe-Censored like this.
  6. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Progress: we fixed a race condition in the NetworkManager's StartHost function. Next version will be pushed to Asset Store asap.

    Working on further improvements as usual. Join our discord if you have any questions.
     
    Refeas likes this.
  7. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    There is a long-standing bug ('feature?) in UNet's Network Manager that I've run into, and which seems to be implemented in Mirror as well (so it's fully bug-compatible :)):
    To make it easier to understand, let's imagine we implement an MP game where one player is Host, the other Client, and all games are single-scene short affairs that last a few minutes each, so clients routinely connect and dsconnect from Hosts.

    Now regard this sequence:
    • a game starts in client mode, and successfully connects to the host
    • and a few minutes later the game then ends. Client disconnects and returns (via offline scene) to, uh, the offline scene
    • A new game is set up, and the Host starts a new game. If in the Client's NetworkManager the 'online scene' attribute is not set (i.e. it is a blank/empty string), the Network Manager will not load the scene the Host specifies, but immeadiately signat to the Host that it is ready. This prompts the Host to tell the Client to start loading Assets (usually at least the player prefab) that do not exist (the Client is still in the offline scene)
    • This results in a non-recoverable situation.
    Obviously, the remedy is to never leave the onlineScene attribute blank, but you will only program for this if you know about this bug. If you don't, you seem to have an intermittend error on your hands, that might cost you all your remaining hair. I recommend you modify the NM so that it outputs a warning when starting the Client on an empty onlineScene attribute

    Oh, and again thanks for a great resource!

    -ch
     
  8. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Please post it on our github issue tracker :)
     
  9. Privateer

    Privateer

    Joined:
    Jun 5, 2016
    Posts:
    23
    How would you go about it?
     
    QQuixotic likes this.
  10. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Privateer likes this.
  11. bystrickdm

    bystrickdm

    Joined:
    Jan 9, 2020
    Posts:
    1
    Hello! I am your sponsor on GitHub. I really like Mirror PRO. But I can’t work with them, because Link to Download Mirror PRO does not work. What should I do?
     
  12. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    News: fixed all open bugs this week:
    upload_2020-1-10_16-21-14.png
    Enjoy!


    Contact us in discord please.
     
    FaberVi and hopeful like this.
  13. digiross

    digiross

    Joined:
    Jun 29, 2012
    Posts:
    323
    Ok after a lot of research on the various networking threads I'm convinced that Mirror is the way to go for my game. I'm not new to game dev just multiplayer and i'm starting a 3rd person shooter and I'm wanting to build the game from the ground up with multiplayer included (as recommended).

    Previously, I was considering Photon but appears to be limiting with it's message limits but it does have relays globally.

    Anyways, using Mirror I know I would set up a server but what is recommended to have global low ping without me hosting a bunch of different places. Any choices besides Amaon AWS?

    Thanks in advance.
     
    Last edited: Jan 14, 2020
    mischa2k likes this.
  14. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    A relay is just a separate service.
    We might host a relay ourselves some day. But that wouldn't require any Mirror modifications.

    In other words: you can use any existing relay from the internet, or code your own, or wait until we do :)
    Or just host servers yourself.
     
  15. BitPax

    BitPax

    Joined:
    Oct 4, 2019
    Posts:
    89
    Is there a tutorial for setting a Mirror server up on a VPS (Virtual Private Server) like Digital Ocean?
     
  16. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Yes, check out our documentation
     
  17. BitPax

    BitPax

    Joined:
    Oct 4, 2019
    Posts:
    89
    I must be confused. I looked at the documentation but I couldn't find a tutorial for setting it up on a VPS. Could you help me out with a link?
     
  18. bigd53618

    bigd53618

    Joined:
    Sep 23, 2017
    Posts:
    18
    How do you make sure old cameras our local players and not controlling other characters
     
  19. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
  20. Togias

    Togias

    Joined:
    Jul 17, 2017
    Posts:
    6
    Hallo, I am having an issue or maybe i am not configuring the scene right.
    I setup my server in a different scene than the client scene.
    I build the server and run it.
    Every time the client connects to the server i get


    Could not spawn assetId=00000000-0000-0000-0000-000000000000 scene=5773007113619659104 netId=2
    UnityEngine.Debug:LogError(Object)
    Mirror.ClientScene:OnSpawn(SpawnMessage) (at Assets/Mirror/Runtime/ClientScene.cs:526)
    Mirror.<>c__DisplayClass36_0`1:<RegisterHandler>b__0(NetworkConnection, SpawnMessage) (at Assets/Mirror/Runtime/NetworkClient.cs:421)
    Mirror.<>c__DisplayClass7_0`1:<MessageHandler>b__0(NetworkMessage) (at Assets/Mirror/Runtime/MessagePacker.cs:169)
    Mirror.NetworkConnection:InvokeHandler(Int32, NetworkReader, Int32) (at Assets/Mirror/Runtime/NetworkConnection.cs:301)
    Mirror.NetworkConnection:TransportReceive(ArraySegment`1, Int32) (at Assets/Mirror/Runtime/NetworkConnection.cs:357)
    Mirror.NetworkClient:OnDataReceived(ArraySegment`1, Int32) (at Assets/Mirror/Runtime/NetworkClient.cs:168)
    UnityEngine.Events.UnityEvent`2:Invoke(ArraySegment`1, Int32)
    Mirror.TelepathyTransport:processClientMessage() (at Assets/Mirror/Runtime/Transport/TelepathyTransport.cs:92)
    Mirror.TelepathyTransport:LateUpdate() (at Assets/Mirror/Runtime/Transport/TelepathyTransport.cs:119)
     
  21. LootlabGames

    LootlabGames

    Joined:
    Nov 21, 2014
    Posts:
    343
    I noticed when I send a message from a object with client authority like this:
    Code (CSharp):
    1. NetworkClient.Send(new UMAManangerSync());
    The netId of object sending it is 5. The netId of of the object that spawned the object is 1.

    When the server gets the message it has the netId of the player that spawned it and not the id of the object that sent it.
    Code (CSharp):
    1. public void OnSyncFromClientToServer(NetworkConnection connection, UMAManangerSync message)
    2. {
    3.     if (netId == connection.identity.netId)
    4.     {
    5.         //to do
    6.     }
    7. }
    When the method is called it never gets inside because the netId is 5 but the connection.identity.netId is 1. This used to work fine under UNET.
     
    Last edited: Feb 9, 2020
  22. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Try one of our built in examples, like Tanks. it works out of the box :)
     
  23. magickprogramando

    magickprogramando

    Joined:
    Mar 8, 2017
    Posts:
    24
    Hi!What happens if someone has closed ports? Is there a way around this without having to ask each person to open the ports?
     
  24. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Host your server on the internet. For example, google cloud offers a free f1-micro instance that you can try.
     
  25. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Mirror Progress Update:
    We are working on bug fixes and hardening / tests.
    Right now, we are at 42.3% test coverage.
    We will remove all obsolete code in a month or two, then we will see the true test coverage. It should be around 50%.
    50% isn't perfect, but we are getting there!
    upload_2020-2-23_14-30-44.png

    To put this in context: we want to cover as much code as possible with tests for maximum reliability. The closer we get to 100%, the less likely it is that someone encounters a Mirror bug.

    Think about it this way: with 0% code coverage, anything could go wrong in production. With 100% coverage, you can be confident that your game's networking is not gonna fail.

    It also helps us to improve old code more. Every time we change something, we can run our tests to make sure that we didn't break anything unexpectedly.

    When someone reports a bug, we can add a test case for it so that the bug never happens again in the future.

    This is going to be the biggest Mirror feature of 2020. It won't look exciting, but it will be extremely useful for everyone that uses Mirror for their games. We can't allow it to fail when you run an MMO with 1k online players. It's just not an option.
     
    Last edited: Feb 23, 2020
    jjobby, iSleepzZz and AlexHolderDev like this.
  26. Davood_Kharmanzar

    Davood_Kharmanzar

    Joined:
    Sep 20, 2017
    Posts:
    411
    does exists any manual to how to build headless server with MiRRoR?
    and howto run server with specific commands? i mean call server with specific commands to load specific map or scene with their parameters and etc ..
     
  27. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    You can pass your Unity build any command line argument you want. From an older thread:
    Then you'd just check for certain arguments in the returned array and act on them. This isn't really a Mirror specific thing, or even networking related. You could do this kind of thing in the client if you wanted a way for players to force certain options in your game without editing a config file or getting into an options menu.

    As for headless, that's again unrelated to Mirror or networking. You just use -batchmode -nographics which are built in command line arguments for Unity builds. For platforms like Linux which have the headless build check box, check that as well so it doesn't even include graphics related stuff in the build in the first place.
    https://docs.unity3d.com/Manual/CommandLineArguments.html
     
  28. zhn17

    zhn17

    Joined:
    Jul 30, 2019
    Posts:
    52
    Been searching for a response to this question but found none
    How would I go by syncing a animation "manually" via Object.Anim.Play("Run", 0);?
    I've got everything set up for a multiplayer game using Malbers' HorseAnimset PRO asset, all animations except some, mounting being one of those.
     
  29. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    I would make it a server command that is issued by the player with authority, and then passed to all clients via rpc
     
  30. zhn17

    zhn17

    Joined:
    Jul 30, 2019
    Posts:
    52
    Could you write how to do it?
    I've done stuff with Commands and RPC before but never with Authority
     
  31. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Luckily, this isn't that difficult. First, make sure that the script that needs to sync the animation is a networked object (has a Network id). This means that the script that wants to set the animation runs on all Clients, and must make sure that it is part of the object that can make such decisions (i.e. has authority), not one that is merely a clone. You do that by checking the hasAuthority bool.

    To make sure that the Animation is set on all clients, the script then sends a command (that you define yourself) to the server. Only scripts with authority can do that. On the server, the command is executed and it calls the Client remote procedure to actually set the Animation, passing along the relevant Parameters.

    the logic should be similar (i'm typing this off the top of my head, I'm on the road) to this

    Code (CSharp):
    1. ... // in the networked script
    2. if (hasAuthority) {
    3.    CmdSetAnim("Run", 0);
    4. }
    5.  
    6. ...
    7.  
    8. // server-only part
    9. [Command] public void CmdSetAnim(string Name, int id){
    10.    // call the Clients and have them set anim
    11.    RpcSetAnim(Name, id);
    12. }
    13.  
    14. // Client part, executed on all Clients
    15. [ClientRpc] public void RpcSetAnim(string Name, int id) {
    16.    Object.Anim.Play(Name, id);
    17. }
    18.  
    Note that you must insure that all scripts have correctly linked to the same instance of "Object", further Network logic for that may be required.
     
    mischa2k and zhn17 like this.
  32. zhn17

    zhn17

    Joined:
    Jul 30, 2019
    Posts:
    52
    Alright, thanks. I'll update you with the progress if I get it working, considering Malbers' horse animset pro is really badly coded
     
  33. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Progress Update: we went from 0 to 29 NetworkServer tests, which helped us discover and fix 5 bugs:
    upload_2020-2-26_18-39-17.png

    That's 5 bugs that your players won't encounter in production :)
     
    Matt-Cranktrain, yploid and Grizmu like this.
  34. zhn17

    zhn17

    Joined:
    Jul 30, 2019
    Posts:
    52
    Seems to work for the mounting and dismounting (haven't had time to do it for the horse yet), thanks for the help!
     
  35. xerokwan

    xerokwan

    Joined:
    Jun 23, 2017
    Posts:
    4
    I want to adopt my game to addressables recently, it seems there are a lot of work to do to switch SceneManager.LoadSceneAsync to Addressables.LoadSceneAsync in NetworkManager because they are using different return types and handlers...Anyone got any idea to do it much more cleaner and simpler instead of altering hundreds lines of codes?
     
    AlexHolderDev likes this.
  36. KAYUMIY

    KAYUMIY

    Joined:
    Nov 12, 2015
    Posts:
    115
    How to install mobile / desktop communication with wifi?
    I am testing Chat scene but I fail.
    Is there any documentation or tutorial how to make comunnimcation environement?
     
  37. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Try to host the chat server on a vps/dedicated/cloud server on the internet.
     
    KAYUMIY likes this.
  38. KAYUMIY

    KAYUMIY

    Joined:
    Nov 12, 2015
    Posts:
    115
    I don't know how to move chat server to cloud.
    Can you explain it in detail please?
    How to host chat server on the cloud server on the internet?
     
  39. zhn17

    zhn17

    Joined:
    Jul 30, 2019
    Posts:
    52
    A quick Google search explains how to get a server.
     
    KAYUMIY likes this.
  40. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Mirror Progress Update:
    We have the single most complex UNET function now fully covered in tests:


    This allows us to modify and improve it, hopefully with some performance gains in observer building (which is still an easy bottleneck to encounter).

    The rest of NetworkIdentity is almost entirely covered by tests now too:
    https://gyazo.com/787f3150b6873beb7f4ce2002a5801e3
    The red parts are mostly obsoletes that will be removed in 1-2 months. This is HUGE for stability in every single Mirror project for the next 5-10 years!
     
    Matt-Cranktrain and KAYUMIY like this.
  41. KAYUMIY

    KAYUMIY

    Joined:
    Nov 12, 2015
    Posts:
    115
    Do you think Google Cloud plafrom can be solution for the desktop & mobile communication project?
     
  42. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Yes, we use it for our list server and demo projects too.
     
    KAYUMIY likes this.
  43. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Mirror Progress Update: NetworkBehaviourTests finished with 96.1% code coverage. More after we remove obsoletes.

    In other words, it's extremely unlikely that NetworkBehaviour will have bugs ever again.
    upload_2020-3-7_20-54-43.png
     
  44. itzikpel

    itzikpel

    Joined:
    Mar 27, 2015
    Posts:
    19
    Hi,
    I have two questions

    #1: Can I have nested NetworkIdentity of different clients?
    example:
    A non VR player is driving the Bus and a VR Player can walk inside the Bus.
    If I set the VR player position/rotation offset to the bus - the movement is very laggy..
    If the VR player is the child of the Bus - its works perfect..

    #2: Can I have nested NetworkIdentity of the same client?
    example:
    A player charcater is walking in the street
    Each car in the street have a NetworkIdentity without client authority
    When the player start to drive, I'm parenting the player character to the car GameObject -
    And set the client authority of the car to be the same as the player,
    When the player stop driving, I'm unparenting the character from the car and release the client authority from the car

    thanks
     
    Last edited: Mar 11, 2020
    tylerguitar75 likes this.
  45. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
  46. patSilva

    patSilva

    Joined:
    Mar 25, 2019
    Posts:
    27
    I believe so, and I also think you can achieve a lot of what you want with NetworkTransformChild. Add that component to the parent and set its target to the child you want. In my experience I have used multiple NetworkTransformChild components on the same Parent so it could track multiple Network Identities. So multiple players could enter the bus. Also the parenting and unparenting would be done the same way in case number two with the added step of resetting the car's client authority to false.
     
  47. GuiTeK

    GuiTeK

    Joined:
    Apr 28, 2017
    Posts:
    12
    Hi Mirror enthusiasts!

    Congratulations to all the Mirror contributors for the amazing work! It's impressive to see the progress made in just a few months (not only in terms of code, but also documentation, video tutorials, etc.). The KISS approach of Mirror and the hard work you put into this project to make it reliable result in such a great Developer eXperience, and it allows us to move forward quickly with our games! Thank you!

    I got a quick question: when is/should ClientScene.AddPlayer() be called? Currently I have this function in my server code:
    Code (CSharp):
    1. public override void OnServerAddPlayer(NetworkConnection conn)
    2. {
    3.     base.OnServerAddPlayer(conn);
    4. }
    Which is called when the client calls ClientScene.AddPlayer() and basically adds a player for this connection.
    However, as I spawn the player manually server-side, should this ever be called? I'm concerned by malicious clients calling this while they should not.
     
    mischa2k likes this.
  48. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Thanks mate.
    We kept the naming from UNET, and it's a bit confusing.
    ClientScene.AddPlayer doesn't really add a player. it just asks the server to add a player, after which OnServerAddPlayer is called.
    If you instantiate+spawn your player manually, then you don't need to call the base function.
    If you need help I can post a snippet of uMMORPG's NetworkManager code, where I do that too.
     
    Joe-Censored likes this.
  49. Piflik

    Piflik

    Joined:
    Sep 11, 2011
    Posts:
    292
    I have an issue with spawning a customized player. I followed the Docs to send a message containing the configuration. This works only on the Host instance of the application, but for all player objects. On the client, the registered message handler doesn't get called. I tried registering it in OnStartClient, too, but that didn't change anything.
    Can anybody tell me what I am doing wrong?
     
    Last edited: Mar 26, 2020
  50. ronnansouza

    ronnansouza

    Joined:
    Feb 21, 2019
    Posts:
    1
    Hello everyone, is there any way to measure packet loss between client and server using Mirror? I've been reading about using UNet I can't anything reliable. Thanks in advance!