Skip to content

DOTS and NetCode

Nicholas Maltbie edited this page Aug 20, 2020 · 2 revisions

Unity's Data Oriented Technology Stack is the core of this project. In addition, since this is a multiplayer first game, we are using the Unity NetCode and Unity Physics preview packages for this game.

Data oriented programming is similar to object oriented programming. In object oriented programming, you create objects which have contain data, behaviors, and relationships. In data oriented programming, this is broken into separate parts where you have data in a group, and behaviors that are applied globally to the data. You can watch videos or read more about Unity's DOTS overview by looking at their Entity Component System (ECS) tutorial. You can also read their manual for ECS Overview from Unity. The entity component system is Unity's implementation of data oriented programming and is part of the Data Oriented Technology Stack.

Terminology

  • Data Oriented Technology Stack (DOTS) - Name for all of Unity's data oriented packages, overviews, and code.
    • Entity Component System (ECS) - Unity's core platform for implementing the Data Oriented Technology Stack.
    • Unity NetCode - The Unity networking package utilizing ECS and DOTS
    • Unity Physics - The Unity package for implementing physics (primarily 3D) into a Unity project.

ECS Overview

The Entity Component System (ECS) is the core platform for supporting the Data Oriented Technology Stack. This consists of Entities, Components, and Systems.

  • Entities - These are things that exist in the game world (think like a box, player, wall)
  • Components - Data associated with an entity. Although this data is organized independent of the entities in a more optimized manner. Think of this being like a table in a database where a "entity" is a user but they might have an entry in the Users table and another associated entry in the Transactions table.
  • Systems - This is the game logic that transforms component data from the current state to the next state. A system might update the velocity of a character based on a user input and another system might move a character based on the character velocity.

Utilizing this kind of data oriented programming allows for great performance. It is naturally parallel and can support exporting jobs to dedicated hardware such as GPU for intense computations. This leads to natural and significant performance increase and allows for the game to be run on older hardware without putting as much strain on the system (including mobile devices).

For a more in depth overview of ECS and how it works, please refer to Unity's Video Series or their ECS Manual.

NetCode

The Unity NetCode package is a Unity package that can be used to make a game have multiplayer support. The NetCode package relies on the Unity Transport package to handle lower level network communications and connections.

With NetCode, the game will essentially run a shard simulation of the game. The whole project is organized around this shard simulation. By shared simulation, this means that each player has part of the full information (including the server) and data is sent between clients and the server to synchronize the game state. This is done so players can have actions take place without having to worry about their or another player's slower connection. An example of this is when a player tries to move their character, they will have it move on their own machine right away, send a command to do the same on the server, and then the server will send the updated player position to all other clients. (For more about timing and synchronization, look into the Time Synchronization page from the Unity NetCode manual).

Key Concepts

This kind of separation leads to a natural division of Client, Server, and Interpolated (Other clients). A client side piece of code might be something like the User Interface. A server part of code might be something like the initial game state. An interpolated part may be predicting another player's position based on their previous velocity. All of these components work together to build a simulation of the game between the clients and the server.

Another key concept is Ghosts. Ghosts are how Unity synchronizes objects between clients and server. An example of what would be a ghost would be something that can move in the game such as a box players can push. An example of something that is not a ghost would be a static wall. Every client knows where it will start and knows it will never move. These ghosts are defined using a Ghost Authoring Component that sets out rules for how the different components of an entity are shard and synchronized. These ghosts are then combined into a Ghost Collection which is all the possible ghost objects that can exist on a client and server.

RPC Commands are how users send commands to the server. This might be something simple like a command to connect/disconnect. Or something more complex like a game action.

Prediction is how the server and client synchronize the locations while sending less data and presenting the best guess of where something will be. This works great for something like a ball rolling down a hill. The client can predict the location as it's relatively easy to guess. Something like a player controlled vehicle may not work as well for this client side prediction.

Sample Projects

Learning by example can be very powerful. This project may be a bit complicated to understand at first glance. The Project Organization and DOTS Systems pages should help demystify these but Unity also has a set of example projects to help learn dots.

Clone this wiki locally