Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Showcase Mirror - Open Source Networking for Unity

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

  1. surfknasen

    surfknasen

    Joined:
    May 8, 2017
    Posts:
    14
    I was looking through the source code trying to locate the problem why I get a null reference when (after) I disconnect the client. I found that in the StopClient() method, there is a line that sets the client to null after disconnecting it. Then in the OnChangeScene() that gets run after a few lines, it tries running
    . I think this is the reason I get a NullReferenceException in that method. Why do you set the client to null after disconnecting it if the OnChangeScene() method needs it?
     
  2. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,341
    Thanks, will check it out!
     
  3. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,090
    Hey @vis2k I've been using HP for a week or two now and it solved a bunch of my issues, but I just got through setting up a simple lobby (I'm not using the Network Lobby Manager, I just have both scenes set to empty on the network manager and the server pushes a "start game" button after players connect to the match, which calls ServerChangeScene() to tell everyone to switch to the game scene).

    When I do this though, even just playing alone with just me as the host player, when I push that button and it finishes loading the game scene i get the follow, over and over:

    upload_2017-9-23_1-5-26.png

    and my player prefabs are never recreated again in the new scene. Any ideas why that would be happening?
     
  4. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,341
    Probably the same issue that the guy above described. I will look into it asap.
     
  5. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,341
    Update: uploaded a new version:
    • Fixed NullReferenceException in ChangeScene
    Stable and Experimental files are now the same again.

    Thanks for reporting the bug @surfknasen @PrimeDerektive
     
    surfknasen likes this.
  6. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,090
    hrm, I upgraded to the latest but I'm still getting the same error.
     
  7. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,090
    Here's everything in the log that happens when i play by myself as host and click the button to ServerChangeScene to my game map, from before the load until after the new scene is loaded and the error starts:
    upload_2017-9-23_11-18-45.png
     
  8. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,341
    Can you click on the error and copy the exact stack trace in here?
    I added one null check, but maybe another one is still missing somewhere, that's possible.
     
  9. surfknasen

    surfknasen

    Joined:
    May 8, 2017
    Posts:
    14
    It works for me now, although that's for the StopClient() method.
     
  10. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,090
    Sure thing, its the same as it was in my screen from last night:

    upload_2017-9-23_14-10-4.png
     
  11. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,341
    Could you send me your email so I can send you a custom version with more debug.logs to test? This way we would know when it happens.
     
  12. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,090
    Sure thing, sent you a DM.
     
  13. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,341
    Update: uploaded another version with the fix of another NullReferenceException taht @PrimeDerektive found. Good find!
     
  14. wonderfully

    wonderfully

    Joined:
    Jul 18, 2017
    Posts:
    3
    Is websockets + WebGL supported? I created a FPS game and I'm getting those messages after a while with 1-15 players in game:

    SendToTransport failed. error:NoResources channel:2 bytesToSend:13

    (Filename: C:/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)

    SendToTransport failed. error:NoResources channel:3 bytesToSend:117

    (Filename: C:/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)

    SendToTransport failed. error:NoResources channel:2 bytesToSend:13

    (Filename: C:/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)

    SendToTransport failed. error:NoResources channel:3 bytesToSend:117

    (Filename: C:/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)

    SendToTransport failed. error:NoResources channel:2 bytesToSend:13

    (Filename: C:/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)

    SendToTransport failed. error:NoResources channel:3 bytesToSend:117

    (Filename: C:/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)

    ClientScene::OnRPCMessage hash:16749338 netId:2169

    It's seems really random, the server runs fine for 1-2 hours and then suddenly those errors start popping up, and after that the game still runs fine for people already connected but anyone else trying to connect gets a black screen. And it doesn't seem to recover from the errors, they keep popping up forever.

    My channels are set as follow:
    Channel 0: Reliable
    Channel 1: Unreliable
    Channel 2: Unreliable
    Channel 3: State update (I update the position of players with this)

    Even if the buffer gets full, shouldn't it recover after some time?

    Not sure why channel 2 gets full? I only rarely run 2 Command and RPC on that channel.

    I tried to optimize everything to run as few network calls as possible, but the position Cmd and Rpc still get called around 5 times / sec for every player, no choice for a real-time FPS.

    The server is compiled as Windows .exe with websockets and run on a dedicated server that I rent (CPU / RAM / networking never gets use more than 10%), and the clients are run in WebGL (browser)

    If anyone could help me figuring out what's happening, it would be highly appreciated.

    I tried raising the Max buffered packet parameter to 512 instead of 16, same thing! Does that parameter change anything at all?

    Thanks
     
    Last edited: Sep 24, 2017
  15. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,341
    Does it happen when you use the original HLAPI too? Sounds like it would
     
  16. wonderfully

    wonderfully

    Joined:
    Jul 18, 2017
    Posts:
    3
    Yes it does, I tried pretty much everything, was hoping Unet Pro would solve this, it doesn't to bug when it's not websockets.

    The thing is it always bug after a couple of hours, even if the frequency of the messages is the same, could it be a bug with websockets and the buffer?

    I'm thinking of changing to Photon PUN but I'd really like to stick with Unet Pro...
     
  17. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,341
    I recommend changing the global/connection configs and increase some values so that NetworkTransport doesn't run out of resources. If that doesn't work then report the bug to Alex Abramychev.
     
  18. wonderfully

    wonderfully

    Joined:
    Jul 18, 2017
    Posts:
    3
    Thanks for the reply, I tried changing the values directly in the NetworkManager UI, but it didn't do anything, could you point me out how to change the values directly in the code? I'm new to Unity and CSharp.

    Thanks
     
  19. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,341
    Progress: everything runs stable so far. Looking into another HLAPI bug where GameObject/NetworkIdentity SyncVar's become out of sync sometimes. I think several people reported that on the forum already and I encountered it too today.

    Not sure yet how to fix it, might be a tricky one.

    Check out the StartClient/Server/Host scripting reference.
     
    Zullar likes this.
  20. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,090
    my man! You're a hero
     
  21. Vallar

    Vallar

    Joined:
    Oct 18, 2012
    Posts:
    177
    @vis2k I just installed the latest version into 5.6.3 and I am still getting the NetworkManager warnings I have found in 5.6.1 the only difference is that now they surface every time I start the project. I clear them and it is fine. I am using the default Network Manager, I didn't change anything and I don't have a custom or duplicate Network Managers or anything of the such.

    Here is the screenshot:
     
  22. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,341
    Got a lot of messages from people with HLAPI Pro suggestions. Kinda busy with uMOBA/uMMORPG lately, will try to reply to everyone sooner or later though.

    Downwards compatibility isn't guaranteed, I recommend 2017.1 because that's what the HLAPI Pro code is based on.
    It shouldn't show those warnings after you changed the channels properly. Maybe clear / restart unity / resave. Someone else had the same issue and it disappeared at some point.

    The code checks all the reliable/unreliable channels properly as far as I know:
    Code (CSharp):
    1.         public static bool IsReliableQoS(QosType qos)
    2.         {
    3.             return (qos == QosType.Reliable || qos == QosType.ReliableFragmented || qos == QosType.ReliableSequenced || qos == QosType.ReliableStateUpdate);
    4.         }
    5.  
    6.         public static bool IsUnreliableQoS(QosType qos)
    7.         {
    8.             return (qos == QosType.Unreliable || qos == QosType.UnreliableFragmented || qos == QosType.UnreliableSequenced || qos == QosType.StateUpdate);
    9.         }
     
  23. Vallar

    Vallar

    Joined:
    Oct 18, 2012
    Posts:
    177
    Yeah, I reported it before but it went away with a restart. But like I said I DID NOT change anything. It is the DEFAULT manager. No changes. Nothing. I have also mentioned I restarted the editor several times after saving. Plus the screenshot shows it is set up correctly. If this is because 2017, alright, I'll download and test with it.
     
  24. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    Yeah, I've had similar messages popping when first time re-imported on the newer version of Unity. Though it doesn't reoccur when clear->played.
     
  25. Ben_Iyan

    Ben_Iyan

    Joined:
    May 13, 2016
    Posts:
    204
    I'm new to Unity multiplayer (new to Unity, period) so I started out using Photon. I created a simple scene with a single cube in it, which I can translate back and forth across the screen with the arrow keys. Running both instances of the game on the same PC, I was surprised to see that the networked copy of the cube had a noticeable delay after I started moving the "owned" version. I figured that is what I get for free from Photon -- not the fastest server on the planet.

    I tried Unity's HLAPI and it was much worse (choppy movement using only the NetworkTransform component and no interpolation of my own). I decided to try this version and was pleasantly surprised to see how smooth the movement was. Unfortunately, the same delay in the networked cube beginning to move is present. Given that I'm not running this game on a LAN, I'm at a loss to understand why I see the delay, as movement in this case (LAN game, with both instances on the same PC) should be near instantaneous.

    Has anyone else experienced this? HLAPI Pro's NetworkTransform is definitely better than Unity's (props to the dev), but I cannot understand the lag. Any help would be great.
     
  26. Vallar

    Vallar

    Joined:
    Oct 18, 2012
    Posts:
    177
    @vis2k I downloaded 2017.1 and I can report that message doesn't come up at all when I upgraded the project (no change). I guess it is a thing with 5.6

    This will always be the case unless you either have client authority or implement client prediction in a server authoritative setup. If you are using the latter, I can link my recent tutorial on the subject and how I implemented it.
     
    Ben_Iyan and mischa2k like this.
  27. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,341
    You could also modify the UNET configs to send packets directly instead of waiting for a bunch of them to send them out ever 10ms or so. But ultimately you'll need rubberbanding / client side prediction if you want the impression of no delay.
     
  28. Deleted User

    Deleted User

    Guest

    Hello. May I ask you about these configs, where can I find them? Thanks.
     
  29. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,341
    ConnectionConfig/GlobalConfig, check the Unity docs
     
  30. Vallar

    Vallar

    Joined:
    Oct 18, 2012
    Posts:
    177
    I think I found a problem with the NetworkTransform. In the old one when spawning projectiles I would set the SendInterval rate to 0 and choose Sync Rigidbody2D (I am creating a 2D game) and the projectiles on clients moved smoothly. However when I am trying the same exact thing with HLAPI pro the movement stutters quite a bit.

    Any ideas why the interpolation is bad with projectiles on the new HLAPI and was good in the old one?
     
  31. surfknasen

    surfknasen

    Joined:
    May 8, 2017
    Posts:
    14
    I've also been having a bit of problem with the network transform. There does seem to be some interpolation, but it doesn't seem much better than the old. My players' movement gets very laggy the higher their ping get, and it hardly looks like there is any interpolation at all. Is there anything I might have missed? I'm also using 2D Rigidbodies.
     
  32. dadude123

    dadude123

    Joined:
    Feb 26, 2014
    Posts:
    789
    Hey vis
    I've taken a look at this and tried it out. Its a good start, but there is a lot of room for improvement still.

    Allocation is an absolute no-go, especially in something like networking, where there are many objects, each one doing calculations every frame.

    Fortunately the things I've seen can be fixed easily with no impact on readability or complexity.
    Still a bit surprising that this is the case at all (should be immediately obvious while rewriting it).

    General:
    - NetworkWriter and Network reader are instantiated all over the place. Even if the object were small that would not be acceptable. However it is gigantic, allocating more than 1kb initially already.

    NetworkTransform:
    - UnpackUShortIntoThreeFloats allocates an array when it could just output its return values into `out` parameters as well.
    - DataPoint is a class instead of a struct. There is literally no reason for that at all

    NetworkIdentity:
    - RebuildObservers: The hashsets should be pooled or cached, performance will go down really quickly once you have this in an actual game.

    SyncList / SyncListStruct<T>:
    - In this line of code you're not correctly overriding Count. Instead you're creating a new property, which can be hidden if you happen to pass cast it to a SyncList (and that happens quite often usually, since passing something to another function will implicitly cast to an ancestor type if the function declared its parameter as such).
    Add "Count" as virtual (or abstract) to the SyncList, then correctly override it in SyncListStruct.
    This one actually took a while to debug/find because I was not expecting it.



    If you want, I can help with that stuff and more. :) (And by help I mean actually fixing all the problems I listed)

    It's great that someone takes the time to fix all the problems. Also simplifying the code is almost always a good thing, but in performance sensitive scenarios (such as games, and especially networking) you have to be careful that your simplifications don't actually cause problems down the road (which they sorta already did with all those allocations)
     
    nxrighthere likes this.
  33. Ben_Iyan

    Ben_Iyan

    Joined:
    May 13, 2016
    Posts:
    204
    Hi Vallar,

    Thanks for the feedback and, yes, I would very much like to look at your tutorial.

    This will always be the case unless you either have client authority or implement client prediction in a server authoritative setup. If you are using the latter, I can link my recent tutorial on the subject and how I implemented it.[/QUOTE]
     
  34. Vallar

    Vallar

    Joined:
    Oct 18, 2012
    Posts:
    177
    @Ben_Iyan You can find it here. Note however it is aimed at beginners (since I am still in the learning process myself). Hope it helps at all.
     
  35. Ben_Iyan

    Ben_Iyan

    Joined:
    May 13, 2016
    Posts:
    204
    Thanks Vallar. That's a very well written tutorial, btw.
     
  36. Vallar

    Vallar

    Joined:
    Oct 18, 2012
    Posts:
    177
    Oh, thanks. Glad you liked it :)
     
  37. doctorpangloss

    doctorpangloss

    Joined:
    Feb 20, 2013
    Posts:
    270
    I went ahead and put in $10/mo on your Patreon. Keep up the great work. Migrate to GitHub if at all possible, it'll be a lot healthier for the project.

    I can contribute important fixes to weaver too.
     
    runningbird and Deleted User like this.
  38. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,341
    Hey guys, lots of new posts!

    Status Update: implementing Pets for my uMMORPG asset right now and will wade through HLAPI Pro suggestions/reports again afterwards. If anyone finds HLAPI Pro bugs then please provide me with a step by step instruction on how to reproduce them.

    Does it happen with all send intervals?

    Higher ping is pretty much the definition of things being laggy. The interpolation should still work though. Any easy way for me to reproduce it?

    Right now my biggest concern is if my uMMORPG customers and I can release our game to the world or not. There is no point in discussing allocations if we can't even connect to the server without showstopper bugs. IMO we should talk about allocations and performance a year from now if it all runs stable by then.

    About your other points: noted. I didn't touch the SyncList.Count part though, that's original HLAPI Pro code.

    .AsArray was removed because the stream's internal buffer can't (and shouldn't) be accessed from the outside. That's a bug waiting to happen otherwise. Use .ToArray instead, which is safe and still compatible with the original HLAPI in case you switch back again.

    Take a look at the source to find alternatives for the other functions that you mentioned. I highly recommend using the safer ones.

    Thanks. What weaver fixes do you mean?
     
    Last edited: Oct 3, 2017
  39. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,341
    If you mean the GameObject/NetworkIdentity out of sync issue then I doubt it
     
  40. Vallar

    Vallar

    Joined:
    Oct 18, 2012
    Posts:
    177
    Yes, it does. Well, I didn't test with EVERY single interval but 0, 0.1, 0.25, 0.5, 0.9 and 1 have almost the same results. Either the object stutters from the get go and lag is visible, they stutter at the beginning of the movement then things are fine after or don't stutter at the beginning but stutter after 1 second. Basically what I found out (or at least what I am gleaning from this) is that the stutter happens at the moment of sync -- the exact time the object receives new data.
     
  41. surfknasen

    surfknasen

    Joined:
    May 8, 2017
    Posts:
    14
    @vis2k How to reproduce:
    1. Create a new project.
    2. Create a new sprite, add some sprite to it and set the X and Y scale to 5. (This will be the player prefab)
    3. On the player prefab: add a NetworkIdentity (with localPlayer ticked), NetworkTransform (send interval 0.05) and a Rigidbody2D with "Linear Drag" set to 1 and "Gravity Scale" to 0.
    4. Create a script on the player prefab called "Move" and add the following to that script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Networking;
    5.  
    6. public class Move : NetworkBehaviour {
    7.  
    8.     Rigidbody2D rb;
    9.  
    10.     void Start () {
    11.         if(!isLocalPlayer) return;
    12.         rb = GetComponent<Rigidbody2D>();
    13.     }
    14.  
    15.     void Update () {
    16.         if(!isLocalPlayer) return;
    17.         if(Input.GetKeyDown(KeyCode.LeftShift))
    18.         {
    19.             rb.AddForce(transform.up * 100);
    20.         }
    21.     }
    22. }
    23.  
    6. Create a new gameObject and add a NetworkManager and NetworkManagerHUD. Set the PlayerPrefab to your Player Prefab. Set the Simulated Average to 100 millisec.
    7. Build.

    This is how it looks for me (note: it looks a bit extra laggy due to the fps of the gif):
    https://gfycat.com/gifs/detail/WarlikeGlamorousAmericanmarten
     
  42. KiraELL

    KiraELL

    Joined:
    Apr 6, 2017
    Posts:
    25
    Hi @vis2k , I use newest version of hlapi pro, it get me null reference in old hlapi pro im not getting error, its all works and also when i tried to login in the game it does not show Character Selection panel or Character Creation panel and above it said null reference (I tested it n Adnroid device but in unity editor it works without problem) nor in fresh project either not work when i build in android. (im getting this problem when i use newest version of hlapi pro). Thanks

    @Vallar We have same problem it shutter either, when i login in the game it all smooth but when changing scene and get back in main scene my movement is not smooth at all and when i click in the ground character delay to move that position you click, also its lag.

    OS: Win 7 64bit
     
    Last edited: Oct 4, 2017
  43. Vallar

    Vallar

    Joined:
    Oct 18, 2012
    Posts:
    177
    Honestly I tried the NetworkTransform in a few settings and it depends on how you move that it sometimes doesn't work well. Currently I am using pathfinding on the server but on client they just get the position each sync update so NetworkTransform is handling it. Haven't tried with lag (since simulation isn't working) but it seems to hold up good. Will see what will happen when I start putting it up on a server.
     
  44. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    Ok, 2017.2.x UnetWeaver<->Editor is broken with HLAPI Pro. Had to convert back to 2017.1 due to errors.
     
  45. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,341
    Thanks, will take a look soon.

    Please tell me how to reproduce it

    2017.2 HLAPI source is not available: https://bitbucket.org/Unity-Technologies/networking/branches/
     
  46. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    Hopefully they'll upload 2017.2 when it's actually out. Though, I'm not using any of the new features, so staying 2017.1 is fine for now.
     
  47. KiraELL

    KiraELL

    Joined:
    Apr 6, 2017
    Posts:
    25
    @vis2k

    How to reporduce:
    1. I just Download Stable Version (2017-09-23) / My unity version : Unity 2017.1.1p1 (64-bit)

    2. I just follow User Guide instruction. i repleace default UnityEditor.Networking.dll to your hlapi pro UnityEditor.Networking.dl in C:\Program Files\Unity\Editor\Data\UnityExtensions\Unity\Networking
    and i used your Unity.UNetWeaver.dll i paste in on C:\Program Files\Unity\Editor\Data\Managed

    3. I open my project reimport my project ,

    4. I compile as APK file

    5. Install it on mobile phone, and i open it. I just login but not show Character creation nor Character Select. Also give error Null reference notification.

    6. I try in fresh install project of ummorpg. Same it give null refenre.ce no creation/selection panel.

    When i build it as .EXE or standalone no problem when i test no error. in android it give null reference.
     
  48. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,341
    I just tried to build uMMORPG with HLAPI Pro on Android and it works fine for me. Maybe try to find out what exacty the null reference exception says. It probably has nothing to with HLAPI Pro - did you try it with just HLAPI?
     
  49. KiraELL

    KiraELL

    Joined:
    Apr 6, 2017
    Posts:
    25

    @vis2k Yes it works hlpi pro and build it on android without networkzone, i tried this without networkzone addon with no problem, but if you put networkzone addon on it you got null reference.

    How to reporduce with networkzone addon,

    1. i extract networkzone addon in my project
    2. In networkmanager i attach NetworkZone component and drag networkmanager on it.
    3. Create or duplicate World, I duplicate and rename it World2
    4. I change scene path to spawn. to "2" and add World and World2 path. and add World2 scene in build setting.
    5. Add object and attach Networkzone Portal component and add World2 scene path.
    6. ofcourse set dontdestroyonload in Canvas and EventSystem
    7. Build it in android and run it on my phone
    8. Result : Null reference not show character creation panel.

    Edit: This is fresh project with networkzone addon.
     
    Last edited: Oct 9, 2017
  50. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,341
    Don't use the server&play aka host mode on Android. Use the login button aka client mode. You won't be able to run multiple server instances on your phones, that would be pretty weird.

    This is definitely not HLAPI Pro related, please use the uMMORPG forum thread if you have more questions so we don't annoy people here :)
     
    KiraELL likes this.