Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Discussion The Proper Way to Multithread Network Management?

Discussion in 'Multiplayer' started by midramble, Aug 9, 2023.

  1. midramble

    midramble

    Joined:
    Jan 20, 2017
    Posts:
    9
    Hello, the short and sweet of it, what is the best method for threading a network manager that waits on data coming from other sources. The Jobs System, async/await, coroutine, or something else?

    The long of it, I'm wanting to use NetMQ (ZeroMQ) for netcode for my game as netcode for gameobjects can't handle the ship entities I am spawning that have thousands of child game objects that need to by synced between server and clients, and netcode for entities seems way too daunting as I'd have to rewrite pretty much my entire project it seems to get even simple things to work. In this case NetMQ would have a while(true) loop constantly waiting on data to arrive to act on, so the assumption is that this would need to be in a separate thread as to not interfere with the flow of normal game code. For this separate thread, what would be the "correct" method of implementation, Jobs System, async/await, coroutines, or something else? Or am I going down the wrong path all together? The next friction point would be getting the data back to the main thread, but I assume the thread safe way to do that is with Native Containers, as they, as far as I understand it, have locks and checks to make them thread safe.
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    7,263
    Is Netcode for Entities an option? If so I‘d use that if you really have a computational issue. More likely synching thousands of gameobjects every tick goes way beyond reasonable network bandwidth for a game.

    You can still do this if you move away from thinking you have to synchronize them over the network. The alternative is to use a deterministic simulation that every client can reproduce with minimal data such as just the number of ships of a given type.
     
  3. midramble

    midramble

    Joined:
    Jan 20, 2017
    Posts:
    9
    The thing keeping me away from Netcode for Entities is having to recreate my entire system to be entities, which seems like an impossible undertaking at this point, especially with how confusing the many layers of boilerplate code that comes with entities.

    As for your second suggestion, that's basically what I plan on doing. My ships are parent objects that have thousands of child objects that are pieces of the ship (the player builds ships from these parts). I plan on only syncing the transform for the parent ship object, but my understanding is that I'd also have to "spawn" each child object as a networked object even if their transforms are not synced. From the Unity documentation it was also my understanding that Netcode for Game Objects can only really handle a dozen or so networked objects so my project would be too large. Or is it that it can only handle a certain number of synced transforms and my ships should be fine?

    My original solution to this problem was to use lower level netcode like ZeroMQ and just handle the data transmission for syncing myself as that seemed simpler than the mess that is Netcode for Entities and more powerful than Netcode for Game Objects. My only problem there is the starting point of threading the network listeners, hence why I was trying to figure the best way to thread them via Jobs, async/await, coroutines, or something else entirely.
     
  4. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    700
    Take a look at Unity Transport, that supports Jobs and Burst and I've integrated it as a replacement for NGO (albeit in a rather limited fashion).
     
  5. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    7,263
    The smaller ships shouldn‘t be children of the big ship. Even if the big ship isn‘t moving (if it were, the small ships would move/rotate along with it, which would be odd), the small ships should just roam about freely in their own, non-networked parent object.