- Home /
Networking UNet: Why can we have multiple NetworkClient and how NetworkBehaviour knows which one to use?
Hi,
The title says it all but I'll elaborate. I'm learning to use the UNet network and some of parts the design of creating the connection between client and server confuse me.
You see, when you start a server in UNet you just use the static class NetworkServer and call Listen(). Everything is static so there is no ambiguity there.
But when you want to connect a client to a server you first have to create a new (non-static) NetworkClient object and then connect this NetworkClient instance to the server using Connect(). So:
Why do we have to instantiate a NetworkClient at all? This suggest that we might want to have multiple NetworkClient instances in the same game process. If so, what is the use of it (connecting to multiple servers?)?
If we really can have multiple connections how does this play with NetworkBehaviour synchronisation? How do NetworkBehaviours know on which connection to serialize to?
Since NetworkClients are instanciated do I need to keep a reference of their instance if I want to keep the connection? Otherwise my impression would be that the connection will be garbage-collected.
Regarding my previous question, I've found the NetworkClient.allClients array variable, a static array listing all NetworkClient instances. Would setting this variable to null then trigger a garbage-collect of the client connections?
I just can't get my head around why would a static class suffice for the Server but not for the Client.
For whoever takes the time to answer: Thank you very much! :)
Answer by Mazer83 · Jul 05, 2017 at 05:01 PM
Re: Multiple NetworkClients: Say you're making a turn-based game. You want to have online play, single-player and hot-seat (taking turns on the same device).
For the 1st option, you'll only need one client, (and a server if you're the host). For the 2nd and 3rd option, you're going to need multiple clients on the same process. You could just create a whole different set of classes for single-player and hot-seat play, but this way you don't have to, you can just re-use the architecture you used for online play, but just change the number of clients you spawn.
Re: Garbage collection. Yes, I believe you are correct.
Hopefully, someone can answer your 2nd bullet point question.
Thank you for taking the time to answer my question.
However, I think you are confusing clients and players. NetworkClients are only there to connect to the server and serves as a network Interface. They are not game objects so I'm not sure why you are talking about spawning them.
In your example I really don't see why you would need a *Network*Client in single-player and why would you want to connect 2 times on the same server in hot-seat. I get that design-wise this might appear simpler, but it just does not make sense network-wise. Also it really does not answer my main question which is "How NetworkBehaviour knows which NetworkClient to use if there are many?"..