Multiplayer networking

In this section, we will briefly introduce networking, specifically networking in the context of multiplayer games and Unity. It's worth emphasizing the word briefly as it's not the intention of this section (and chapter) to teach you everything you need to know to create a multiplayer game, but to build up a vocabulary of meanings and concepts so that this chapter makes sense. For more comprehensive details, refer to the official Unity documentation at https://docs.unity3d.com/Manual/UNet.html or dedicated books, such as Unity Multiplayer Games by Alan R. Stagner from Packt Publishing, which can be referred to at https://www.packtpub.com/game-development/unity-multiplayer-games.

Let's start by introducing the main components and how their architecture differs from single-player games. At its core, a multiplayer networked game (from here on referred to as multiplayer game for brevity) consists of components responsible for the following:

  • Discovery
  • Communication
  • Coordination

Discovery is concerned with finding existing games; this can be done through a central repository that tracks the current games or something more dynamic, where each game session advertises itself and is discovered through intentional inspection.

Communication refers to the task of sending and receiving data to and from peers; how it achieves this depends on the topology and protocol. The most typical topology, and one you are no doubt familiar with, is a server and client. This topology is what we use when browsing the internet--the server is responsible for serving multiple clients, where the clients only know of each other through the server (if at all). The alternative to this is peer-to-peer, where each participant communicates with each other (and many variants in-between). Protocol here refers to the way in which data, raw bytes, is transmitted to and from participants. The two main protocols are User Datagram Protocol (UDP) and Transmission Control Protocol (TCP), where UDP is more analogous to firing an email/text/message to someone (connection-less) while TCP is analogous to calling someone on the phone (connection-oriented).

Lastly, coordination is concerned with managing and synchronizing the shared state of the game; this includes networked objects (such as player objects) and game states. It simplifies things if we have a single participant managing a specific object, which avoids having conflicts in their states, that is, allowing the associated virtual player to be controlled only by the participant who created it rather than everyone in the game. Once updated, the entity needs to be synchronized with its counterparts across the network; this is normally done by serializing its state and sending it to any connected peers to have their instances updated.

There are a lot of moving parts and luckily Unity has encapsulated a lot of this in an API called the High Level API (HLAPI). It literately consists of a set of components that handle the aforementioned tasks. We will return to these concepts and components as we walk through building up the example for this chapter, but before that, let's introduce what we plan to build.

Along with HLAPI, Unity also exposes Transport Layer API for more control. HLAPI is sufficient for our example and will be the API we will focus on. It is also worth noting that Unity has made this API open source, and it is available at https://bitbucket.org/Unity-Technologies/networking.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.219.217