-
Notifications
You must be signed in to change notification settings - Fork 8
Multiplayer Rookie
This page is directed to people who have never done networking multiplayer or those who are very new to it.
Basic concepts will be explained, if you feel you already have a good grasp of it, please proceed to the next tutorials.
TinyBirdNet uses a Client/Server archetype, this mean that each client is connected to a single server. So if a client wants to send information to another client it will first need to send it to the server, who will redirect the information.
While this may seem cumbersome, having a connection to each and every other client is in most cases not what you need and extremely difficult.
For imagine that you need to make sure all clients are able to connect to each other, have their ports open, handle any connection error, etc...
A good read about different types of networked games can be found here: https://gafferongames.com/post/what_every_programmer_needs_to_know_about_game_networking/
So how exactly is networking multiplayer implemented in games? Is the video streamed to the players? Do we just keep sending every possible information to everyone? Is it the cloud magic?
At a basic level most networked games is mirror and smokes, you normally have huge amounts of data needed to be synced really fast between computers with different speeds. So unless you have a game where time is irrelevant, like a turn based game with timers, you need to up on some tricks.
So, the first issue when making a game work across the network is Latency. Latency, sometimes mentioned as Ping, is the time interval between the stimulation and response. In this case, this means the time it takes for a message to be sent from one computer and it being received in another.
It might also be referred to the time it takes for a message to be sent, plus the time it takes for another message to be sent back. Although this is often referred to as "round-trip delay time", RTD or RTT (round-trip time) to avoid confusion.
Some of you may think, why is Latency more important than Bandwidth? If you don't know yet, Network Bandwidth normally defined as the amount of data able to be sent across the network in a given period of time.
As said before networked games uses a lot of tricks to work, if you are too young you might not known that before we all had internet speed ranging around 56kbps (Around 7 kilobyte per second), while today it's common to have internet speed surpassing 1 megabyte per second (Around 142 times more information per second).
Games then had to deal with sending extremely small packets of data, not much information could be exchanged. Although you might see later that 7kilobytes is a lot of info, this was in excellent conditions, and you couldn't just hug all the resources for yourself.
In a nutshell: Sending the minimum amount of data necessary to interpret the current game state.
There are a variety of ways of doing this, from most simply not sending data that the client don't need to known about (Like not sending data of objects the client can't see nor interact, If it's irrelevant to known, why known it?), packing all data together (We will see in the next part about it), to making predictions about stuff (If someone has been walking forward last update, it is more likely he will keep walking forward until we are certain he has stopped or changed directions).
This can get complicated really fast, but you can take your time learning it.
Some common tricks may be found at this article: https://en.wikipedia.org/wiki/Lag#Solutions_and_lag_compensation
TinyBirdNet uses UDP (User Datagram Protocol) to communicate, UDP uses a little less data than TCP (Transmission Control Protocol) to send information, but it has less safe handling mechanisms, don't worry tho as the LiteNetLib handles that most graciously!
If you were to send an empty message in UDP, meaning no data is actually sent, the packet would contain at minimum 28bytes(IPv4) or 48bytes(IPv6). This is because the packet needs to contain data about the destination and content, we call those the Header of the packet.
Remember that Bandwidth is measured by amount of data per second, but one second would mean you have a 1000ms delay, everything happening one second late for clients. Data is constantly sent across the network instead of being sent every second, this is made by fragmenting packets and this fragmentation may have a huge impact on lower Bandwidth connections.
You can read more about how Bandwidth and Packet size may affect Latency here: https://books.google.com.br/books?id=MLxfy6W8bxgC&lpg=PA193&ots=9FaVhqdM2m&dq=56kbps%20packet%20size&pg=PA194#v=onepage&q&f=false
TinyBirdNet operates on the basis that there is one Server that every Client connects to. On the included demo any Host will also be a Listen Server, meaning they are a Client and a Server at the same time. (A Client connected to their own computer, which is also a Host)
In most cases one would adopt the Server Authority architecture, meaning the Server is the correct version of the game and any important information is validated by it, while Clients try to keep a simulation as close as possible to the Server.
This Server Authority is not provided by TinyBirdNet, it's upon you to implement it or not on your game.