Network System for Game Engine OUTDATED
- reynosorobertov
- Nov 16, 2024
- 4 min read
Updated: Nov 25, 2024
OUTDATED GO HERE: https://reynosorobertov.wixsite.com/mysite/post/my-netcode-system



I developed a network system for our 3D game engine, which gives the developer an option to incorporate peer-to-peer online multiplayer functionality for their games.
To use my interface for connecting and creating lobbies, players can either host a server or join as a client. A player can create a server by entering ‘s’, which initializes the server based on the port that they are using. Once the server is running, another player can join by entering ‘c’ designating them as a client rather than the host. The client is then prompted to enter the IP address that is associated with that server’s port to establish a connection. Once connected, the server host can start the game, after which no additional clients can join. Each client, including the host is assigned a unique client ID, which will be linked to specific game objects in the world, as determined by the game’s developer. This ID ensures ownership of those objects, allowing only the designated client to manipulate and move them within the game world.
To set up your project with my network system, start by installing the static library project named ‘Netcode’ and adding it to the Engine portion of your solution. Then, update the project references by adding references to the ‘Physics’ and ‘Math’ static library projects, respectively. Next, in the Property Manager, include ‘EngineDefaults’ for both the Win32 and x64 configurations of the solution. Additionally, add OpenGL to the Win32 configuration and ‘Direct3D to the x64 configuration.
Within the ‘Netcode’ project it includes three header files named Netcode.h, NetcodeManager.h, and NetcodeTools.h. Additionally, it contains one NetcodeManager.cpp file. The ‘NetcodeManager’ is the most important component of this project. As a developer, you will primarily interact and reference its functions. The ‘NetcodeManager’ is designed as a singleton, which ensures convenient access to all necessary functionality.
Within NetcodeManager.h, you will find key variables that synchronize across all clients, including a container of integers named ‘m_connectedClientIDs’, which holds the unique IDs of all clients and host in the game. Each client also has a distinct ‘m_localClientID’, which is part of ‘m_connectedClientIDs’. This structure provides an efficient way to determine ownership of specific game objects in the game world.
To retrieve the local client ID, you can use the ‘GetLocalClientID()’ function, which is essential for making conditional checks to manage how game objects move and interact across the network.
The two critical functions, ‘StartServer()’ and ‘StartClient()’, are responsible for creating the server and client, respectively. These functions ensure that each client is assigned a unique ID, which is then added to the ‘m_connectedClientIDs’ container. This synchronization across all clients prevents discrepancies in game object ownership, ensuring a consistency of ownership between game objects.
Let’s review some of the key functions within the NetcodeManager. If you want to change the server’s port, you can change #define DEFAULT_PORT to whatever you want. ‘BroadcastStartGame()’ is used by the server to send a message to all clients, signaling that the game is starting and preventing additional clients from joining the host’s lobby. ‘WaitForStartSignal()’ should be called after ‘BroadcastStartGame()’. When invoked, it sends a start message to all clients, which includes the ‘m_connectedClientIDs’ container, ensuring proper synchronization across all connected clients. ‘SendPlayerMovementData()’ allows a client to send its movement data to the server, enabling the corresponding game object to move on the host’s side.
‘ProcessAndBroadcastReceivePlayerMovementData()’ processes the movement data for a game object based on its associated ClientID. It then calls ‘BroadcastPlayerMovementData()’, which utilizes ‘BroadcastData()’ to synchronize the game object’s movement and position across all clients. Lastly, ‘SendDataToServer()’ allows a client to send data to the server for synchronization, while ‘SendDataToClient()’ is used to send data that only needs to exist on a specific client.
NetcodeTools.h contains functions for serializing and deserializing vector and sRigidBodyState data, which enables conversion for transmission across the network. Netcode.h provides functions for handling various message types, including Input, SyncState, Acknowledgment, StartGame, and Ownership. These message types help ease the process of communication between the clients and the server. Additionally, Netcode.h includes a function to convert data into a byte array, simplifying transmission over the network.



Above is an example of what you need/could do to set up your ‘MyGame’ portion of your project for a peer-to-peer online experience.
One of the major challenges I faced during this project was understanding and properly utilizing the Windows Winsock2 API, as well as integrating it into the game engine. Setting it up correctly proved difficult, and I encountered significant setbacks due to port issues. Many ports were blocked during development, which severely restricted my ability to perform the necessary testing and debugging to ensure the system worked as intended.
Through this process, I gained a deeper understanding of how clients interact with game objects within a game, particularly the concept of ownership. I learned how each client is assigned a unique ID, which is then associated with specific game objects in the game world. This setup ensures that only the designated client has permission to manipulate or move a particular game object.
Although getting this system to work properly was a frustrating process due to the numerous complications I encountered, I am ultimately happy with my decision to tackle the challenge of building a networked system from scratch. Networking is a field I want to specialize in, alongside gameplay engine development, and this project provided valuable experience in that area.
One of the main things that I used in this project from my learning in this class, was how to properly setup my project to interact accordingly with the other systems within the game engine.
Comments