Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Unity Multiplayer Mirror - Networking for Unity (UNET Replacement)

Discussion in 'Connected Games' started by vis2k, Aug 11, 2016.

  1. vis2k

    vis2k

    Joined:
    Sep 4, 2015
    Posts:
    3,217
    That part is from the original source, can't take credit for that :)
     
  2. Saishy

    Saishy

    Joined:
    Jun 3, 2010
    Posts:
    73
    Yeah I'm looking too at the original, but you at least was the one who showed it to me xD

    Also, found a bug in your code, I'm editing it to make a custom one for my needs and I noticed some stuttering, seems like your fixed update is called on the owner in a server if the server is also a client (listen server).

    http://i.imgur.com/33SeUgi.png

    I basically changed it to the original one part and that fixed.

    Code (csharp):
    1.  
    2.   void EmulatedFixedUpdate() {
    3.        if (!isClient || hasAuthority) {
    4.            return;
    5.        }
    6.  
    7.        // dont run if network isn't active
    8.        if (!NetworkServer.active && !NetworkClient.active) {
    9.            return;
    10.        }
    11.  
    12.        // dont run if we haven't been spawned yet
    13.        if (!isServer && !isClient) {
    14.            return;
    15.        }
    16.  
    17.        // received one yet? (initialized?)
    18.        if (goal != null) {
    19.            // rigidbody movement?
    20.            if (UseRigidbody2D() || UseRigidbody3D()) {
    21.                // teleport or interpolate
    22.                if (NeedsTeleport()) {
    23.                    SetPositionAndRotationUniversal(goal.position, false);
    24.                } else {
    25.                    SetPositionAndRotationUniversal(InterpolatePosition(), true);
    26.                }
    27.            }
    28.        }
    29.    }
    30.  
     
    vis2k likes this.
  3. vis2k

    vis2k

    Joined:
    Sep 4, 2015
    Posts:
    3,217
    Can you try this version:
    Code (CSharp):
    1.         // interpolation. needs to be in FixedUpdate so that we can use
    2.         // RigidBody too.
    3.         void FixedUpdate()
    4.         {
    5.             // apply interpolation on client for all other players
    6.             if (isClient && !(isLocalPlayer && hasAuthority)))
    7.             {
    8.                 // received one yet? (initialized?)
    9.                 if (goal != null)
    10.                 {
    11.                     // rigidbody movement?
    12.                     if (UseRigidbody2D() || UseRigidbody3D())
    13.                     {
    14.                         // teleport or interpolate
    15.                         if (NeedsTeleport())
    16.                         {
    17.                             SetPositionAndRotationUniversal(goal.position, goal.rotation, false);
    18.                         }
    19.                         else
    20.                         {
    21.                             SetPositionAndRotationUniversal(InterpolatePosition(), InterpolateRotation(), true);
    22.                         }
    23.                     }
    24.                 }
    25.             }
    26.         }
    Debug messages in Update will be removed soon by the way, don't worry about them.
     
  4. Saishy

    Saishy

    Joined:
    Jun 3, 2010
    Posts:
    73
    Gonna test, but any specific reason to use that?
    Also what does isLocalPlayer mean? I don't know why it's false since I am actually the local player.

    Edit: Uh, the test might take some time, I'm receiving connectionToServer null in my first update call so I have to fix that first :2
     
  5. Saishy

    Saishy

    Joined:
    Jun 3, 2010
    Posts:
    73
    Nope, still gets inside on listen server.

    http://i.imgur.com/LInXEAy.png

    Also, the error is because you are using connectionToServer which is null until it is initialized, I changed to ClientScene.readyConnection as the original from unet.

    Code (csharp):
    1.  
    2.  
    3. /** <summary> REQUIRES AUTHORITY. Sends a transform message to all clients if I'm the server, or to the server if I'm a client. </summary> */
    4.    [Client]
    5.    void SendTransformMessage() {
    6.        if (!hasAuthority) {
    7.            Debug.LogError("SendTransformMessage called on " + gameObject.name + " without authority. InstanceId: " + GetInstanceID());
    8.            return;
    9.        }
    10.  
    11.        if (ClientScene.readyConnection == null) {
    12.            return;
    13.        }
    14.  
    15.        NetworkWriter writer = new NetworkWriter();
    16.  
    17.        writer.StartMessage(CustomNetworkMessages.CustomNetworkTransform);
    18.        writer.Write(netId);
    19.        SerializeIntoWriter(writer, true);
    20.        writer.FinishMessage();
    21.  
    22.        ClientScene.readyConnection.SendWriter(writer, GetNetworkChannel());
    23.    }
    24.  
    ----
    Anyway, I have to find now why it only ever updates the position when I stop moving: https://gfycat.com/ImpressionableShamefulHuemul
     
    Last edited: Aug 8, 2017
  6. vis2k

    vis2k

    Joined:
    Sep 4, 2015
    Posts:
    3,217
    Local player means that this object is the player that you control. It shouldn't be false if this is your local player. It might be false for other players that you connect to the server with. Are you sure it wasn't just another player?

    Thanks, I never got any null issues with connectionToServer, but I believe you and since the original one does it too, I will change it.
     
  7. Saishy

    Saishy

    Joined:
    Jun 3, 2010
    Posts:
    73
    Nope, I'm actually playing alone lol

    You can see here: http://i.imgur.com/33SeUgi.png
    I'm a client, I has authority, and I has localplayer authority, but I'm not a localplayer (who am I?)

    The one from the original works fine in all cases so I don't see a reason to change it anyway.
     
  8. vis2k

    vis2k

    Joined:
    Sep 4, 2015
    Posts:
    3,217
    Can you strip down your project and send me a simple version to look at later? Might be the next UNET bug right there.

    Also before I change to readyConnection - does it help if you check if connectionToServer != null in the original code before sending?
     
  9. Saishy

    Saishy

    Joined:
    Jun 3, 2010
    Posts:
    73
    Depends, can you get a better channel to talk than the unity forums? xD
    Also wanna talk something that might be a new bug in your release.
     
  10. vis2k

    vis2k

    Joined:
    Sep 4, 2015
    Posts:
    3,217
    Uploaded another version with connectionToServer != null check and removed debug messages.
    Let's use this thread a while longer until this grows a bit. Will setup a discord server later.
     
  11. vis2k

    vis2k

    Joined:
    Sep 4, 2015
    Posts:
    3,217
    Update:
    • [2017-08-08] Nondeterministic SceneID bug fixed
      • Problem: OnPostProcessScene assigned SceneIDs based on the order of FindObjectsOfType<NetworkIdentity>(). The order is not guaranteed, which resulted in different SceneIDs each time the Editor restarts. Reconnecting to a build after restarting the Editor would then cause bugs where UNET tried to serialize the wrong objects, often causing 'ReadString out of range' errors etc.
      • Solution: SceneIDs are now based on the recursive sibling index in the Hierarchy. This doesn't change after restarting, only when manually changing the Hierarchy.
    This was the most critical UNET bug that I am aware of. A showstopper for many developers, causing random ReadString/ReadBytes ouf range exceptions without knowing why. I highly recommend everyone here to use the new DLL that I just uploaded, as this bug is not good for your sanity.

    If you like it, consider supporting HLAPI Pro development.
     
    Last edited: Aug 8, 2017
  12. camta005

    camta005

    Joined:
    Dec 16, 2016
    Posts:
    207
    Agreed :p
     
    vis2k likes this.
  13. Stormy102

    Stormy102

    Joined:
    Jan 17, 2014
    Posts:
    495
    I second people saying Unity should hire you. This is the biggest step forward in UNET since its release.
     
    akuno, vis2k and xVergilx like this.
  14. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    1,875
    I agree. But, it's not a step, it's a HUUUUUUUUUUUUUUGE LEAP. That single bugfix of sceneId's is priceless.
     
    akuno, Stormy102 and vis2k like this.
  15. vis2k

    vis2k

    Joined:
    Sep 4, 2015
    Posts:
    3,217
    Thanks guys. My set of projects to test it with is limited, so please let me know if you encounter errors.
     
  16. vis2k

    vis2k

    Joined:
    Sep 4, 2015
    Posts:
    3,217
    Last edited: Aug 8, 2017
    chiapet1021 likes this.
  17. nxrighthere

    nxrighthere

    Joined:
    Mar 2, 2014
    Posts:
    522
  18. Stormy102

    Stormy102

    Joined:
    Jan 17, 2014
    Posts:
    495
    It's a shame that it requires replacing core Unity files - makes it sadly virtually unusable for our main project :(
     
  19. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,133
    Make a editor script?
     
  20. vis2k

    vis2k

    Joined:
    Sep 4, 2015
    Posts:
    3,217
    Will put it on my list.

    There are benefits to just having to replace a DLL. No code modifications necessary and (in theory) you should be able to swap it out with the original HLAPI DLLs again any time soon.

    As long as some NetworkTransform classes are internal that's the only option anyway.
     
  21. vis2k

    vis2k

    Joined:
    Sep 4, 2015
    Posts:
    3,217
    Update: one of my uMMORPG customers found another issue with SceneIDs. I uploaded the last stable version and linked it in the first post. Will provide a stable and a experimental version from now on, so that the experimental one can be tested by as many people as possible before replacing the stable version. Less stress for everyone involved.
     
  22. Saishy

    Saishy

    Joined:
    Jun 3, 2010
    Posts:
    73
    NetworkTransform?? The only thing internal in that one is the RegisterHandle isn't it? Did you do a typo?

    Also I don't think anyone cares about no code modifications and the swap.
    Like, it's cool and all, but swapping actually brings a lot of problems with serialization in the table.
    And code modifications are really simple, people who are doing networking "should" have higher average programming abilities. If not... well they are not going too far anyway :2c
     
    Last edited: Aug 9, 2017
  23. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    1,875
  24. vis2k

    vis2k

    Joined:
    Sep 4, 2015
    Posts:
    3,217
    Progress: found the issue for the other sceneId bug. Implemented a solution yesterday night, going to test it today and then hopefully release later.

    Sorry, NetworkTransport of course!

    Didn't they say they fixed it in a future version?
     
    xVergilx likes this.
  25. Stormy102

    Stormy102

    Joined:
    Jan 17, 2014
    Posts:
    495
    Problem with replacing a DLL is that our team uses Git. And every time we upgrade Unity (which is pretty regularly) we have to change the DLL :( It's still good for my own tinkering with Unity tho - I can always do experimental builds with HLAPI Pro but its not possible for using in a production environment.
     
  26. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    1,875
    Welp, it's 2017.1, still not signs of fixes. So it's actually the same behaviour. Also last post from Unity dev:
    They literally don't know how to fix it, since nobody understands HLAPI there. ._.
     
  27. Saishy

    Saishy

    Joined:
    Jun 3, 2010
    Posts:
    73
    Same reason I'm not using :2
     
    Stormy102 likes this.
  28. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,133
    Can't you write a Editor script? @vis2k if you make a static download page that always contains the latest version. Then you could easily write a editor script that checks for updates every once in a while. And if there is a new one. You download it and replace it (From the editor script). That's assuming you can write to the file while unity is running.
     
  29. Stormy102

    Stormy102

    Joined:
    Jan 17, 2014
    Posts:
    495
    Sounds like a decent idea (kind of like a separate package?) but I think they're all in use when Unity is running. Might be worth looking into @vis2k?
     
  30. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,133
    Just tried, I can access it if I have the editor open. I just need admin rights. Atleast on Windows.
     
    Stormy102 likes this.
  31. Saishy

    Saishy

    Joined:
    Jun 3, 2010
    Posts:
    73
    The people who work with other projects too would kill me lol
     
  32. Driiades

    Driiades

    Joined:
    Oct 27, 2015
    Posts:
    151
    HLAPI is so bad at all.

    NetworkManager need to be inherited from and you need to replace virtual function... Ok it's not the feeling of all the rest of Unity...

    Replaced it by my own HLAPI with use of delegate was what strongly deliver all potential of the LLAPI.

    Same things for Matchmaking included in the NetworkManager ... cut in two differents scripts was better : one for connecting to party (LOCAL, LAN, ONLINE) , one for finding party (Online and LAN. LAN was not possible with the current HLAPI but the LLAPI permit it...)

    And NetworkTransform : totally rewritten including LAN/Online switch for different bandwidth utilization.

    Same for sceneChange : what if clients have to change scene with a load screen ? It's not possible because the server tell nothing to client except that the scene changed.
    What if they have to wait the server before entering to the scene. HLAPI : server change scene and AFTER clients are tell to change scene : you have double the time needed for a client to change the scene : Good Job. You can't even load the scene at 90% on client and wait the server for complete the loading...

    AnimatorSync is a bad coding practice. Most of the time you drive animation by an other stateMachine and/or all animations states don't need to be synchronized (because it transit automaticly from animation to animation ...).


    My opinion : ok it's cool to repair the HLAPI, but you can do better in rewritting it from scratch and only with the LLAPI.
    HLAPI is for beginner, you can't do complex games with that. I don't know how you can do complex network things with few scripts like the HLAPI provide to you.

    That's my opinion, I follow you to see how far the job goes =)
     
  33. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    1,875
    Well, it was an example of how high level library should be written. A bad example.

    Too bad nobody actually told us that it was an example at the time our project has started. Only like 3 years after, Unity dev's mentioned that.
     
    Last edited: Aug 9, 2017
  34. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,133
    I agree, it should have been alot clearer. However, I think it's a good example. If per say a large studio wants to make a own HLAPI for their devs to make networked code easier. Then I can see that being an excelent example.

    And also, I feel the same that I wish I would have been told earlier. Our title is being rewritten to LLAPI. We have only about a week left on that Deadline and it's going really smooth. Fairly straight forward process. No big isuses. Just time consuming.
     
    vis2k likes this.
  35. vis2k

    vis2k

    Joined:
    Sep 4, 2015
    Posts:
    3,217
    Progress: hi guys, nice to see so much activity here! Been researching the scene id issue all day. I am coming very close to a solution that works really well and is almost no change from the original code.

    Well he's the LLAPI developer so can't take that negatively.
    I am pretty sure I can fix it, will look into it after the readstring fix.

    Yes planned for the future. Fixes first, then the 'comfort' features later.

    I thought so too. But to be honest, the more I look into the HLAPI the more I respect the work. This must have been a pain to develop, the Weaver alone is pretty crazy. The architecture is really good. Just feels overly complicated here and there, but that can be improved upon.

    See my above answer. It might have been meant as an example, but it's way too well thought out for 'just an example'. The Weaver alone is not something that you quickly develop for an example, this is really difficult to do and really useful to have when it comes to performance.

    The HLAPI's power is that you can now develop multiplayer games that would've been too big otherwise. For example, my uMMORPG project uses only 5000 lines of code thanks to UNET. My previous C++ MMO project had 10% of the features and 50.000 lines of code.

    If I were to develop a high level networking solution on top of the LLAPI, I would use exactly the same architecture with client&server in one project, syncvars, commands, etc. It just needs cleanup and some more attention, that's all.
     
  36. Stormy102

    Stormy102

    Joined:
    Jan 17, 2014
    Posts:
    495
    There are lots of really neat things about the HLAPI. I love the [Command] [ClientRPC] and [ClientCallback] stuff - really nicely done, along with NetworkMessages and the ability to serialize raw data via OnSerialize/OnDeserialize. But there are several flaws, and many hoops, such as having to initialise the NetworkTransport before creating a server and the over-complex nature of some elements.

    TL;DR Don't execute the HLAPI yet, it's not beyond saving :)
     
    vis2k likes this.
  37. vis2k

    vis2k

    Joined:
    Sep 4, 2015
    Posts:
    3,217
    Update: Experimental version added to first post. It has the new sceneId bug fix. Instead of assigning the sibling index path hash as SceneID, it now sorts the FindObjectsOfType result by the sibling index path. This way it's robust against DontDestroyOnLoad messing with the indices and it's the least invasive fix.

    I tested all the previous scenarios and it works. If you have a minute, please test it in your project and let me know if all the spawning works fine - even after restarting the Editor and connecting to a build again.
     
    Last edited: Aug 10, 2017
    Stormy102 likes this.
  38. Stormy102

    Stormy102

    Joined:
    Jan 17, 2014
    Posts:
    495
    @aabramychev Is there any possibility of merging some of these changes into official UNET at some point in the future? I know you're the LLAPI guy, but you are the only person who seems to routinely respond :p
     
    akuno likes this.
  39. vis2k

    vis2k

    Joined:
    Sep 4, 2015
    Posts:
    3,217
    Update: uploaded a new experimental version, I am sure you guys will like this one:
    • [2017-08-10] Fixed "ServerDisconnected due to error: Timeout" bug.
      • Problem: NetworkManager.OnServerDisconnect throws an error each time a client times out. A timeout isn't really worth throwing an error, since it happens all the time.
      • Solution: NetworkManager.OnServerDisconnect only throws errors if the cause is not 'Ok' and not 'Timeout'. Also improved the log message because it sounded like the server disconnected itself, even though a client disconnected.
      • upload_2017-8-10_12-58-24.png
     
    Stormy102, camta005 and xVergilx like this.
  40. Driiades

    Driiades

    Joined:
    Oct 27, 2015
    Posts:
    151
    Command and Rpc work great. Just you can't have the same script multiple time on the same object .. command/rpc will fail by taking the first one.

    After this, you can't keep a gameObject with networkIdentity alive when disconnecting from server when you have authority on it... this is a problem too. The same things appear when you locally play your game and want to connect to a server without changing scene : you can't keep your character and all networked object... It's a problem when you want a very smooth game.

    If you have a solution for the last one .. I would take it :)
     
  41. Saishy

    Saishy

    Joined:
    Jun 3, 2010
    Posts:
    73
    I just got that bug with [command] and [clientrpc] where if you have multiple classes that inherit from the same it don't know which one to call xD
     
  42. Stormy102

    Stormy102

    Joined:
    Jan 17, 2014
    Posts:
    495
    If you're serious about your networking, I don't think many people will use the NetworkManager. We literally ripped the code straight from Bitbucket and used that as the basis for our NetworkManager. The original code is virtually unrecognisable now... added comments and stripped the crap like WebSockets and all the different Start* functions as well as adding our own custom stuff.
     
  43. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    1,875
    Well, we surely do. Not that we should. Maybe same should be done for our project.
     
    nxrighthere likes this.
  44. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,133
    Due to the Ease of use of the HLAPI it attracts many non experianced developers to create multiplayer games. That's when the NetworkManager is great.
    It's kindof the same deal with the NetworkTransform. It's great to just prototype with. But if you want something for a real project that's going to market. You probably want to rewrite your own. I find that the HLAPI is a great tool for developing. But that the HLAPI components are just good for Prototyping and learning.
     
    Stormy102 and wobes like this.
  45. vis2k

    vis2k

    Joined:
    Sep 4, 2015
    Posts:
    3,217
    That's a HLAPI bug, right? Not something that only happens with HLAPI Pro?
     
  46. Saishy

    Saishy

    Joined:
    Jun 3, 2010
    Posts:
    73
    yep
     
  47. Stormy102

    Stormy102

    Joined:
    Jan 17, 2014
    Posts:
    495
    You should - we were able to add in our own authentication, version checking and the integrity of certain core files. Means that networking is safe from different versions and we can have password-protected servers.
     
  48. Driiades

    Driiades

    Joined:
    Oct 27, 2015
    Posts:
    151
    Password protection are in the HLAPI no ? I used that for my little small bit project.
    Same for version you can send it to matchmaking and disable match not in the same version.

    What you can't do, and it's not super, it's to check a whatever current online version of your game and don't permit client to join game.
     
  49. vis2k

    vis2k

    Joined:
    Sep 4, 2015
    Posts:
    3,217
    Update: uploaded a new experimental version:
    • [2017-08-11] Statically cached Messages removed.
      • Problem: the HLAPI is complicated enough as it is, caching messages statically makes the code so complicated that it's hardly worth an extra allocation. Many of the cached messages are only sent once per client anyway.
      • Solution: messages like 'new CRCMessage' are created when they are needed by the message handler. Less global state, less complexity, cleaner code, easier to debug and to understand.

    "Make it work, then make it fast" - the HLAPI is fast, but it doesn't work too well. All those micro optimizations are useless as long as the HLAPI is full of bugs and too complex to debug. The smart thing to do is to simplify where ever possible so we end up with a code base that can be maintained and understood without going insane.

    Related:
     
    Kaivaan, akuno, Saishy and 3 others like this.
  50. Stormy102

    Stormy102

    Joined:
    Jan 17, 2014
    Posts:
    495
    That Code Quality Measurement :D