Search Unity

UNET to Mirror/MLAPI

Discussion in 'Netcode for GameObjects' started by PhoenixAdvanced, May 2, 2021.

  1. PhoenixAdvanced

    PhoenixAdvanced

    Joined:
    Sep 30, 2016
    Posts:
    316
    Hi,

    I am researching a replacement for the UNET code in my project, and it seems that MLAPI and Mirror are going to offer the easiest upgrade path.

    The multiplayer component of my project is very simple, I basically have a lot of handlers:


    Code (CSharp):
    1.         NetworkServer.RegisterHandler(123, OnCommandSent);
    2.         NetworkServer.RegisterHandler(124, OnPlayerLogin);
    3.         NetworkServer.RegisterHandler(125, OnUpdateZone);
    and a lot of custom messages, estending from networkmessage:

    Code (CSharp):
    1. public class testmessage : MessageBase
    This is pretty much all I am doing.

    What would be the easiest way to replicate this functionality with any other networking library?

    My knowledge of netcode is not great, so I was hoping for a simpler migration path.
     
  2. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
  3. CreativeChris

    CreativeChris

    Unity Technologies

    Joined:
    Jun 7, 2010
    Posts:
    457
    Hi @PhoenixAdvanced

    Check out the UNet to MLAPI guide: https://docs-multiplayer.unity3d.com//docs/migration/migratingtomlapi

    It should help. If you get stuck, please join our Discord Server for #support.

    Thanks,
    Chris
     
  4. PhoenixAdvanced

    PhoenixAdvanced

    Joined:
    Sep 30, 2016
    Posts:
    316
    Hi, thanks, I read those migration guides, and it seemed the Mirror one got me further (There are already existing functions in mirror for what I was doing), I just had some issues with it.

    It's looking like I will need to replace my "registerhandlers" with rpcs/cmds?

    I might check out the discords, thanks a lot for that!
     
    CreativeChris likes this.
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    mischa2k likes this.
  6. PhoenixAdvanced

    PhoenixAdvanced

    Joined:
    Sep 30, 2016
    Posts:
    316
    That is excellent, I never knew that!

    The one problem I had with mirror (So far, at least) was with the register handlers.

    Lets say I have, as above (On the Server):

    NetworkServer.RegisterHandler(123, TestFunction);

    I can replace that with:

    NetworkServer.RegisterHandler<testmessagenew>(TestFunction);

    And the function would then look like this:


    Code (CSharp):
    1.    
    2.  public void TestFunctiononServer(NetworkConnection conn, testmessagenew msg)
    3.  
    I haven't tested this, but it seems correct.

    The problem is, how do I send specific commands to a client?

    For example, I have this:


    Code (CSharp):
    1.        
    2.  testmessage msg2 = new testmessage();
    3.   msg2.testvariable = "hi from server!";
    4.   netMsg.conn.Send(120, msg2);
    5.  
    Which sends a specifc message using "120" as an ID, so on the client, I can do this:

    clienttest.RegisterHandler(120, onmessagereceived);

    How would I do this with mirror?

    I can send the message like this:


    Code (CSharp):
    1.             testmessagenew msg2 = new testmessagenew();
    2.             msg2.testvariable = "hi from server!";
    3.             conn.Send(msg2);    
    But there is no way to send an ID to register that to a specific function on the client?

    Would I have to modify all of my network messages to contain an ID, and then use a case select to call specific functions?

    Or use ClientRPC's?
     
    Joe-Censored likes this.
  7. PhoenixAdvanced

    PhoenixAdvanced

    Joined:
    Sep 30, 2016
    Posts:
    316
    I've made some progress with this.

    I have created a client and server and can send messages between them, but I still can't figure out how to register multiple handlers for the same message type.

    For example, I get this issue:


    NetworkClient.RegisterHandler replacing handler for testmessage, id=44734. If replacement is intentional, use ReplaceHandler instead to avoid this warning.

    So, it seems that each message type can only be registered to one handler? So if I have, say, 10 functions that accept the same message type, I need to create 10 identical message structs with different names? This seems completely unnecessary to me, I assume I am missing something?

    Another issue I am having is this one:


    Closing connection: connection(49758). Received message Mirror.ReadyMessage that required authentication, but the user has not authenticated yet

    I have been looking through the examples, and I can't seem to find a solution for this either, again, I assume I am doing something obviously wrong?
     
  8. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Yes, only one handler per message.
    But you could have OnMessage that calls an Event that all your 10 handlers hook into.

    RegisterHandler has a 'requiresAuthentication' parameter.
    Meaning that some messages can only be sent after a player authenticated (=logged in) successfully.
    In your case, it sounds like you have an authenticator component that you didn't use.

    Check out the docs and join our discord, almost 10,000 people there helping each other out :)
     
  9. PhoenixAdvanced

    PhoenixAdvanced

    Joined:
    Sep 30, 2016
    Posts:
    316
    I'll definitely check out the discord, thanks!
     
  10. PhoenixAdvanced

    PhoenixAdvanced

    Joined:
    Sep 30, 2016
    Posts:
    316
    "Yes, only one handler per message.
    But you could have OnMessage that calls an Event that all your 10 handlers hook into."


    Is there an example of this anywhere?

    I am having difficulty replicating the unity functionality, where I can just register a handler using a numeric ID.