Search Unity

  1. 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. SalvoCiri

    SalvoCiri

    Joined:
    Sep 14, 2016
    Posts:
    2
    Hi all,
    i have a big problem using HLAPI PRO. Every time i put [syncvar] on a variable unity give me bad errors.

    Example:
    [sync var]
    public float CurrentHealth

    Error 1:
    InvalidProgramException: Invalid IL code in MyObject:set_Networkm_CurrentHealth (single): IL_000d: call 0x2b00012b
    Error 2:
    Exception in OnStartServer:Invalid IL code in MyObjectObject:set_Networkm_CurrentHealth (single): IL_000d: call 0x2b00012b
    at MyObject.OnStartServer () [0x00007] in D:\Unity Projects\MyProject\MyObject.cs
    at UnityEngine.Networking.NetworkIdentity.OnStartServer (Boolean allowNonZeroNetId) [0x00000] in <filename unknown>:0
    Error 3:
    InvalidProgramException: Invalid IL code in MyObject:OnSerialize (UnityEngine.Networking.NetworkWriter,bool): IL_0052: and
     
  2. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,340
    Sounds like you didn't replace the Weaver dll (also make sure to restart Unity afterwards)
     
  3. SalvoCiri

    SalvoCiri

    Joined:
    Sep 14, 2016
    Posts:
    2
    Thank you so much it works.
     
    mischa2k likes this.
  4. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,340
    Been researching some ClientRpc and TargetRpc related stuff. If anyone cares, this is what a ClientRpc looks like in the final assembly, after the Weaver modified it:

    Code (CSharp):
    1.  
    2.    public void CallRpcMsgLocal()
    3.    {
    4.        if (!NetworkServer.active)
    5.        {
    6.            Debug.LogError("RPC Function RpcMsgLocal called on client.");
    7.            return;
    8.        }
    9.        NetworkWriter networkWriter = new NetworkWriter();
    10.        networkWriter.Write(0);
    11.        networkWriter.Write((short)((ushort)2));
    12.        networkWriter.WritePackedUInt32((uint)Test.kRpcRpcMsgLocal);
    13.        networkWriter.Write(base.GetComponent<NetworkIdentity>().netId);
    14.        this.SendRPCInternal(networkWriter, 1, "RpcMsgLocal");
    15.    }
    16.  
    And a TargetRpc:
    Code (CSharp):
    1.     public void CallTargetMsgLocal(NetworkConnection target)
    2.     {
    3.         if (!NetworkServer.active)
    4.         {
    5.             Debug.LogError("TargetRPC Function TargetMsgLocal called on client.");
    6.             return;
    7.         }
    8.         if (target is ULocalConnectionToServer)
    9.         {
    10.             Debug.LogError("TargetRPC Function TargetMsgLocal called on connection to server");
    11.             return;
    12.         }
    13.         NetworkWriter networkWriter = new NetworkWriter();
    14.         networkWriter.Write(0);
    15.         networkWriter.Write((short)((ushort)2));
    16.         networkWriter.WritePackedUInt32((uint)Test.kTargetRpcTargetMsgLocal);
    17.         networkWriter.Write(base.GetComponent<NetworkIdentity>().netId);
    18.         this.SendTargetRPCInternal(target, networkWriter, 1, "TargetMsgLocal");
    19.     }
    And a Cmd:
    Code (CSharp):
    1.  
    2.     public void CallCmdMsgLocal()
    3.     {
    4.         if (!NetworkClient.active)
    5.         {
    6.             Debug.LogError("Command function CmdMsgLocal called on server.");
    7.             return;
    8.         }
    9.         if (base.isServer)
    10.         {
    11.             this.CmdMsgLocal();
    12.             return;
    13.         }
    14.         NetworkWriter networkWriter = new NetworkWriter();
    15.         networkWriter.Write(0);
    16.         networkWriter.Write((short)((ushort)5));
    17.         networkWriter.WritePackedUInt32((uint)Test.kCmdCmdMsgLocal);
    18.         networkWriter.Write(base.GetComponent<NetworkIdentity>().netId);
    19.         base.SendCommandInternal(networkWriter, 1, "CmdMsgLocal");
    20.     }
    21.  
     
    mons00n likes this.
  5. Driiades

    Driiades

    Joined:
    Oct 27, 2015
    Posts:
    151
    omg a GetComponent ...
    a new writer ...

    Plz, end this xD
    An uint, why ?

    So much information and bandwidth lost.
     
  6. darthbator

    darthbator

    Joined:
    Jan 21, 2012
    Posts:
    169
    Will the 2017.2 fixes and improvements dll function with untiy 2017.3? If not is there any eta on a release? I'm pretty much locking into that version of unity and I would absolutely love to use your project. What you're doing is an incredible service, it's a shame unity isn't paying you to directly integrate this stuff.
     
    Last edited: Mar 5, 2018
  7. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,340
    Just go with fixes-only for now please. HLAPI's future is a bit uncertain right now and I want to see what Unity does with the planned community release before rewriting so many internal things here, so that it's not time wasted.
     
  8. mcegamesmobile

    mcegamesmobile

    Joined:
    Feb 13, 2016
    Posts:
    7
    hoping they take a direction soon, was going to donate to your HLAPI patreon. Have they reached out to you at all about taking over the project?
     
  9. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,340
    No one reached out to me, I just volunteered on the forum thread there.
     
  10. myzzie

    myzzie

    Joined:
    Feb 19, 2015
    Posts:
    13
    Well, if they wont have your back the community definitely will. Keep it up vis2k!
     
    mischa2k likes this.
  11. RikuTheFuffs

    RikuTheFuffs

    Joined:
    Sep 9, 2013
    Posts:
    15
    Hi vis, I'm using unity 5.6.5p1. Could you please tell me wether the library is compatible with this version of unity? Or is it unity 2017-only?
     
  12. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,340
    You could try. Not sure though.
    2017 version had so many HLAPI fixes that it's probably best to upgrade your project though, even if you don't use HLAPI Pro.
     
  13. jethrogillgren

    jethrogillgren

    Joined:
    Jan 11, 2017
    Posts:
    28
    Hi, I have the same problem as Peter-Dijkstra - Lots of ReferenceRewriter errors https://pastebin.com/qXUPcCUB
    I tried the link you shared - following their build instructions on a project after following yours, and it built and ran OK.
    Please note I am also building to UWP, Which uses a slightly cut-down version of .NET. Unity 2017.3.1p1 Windows10.


    Can i also ask, what is the difference between that link and the built-in unity networking?
    The UNET problems are killing my project, I wish I'd read about this S***show before investing into it.
     
  14. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,340
    Can you please be more specific: are you talking about a HLAPI or HLAPI Pro issue?
    Which HLAPI Pro version - fixes & improvements or fixes-only?
    Can it be reproduced easily?
    Can you try without a modified .NET version? Otherwise this is hard to debug.
     
  15. jethrogillgren

    jethrogillgren

    Joined:
    Jan 11, 2017
    Posts:
    28
    Hi, apoligies if my terminology is off.
    I have previously been using whatever the default Unity Networking is, and am trying to move to your HLAPI-Pro version.
    I downloaded the Unity 2017 Fixes-Only version of the code from the first post in this thread, into Unity 2017.3.1p1. That then threw the ReferenceRewrite errors when I try to build.

    Reproducing: It happened both times I tried to build. You need a Windows10 machine to build to the specific I'm building (UWP/Hololens - same as the other guy). Not sure about all UWP apps, but Hololens UWP apps you need Win10...

    You have to use the different version of .NET for any UWP Unity build.

    Thanks for any help.
     
  16. AwesomeX

    AwesomeX

    Joined:
    Sep 10, 2013
    Posts:
    115
    Not entirely sure if this is UNet's fault, or possibly a problem caused by using UNet pro.

    Basically when I assign local authority of a vehicle to a client, the transform doesn't sync anymore.

    Only when the "server" has authority and is driving the vehicle object does the transform re-sync.

    The client can "drive" the vehicle just fine, it just doesn't sync the transform to the server.
     
  17. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,340
    Can you try this: https://bitbucket.org/Unity-Technologies/networking/branches/ and see if the error happens with those dlls too or not? (might need to build it first)

    Will look into it
     
  18. RikuTheFuffs

    RikuTheFuffs

    Joined:
    Sep 9, 2013
    Posts:
    15
    Thanks for replying :) I'll try to, but since I'm working on a very big (and live) title with other 20 people, therefore this kind of upgrade is not very easy to do instantly due to breaking changes. I'll try to and let you know :D
     
  19. jethrogillgren

    jethrogillgren

    Joined:
    Jan 11, 2017
    Posts:
    28
    I tried that and it works OK. I used the 2017.3 branch
    That is the code I meant when i said "I tried the link you shared - following their build instructions on a project after following yours, and it built and ran OK." - you had shared that link to the previous guy with the same problem. Sorry to confuse!

    But yeah, that link works OK.
     
  20. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,340
    Ok.
    I don't have a Windows computer so that'll be a tricky one.
    Could you possibly download the HLAPI Pro fixes & improvements repo and then try a couple of previous commits to see when the error started? There aren't that many code changes, it should be an easy fix once we know what causes it.
     
  21. AwesomeX

    AwesomeX

    Joined:
    Sep 10, 2013
    Posts:
    115
    Just an update. I ended up using a custom script for transform syncing, using just SyncVars, and that fixed my issue with losing position/rotation updates when an object's authority was changed to a client. So I'm assuming it has something to do with the NetworkTransform component. However, I'm not certain if this issue persists with UNet's standard network transform script, or if it's related to any changes done with UNet Pro.
     
  22. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,340
    Please try with default HLAPI and please try to explain the easiest possible way to reproduce it for me in a fresh project.
     
  23. Shinyclef

    Shinyclef

    Joined:
    Nov 20, 2013
    Posts:
    498
    I came to the forums to report what I think is a bug. But then I thought "seriously? am I expecting the HLAPI to get attention?" So I remembered this project and tried the fixes release. Unfortunately it didn't solve my problem so I thought my best chance is to ask our unofficial HLAPI supporter vis2k haha.

    The docs say that the Start() function is meant to be called after the other network callbacks (OnStartClient, OnStartLocalPlayer and OnStartAuthority).

    https://docs.unity3d.com/Manual/NetworkBehaviourCallbacks.html
    A few weeks ago I started getting errors when joining the server from a client, but only sometimes.
    Eventually I found the cause, the Start() function was called before the OnStartLocalPlayer function. I tested with debug logs that print out the frame in each callback. Sometimes Start() would be called one frame earlier, but sometimes several frames earlier.

    I can't see any way it could be my code causing this problem as it's unity that calls these callbacks. Is there any known bug that might explain this?
     
  24. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,340
    Would be best to have a very simple example scene where you can reproduce that issue.
    I can see when OnStartServer is called in the HLAPI, but I can't see when Start is called by Unity so I am not entirely sure.

    From my experience, Start was always called after the others. But who knows, maybe you found a bug.
     
  25. moco2k

    moco2k

    Joined:
    Apr 29, 2015
    Posts:
    294
    As it regards callback orders, you might also want to take a look into these threads #1 #2 #3. There seems to be an issue with UNET which can cause different callback orders between Editor and Builds.
     
    mischa2k likes this.
  26. Shinyclef

    Shinyclef

    Joined:
    Nov 20, 2013
    Posts:
    498
    Wow reading these threads makes me think the best thing for me to do is create my own callbacks, one called ClientSetup, called by OnStartClient or Start (whichever is called first), the other called ClientReady, called by the latter of OnStartClient and Start.

    I know unity will call both methods, just the order is inconsistent. What a dirty hack for something that should not be a problem in the first place lol.
     
  27. antsonthetree

    antsonthetree

    Joined:
    May 15, 2015
    Posts:
    102
    I am trying to build the 2017.3 release from source. Everything compiles fine, but after I copy the dlls to the Unity folders, the Unity Editor will not load any project and crashes with "Unity Editor has stopped working."
     
  28. antsonthetree

    antsonthetree

    Joined:
    May 15, 2015
    Posts:
    102
    FWIW I was able to get some success by replacing my compiled version of the weaver dll with your precompiled version. The other dlls work fine when I compile them locally. That is fine for me for now. I'm using Visual Studio 2017. Not sure if theres anything special I need for that.
     
  29. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,340
    You need to replace all DLLs yes, weaver and the others
     
  30. MostHated

    MostHated

    Joined:
    Nov 29, 2015
    Posts:
    1,227
    Hey there, I am guessing since there is not a version of "Fixes and Improvements" for 2017.3 I am out of luck on that? I figured the 2017.2 one won't work right? I am also guessing that raising syncvars to 64 and things of that nature end up being the improvements that 17.3 doesn't have?
     
    Last edited: Mar 18, 2018
  31. Dazz2000

    Dazz2000

    Joined:
    Apr 27, 2014
    Posts:
    19
    ty for the scripts vis
     
    mischa2k likes this.
  32. esp_sys

    esp_sys

    Joined:
    Nov 19, 2016
    Posts:
    6
  33. MostHated

    MostHated

    Joined:
    Nov 29, 2015
    Posts:
    1,227
    I would imagine you would have to get the one that is made for the version of Unity you are using?
     
  34. MostHated

    MostHated

    Joined:
    Nov 29, 2015
    Posts:
    1,227
    ------ Edit : Nevermind, after all this time I finally decided to try upgrading from 2017.30f3 to the newest version, everything was all fixed up. I typically dread changing even version up a little for updates but am glad I finally did.


    I keep getting the following when I use the 2017.3 Fix on 2017.30f3 trying to use any sort of network interpolation/smoothing. : (

    Code (CSharp):
    1. Error] OnDeserialize failed for: object=Player_M1(Clone) component=NetworkSyncTransform sceneId=0 length=0. Possible Reasons:
    2.   * Do NetworkSyncTransform's OnSerialize and OnDeserialize calls write the same amount of data(0 bytes)?
    3.  * Was there an exception in NetworkSyncTransform's OnSerialize/OnDeserialize code?
    4.   * Are the server and client the exact same project?
    5.   * Maybe this OnDeserialize call was meant for another GameObject? The sceneIds can easily get out of sync if the Hierarchy was modified only in the client OR the server. Try rebuilding both.
    6. System.IndexOutOfRangeException: NetworkReader:ReadByte out of range:NetBuf sz:0 pos:0
    7.   at UnityEngine.Networking.NetBuffer.ReadByte () [0x00000] in <filename unknown>:0
    8.   at UnityEngine.Networking.NetworkReader.ReadUInt32 () [0x00000] in <filename unknown>:0
    9.   at UnityEngine.Networking.NetworkReader.ReadSingle () [0x00000] in <filename unknown>:0
    10.   at UnityEngine.Networking.NetworkReader.ReadVector3 () [0x00000] in <filename unknown>:0
    11.   at NetworkSyncTransform.OnDeserialize (UnityEngine.Networking.NetworkReader reader, Boolean initialState) [0x00000] in <filename unknown>:0
    12.   at UnityEngine.Networking.NetworkIdentity.OnDeserializeAllSafely (UnityEngine.Networking.NetworkBehaviour[] components, UnityEngine.Networking.NetworkReader reader, Boolean initialState) [0x00000] in <filename unknown>:0
     
    Last edited: Mar 19, 2018
  35. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,340
    Please use the fixes-only version for now. It still has the new NetworkTransform and the 64 syncvars too.

    Fixes-only for your Unity version
     
  36. Barbonx

    Barbonx

    Joined:
    Nov 8, 2017
    Posts:
    1
    Hello.

    I have some problem when import dll in my project, i have follow the guide and all directory are correct.
    I try to open the same project in an other computer and it work correctly, but when i try to open it in the office pc Unity crash.
    Also i have try to open a new project and it works.

    Thank you for help and good job.
     

    Attached Files:

  37. MostHated

    MostHated

    Joined:
    Nov 29, 2015
    Posts:
    1,227
    Thanks for the info. I am now using Unity 2017.3 1f1. (I didn't try it before I updated so I am not sure if it did the same thing in 2017.3 0f3) When using a deterministic server, my movement is typically just fine but when I stop, about 0.5 to 1 second later my character will move ever so slightly to a new position that is probably 0.1 - 0.2 from where I stopped.
     
  38. Smiderplanen

    Smiderplanen

    Joined:
    Sep 11, 2017
    Posts:
    2
    When i use ur networktransform with rigidbody i use addforce it dosent sync good the player flyes away and then teleport back to position. what should i do?
     
  39. MostHated

    MostHated

    Joined:
    Nov 29, 2015
    Posts:
    1,227
    When using a deterministic server, if you try to add force locally on the client and not from the server, the server will put you back to where you are supposed to be. The server has to move you, the client only requests that you be moved using input methods.
     
    mischa2k likes this.
  40. antsonthetree

    antsonthetree

    Joined:
    May 15, 2015
    Posts:
    102
    In HLLAPI PRO NetworkManager.cs in the overloads for StartHost, you are calling OnServerConnect in the body for StartHost(config, maxConns), but you are not calling it in the bodies of the other two - StartHost(matchInfo) and StartHost(). Not sure if that is a bug from the original HLAPI, or if it is intentional. Just seems worth noting.
     
  41. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,340
    Probably happens in original HLAPI too
     
  42. RayKatz

    RayKatz

    Joined:
    Oct 18, 2013
    Posts:
    13
    So, I started using this, finally. After some initial confusion on where suddenly all those options on the network transforms went, and some fixing of child transforms, i rebuild my game, and tested it.

    Now here is where I don't understand it. For some reason, players who join the game can't move anymore, while the hosting client has no problem. In my understanding (and I'm no expert at all in that regard), there should be nearly no difference between any clients, no matter if they are also hosting or not. So it's kinda baffling to me why that happens, since that wasn't a problem before switching to HLAPI Pro. I'm sure there is a way to fix it somehow, but I have yet to find the culprit.

    Is there anything code wise that works different in HLAPI Pro that would need changing?
     
  43. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,340
    Not really. What kind of movement do you use?
    I could easily go back to the original NetworkTransform if needed. IMO it's unusable for any game though, nobody likes movement without interpolation.
     
  44. RayKatz

    RayKatz

    Joined:
    Oct 18, 2013
    Posts:
    13
    It's really strange. It's not that it doesn't react. Debug messages say it's all fine.

    My characters get moved by moving their rigidbodies with MovePosition and MoveRotation, and their "turret" (a child object synced with transform child) is moved by their transform with LookAt. Every player has authority over their own Player.

    Turning the network transforms off lets the players move again, so I see those as the culprit. That seems very strange to me however, since the players should have full authority over their own PlayerObject.
     
    Last edited: Mar 23, 2018
  45. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,340
    And with default HLAPI it works?
     
  46. RayKatz

    RayKatz

    Joined:
    Oct 18, 2013
    Posts:
    13
    Yes, I double checked it with the default HLAPI and it works just fine with it.
     
  47. MostHated

    MostHated

    Joined:
    Nov 29, 2015
    Posts:
    1,227
    Any idea why when using a deterministic server, my movement is typically just fine but when I stop, about 1 to 2 second later my character will move ever so slightly to a new position that is probably 0.1 - 0.2 from where I stopped?
     
  48. RayKatz

    RayKatz

    Joined:
    Oct 18, 2013
    Posts:
    13
    This is a short version of the code that is working on the host, but not the clients:

    Code (csharp):
    1.  
    2. void FixedUpdate()
    3.     {
    4.         if (isPaused != null)
    5.             if (isPaused.value)
    6.                 return;
    7.  
    8.         if (!hasAuthority)
    9.             return;
    10.  
    11.         RotateTurret();
    12.  
    13.         if (isStunned)
    14.             return;
    15.  
    16.         MoveTank(Input.GetAxis("Vertical"));
    17.         RotateTank(Input.GetAxis("Horizontal"));
    18.     }
    19.  
    20. void MoveTank(float _speed)
    21.     {
    22.         rb.MovePosition(rb.position + rb.transform.TransformDirection(new Vector3(0f, 0f, _speed * Time.fixedDeltaTime * speed)));
    23.     }
    24.  
    25. void RotateTank(float _speed)
    26.     {
    27.         rb.MoveRotation(rb.rotation * Quaternion.Euler(0f, _speed * Time.fixedDeltaTime * turnSpeed, 0f));
    28.     }
    29.  
    THis works on its own, but once the PlayerObject gets the NetworkTransform component, it can't move anymore if it's not the hosting player.
     
    Last edited: Mar 24, 2018
  49. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,340
    Could you describe the easiest way to reproduce this issue in an empty project? E.g. 'add networkmanager, add player, add this script, press play, see the bug'
     
  50. Arkade

    Arkade

    Joined:
    Oct 11, 2012
    Posts:
    654