Search Unity

Unity MLAPI multiproject structure

Discussion in 'Netcode for GameObjects' started by Lyquifire, Feb 11, 2021.

  1. Lyquifire

    Lyquifire

    Joined:
    Jan 1, 2018
    Posts:
    5
    Hi guys,

    I'm planning on making a multiplayer game, and I was reading the MLAPI documentation.
    I want it to have dedicated server, so the server is going to be its own Unity project.

    What I'm not understanding is the actual difference between the two projects.

    Because according to the documentation the server should have all the behaviors and prefabs that the normal game project does.

    Is that the case? Is the dedicated server project the same as the game one asset wise? same assets have to be imported in both projects? Or scene structure? Levels? I'm sure I'm getting something wrong. I would appreciate some clarification.

    Also in the documentation, there seems to be no mention of specifying a port when hosting a server, or an IP address when connecting to one (unless I missed them), which is odd.

    Thanks in advance for the help.
     
  2. luke-unity

    luke-unity

    Joined:
    Sep 30, 2020
    Posts:
    306
    With MLAPI you can have a dedicated server in the same project or use a separate project for the server and the client.

    If you have them in the same project you can either create a separate scene for your server and client and use different prefabs/ networked behaviours on them or you can use a single scene and just only activate your client related code (graphic objects, input handling etc.) on the client.

    You don't need the same behaviours and prefabs on the server and client side but a) their prefab hash must match and b) they need the same order of networkedBehaviours, these behaviours don't have to be the same script but they must contain the same NetworkedVars and RPCs. All other non networked things in the scene like your level is not affected by MLAPI so there are no requirements there.

    The port for hosting a server and the port+ip for connecting to a server are set inside the Transport. There should be a UNetTransport component on your gameobject which contains the NetworkingManager.
     
    Vulcore and CreativeChris like this.
  3. flekoun

    flekoun

    Joined:
    Jul 13, 2010
    Posts:
    51
    Great explanation, thanks. I am doing it just like luke has described. I am using one project and different scenes and prefabs for server/client. This way I can put logic focused scripts on server side prefabs and UI/textures focused scripts on on client side prefabs. It is not as much headache as it may seems.
    However everywhere I read people are using if(isServer){} else{} statements or definitions like UNITY_SERVER to drive their logic in code and use the same project scenes/prefabs for server and client. Even the creator of MILAPI recommended to me to do it that way.
    In my opinion this might make the project quite a mess. Also you are exposing server logic to the clients eventhougt it is not processed on clients ( probably the definitions can be used to omit server parts during compilation so this might not be a problem?).
    I am still not sure which approach is better. My approach definitely brings a little more complexity to the project but I cant imagine how larger games can be made with if else branching in code everywhere? Like Diablo 3? POE? Starcraft? (MMORPGs are different league all together) Or am I wrong here and this is standart approach and I am only complicating things?
     
  4. luke-unity

    luke-unity

    Joined:
    Sep 30, 2020
    Posts:
    306
    Your approach of splitting server and client code completely into different scripts is completely fine. And in my personal opinion it is indeed way more manageable for larger projects. We're working on samples and tutorials which will teach you how to use both approaches in MLAPI.
     
    MadMojo, JoNax97 and CreativeChris like this.
  5. Vorocity

    Vorocity

    Joined:
    Mar 13, 2019
    Posts:
    2
    I'm also very interested in an example which has the server and client in separate projects. I can't seem to find this info in the current MLAPI docs. I have a code heavy PC project and I want to have a light IOS project which allows an iPhone to connect to the PC game as a controller. Is there a sample project or documentation around this setup? Thanks.
     
  6. MadMojo

    MadMojo

    Joined:
    Oct 16, 2014
    Posts:
    19
    Hi @luke-unity ,

    Has there been any progress on the separate client and server project samples or tutorials that you can share with us yet?
     
    DasOhmoff likes this.
  7. DasOhmoff

    DasOhmoff

    Joined:
    Nov 8, 2018
    Posts:
    11
    I would like to know as well.
     
  8. luke-unity

    luke-unity

    Joined:
    Sep 30, 2020
    Posts:
    306
    Since I wrote that message our plans internally have changed a bit. As a first step, we are now mainly focused on improving MLAPI for more small scale co-op oriented games. These games usually have the server and client in the same project because they most often run in a host/listen-server mode. Our samples such as Boss Room reflect that.

    I'm aware that there is a big knowledge gap due to lack of documentation/tutorials for how to create separate client / server projects with MLAPI. I created an issue in our documentation repository to allow us to keep track of the demand of this feature. Please upvote the issue or leave a comment if you would like us to work on documentation/tutorials for this)
     
  9. flekoun

    flekoun

    Joined:
    Jul 13, 2010
    Posts:
    51
    What I have find out is that it is actually not a big problem to create different project for client/server. The problem is that I could not find a use case where you would need that for a multiplayer project. What I have figured out is that you really don't want to use Unity project as a server instance running dedicated game server. It is just too much overhead. The only use-case might be a MMO style game where there is a one server instance running on a Server as a Unity project and a lot of players are connecting to it. like 1000 or so. That might be ok. But you absolutely don't want to spin up many Unity server instances on one DGS. The worst case would be use-case like 1vs1 or 4vs4 games where low number of players play together and each group need a new unity server instance where they play together. If I understand correctly in this case you don't want to use Unity at all. You want your server instance to be as lightweight as possible and run all login in C++ project for example. If anyone can elaborate on this I welcome it a lot.

    Edit: The solution Luke is describing that MLAPI is focusing on is great but absolutely useless for any authoritative game where you don't want any cheaters. You can make fun games with this approach where one guy is a Host but you must take as a given that anyone can cheat anytime. So no leaderboards, competitive rewards or monetisation based on this in your game.
     
    Last edited: Jun 29, 2021
    joanpescador, Chrispins and Davon92 like this.