Search Unity

How to handle being a Client&Server at the same time? And how to handle multiple local players?

Discussion in 'NetCode for ECS' started by PhilSA, Feb 11, 2021.

  1. PhilSA

    PhilSA

    Joined:
    Jul 11, 2013
    Posts:
    1,926
    I'd like to know if there is any info on how to structure a game where you can be a client + server at the same time (you are playing the game as a normal player, but you are hosting at the same time). The play mode tools seem to have this working by creating a client world and a server world, but doesn't that cost twice the simulation cost for nothing? Would it be better to just have a server world but with presentation systems added?

    I'm also wondering how having multiple local players in an online game should be handled, since only one entity can be designated as the commands target. I've read a suggestion about running one client world per client, but once again, isn't it a bit of a waste to have all these systems/jobs run multiple times instead of just once? I'm under the impression that simply allowing multiple command targets would solve this. A DynamicBuffer of command target entities instead of just a CommandTargetComponent, for example
     
    Last edited: Feb 11, 2021
    apkdev likes this.
  2. CMarastoni

    CMarastoni

    Unity Technologies

    Joined:
    Mar 18, 2020
    Posts:
    900
    Multiple local players aren't "natively" supported yet, but it is possible to handle them. You may not even need a buffer of command target. technically.
    The same command target entity can be just a proxy that contains references to the players entities/input buffers.

    The biggest change is how to deal with the command stream that in this case should contains more than one players input from the same connection.
    Another important bits is to properly set the Ghost owners to avoid having the players entities on the same machine.

    At the moment we cannot easily run presentation on the server world or client logic, since many systems does not run on the "server" and converted prefabs might have components stripped away (since aren't needed).
    And I completely agree that there is a waste of computation here, and it is something we are actively look at (while not the current priority).

    On the positive flip side, this dual world logic guarantee the local client-server will work exactly the same as you will connect to a remote host (no shortcut, messages are serialized, etc etc).
     
    PhilSA likes this.
  3. WAYNGames

    WAYNGames

    Joined:
    Mar 16, 2019
    Posts:
    992
    Is it not possible to have each player in a different client world on the same "game" instance ?
    I don't know what you use for input but the new input system handle local multiplayer with split screen, so I would try to incorporate the "create client world & connect to server world" to the "second player join" of that new input system.
     
  4. timjohansson

    timjohansson

    Unity Technologies

    Joined:
    Jul 13, 2016
    Posts:
    473
    Generally there is very few things that run on both the client and server outside of prediction - and when you have a local server you have consistent latency of at most one frame - so the overhead is not as much as you might expect. Like Cristian said there are many things we can and at some point in the future will do to make it perform better in this case.

    Running presentation on the server world should also be possible if you create a bootstrap for it inspired by our ClientServerBootstrap. It will create two separate paths you need to maintain though, so it significantly increases the maintenance and testing effort for - in many cases - a pretty small performance improvement.

    From a simulation point of view it is, but the hybrid renderer does not support having multiple worlds which render something so you'd still need a single "presentation" world.
     
    WAYNGames likes this.
  5. WAYNGames

    WAYNGames

    Joined:
    Mar 16, 2019
    Posts:
    992
    So we would need a headless server world, headless client world per player (kinda like thin client) and a presentation world to render for all player on a split screen. I guess I'll need to dive back into netcode to have a clearer picture of how stuff works :)