Basics Of NetWorking
Game networking generally follows two paths depending on the platform and the "twitch" requirements of the gameplay.
1. WebSockets
- Best for: Web-based games, real-time UI data, and persistent chatrooms.
- The Catch: WebSockets run on top of TCP. They are great for reliability and getting through firewalls (since they use standard HTTP ports), but the overhead makes them less ideal for high-speed action.
2. UDP & TCP
- TCP (Transmission Control Protocol): The "Safe" path. It handles packet loss and ordering for you, but it’s slow. In fast games, we only use this for things that must arrive exactly as sent, like chat messages, inventory transactions, or login data.
- UDP (User Datagram Protocol): The "Fast" path. Used by shooters like CS:GO and Call of Duty. It’s "fire and forget"—it doesn't care if a packet gets lost or arrives out of order. This makes it incredibly fast but dangerous to use for critical data.
What is Reliable UDP (RUDP)?
Most modern games use what you might call "Protected" or Reliable UDP. This is a custom layer built on top of raw UDP that selectively adds the safety of TCP back in, without the massive latency penalties.
How do I learn it?
Since RUDP is just a logic layer on top of a UDP socket, you must start with a solid foundation in raw UDP.
Key concepts to master:
- Sequence Numbers: Labeling every packet so the receiver knows the order.
- ACKs (Acknowledgments): The receiver sends a small packet back saying "I got packet #42."
- Data Validation: Using Checksums to ensure the data wasn't corrupted during the "fire and forget" phase.
- Packet Re-sending: If the sender doesn't get an ACK for a critical packet (like "I fired my gun"), it sends it again.
Recommendation: Don't Reinvent the Wheel
Building a robust RUDP layer from scratch is an "Engine-Level" task. If you want to see how it's done or build on top of a proven system, look at these low-level libraries:
- ENet: A classic, lightweight C library that provides a solid RUDP layer. It’s a great place to start reading source code to see how "protection" is actually implemented.
- GameNetworkingSockets: Valve's internal (Steam) networking layer. It's open-source and handles everything from reliability to encryption.
- Lidgren.Network: The gold standard for C#/.NET developers looking for RUDP.
Engine-Specific Implementations
If you aren't building your own low-level socket handler with ENet, you’ll likely be using the built-in systems for Unreal or Unity. Both engines use UDP by default but provide a "Reliable" layer for critical data.
1. Unreal Engine (C++)
Unreal’s networking is built into the core of the engine via the NetDriver. It uses a "Bunch" system to handle reliability.
- The System: Replication and RPCs.
- Reliable vs. Unreliable: When you define an RPC in C++, you specify its reliability:
UFUNCTION(Server, Reliable) // Acts like TCP (Guaranteed delivery/order)
void Server_SubmitScore(int32 Score);
UFUNCTION(NetMulticast, Unreliable) // Raw UDP (Fast, might vanish)
void Multicast_PlayExplosionEffect();
- The "Why": Unreal uses Bit-level serialization and a unique "Channel" system. Each Actor has its own channel. If a "Reliable Bunch" is lost, the channel pauses until that specific data is re-sent, but other channels (other actors) can keep moving. This prevents a single lost packet from lagging the entire game state.
2. Unity (C#)
Unity’s modern networking revolves around the Unity Transport Package (UTP) and Netcode for GameObjects.
-
The System: Unity Transport (UTP).
-
The Pipeline: Unity uses a concept called Pipelines. You don't just "send a packet"; you send it through a pipeline that adds the "Protection" layer.
- ReliableSequencedPipelineStage: This is Unity's RUDP. It adds sequence numbers and ACKs.
- UnreliableSequencedPipelineStage: UDP speed, but ensures older packets don't overwrite newer data if they arrive out of order.
-
The "Why": Unity Transport is Data-Oriented (DOTS compatible). It is designed to be incredibly lean and allows you to create custom "Stages" (like a stage for encryption or a stage for compression) to sit on top of your UDP socket.
3. Godot 4 (GDScript / C++)
Godot 4 comes with a high-level networking stack built on top of ENet by default (ENetMultiplayerPeer), along with WebSocket and WebRTC adapters.
- The System: Built around the
MultiplayerAPIand the SceneTree hierarchy. - RPC Syntax: Configured via the
@rpcannotation (as noted in Intro Into Gdscript):
# Runs on peers; reliable transmission (like TCP)
@rpc("any_peer", "call_local", "reliable")
func update_player_name(new_name: String) -> void:
player_name = new_name
# High-frequency positions; unreliable fire-and-forget (raw UDP speed)
@rpc("authority", "call_remote", "unreliable_ordered")
func sync_position(pos: Vector3) -> void:
global_position = pos
- Modern Nodes: Godot 4 added
MultiplayerSynchronizer(handles automated property replication at configurable intervals) andMultiplayerSpawner(automatically instantiates networked scenes across clients).
⚡ Advanced Netcode Architecture Topics
For fast-paced action games, basic RPCs aren't enough. You need systems to hide internet latency and minimize bandwidth:
- Client-Side Prediction & Server Reconciliation — Authoritative servers, immediate local response, input history buffers, rollback reconciliation, and remote entity interpolation.
- Network Serialization & Delta Compression — Bit-packing, float quantization, delta compression, and Odin
#packedstruct definitions.
Summary Table: Networking Choice
| Feature | Unreal Engine | Unity (UTP) | Godot 4 |
|---|---|---|---|
| Language | C++ | C# | GDScript / C++ / Odin |
| Default Protocol | UDP (custom RUDP / bunch driver) | UDP (Unity Transport) | UDP (via built-in ENet peer) |
| Reliability Method | Reliable RPCs / Property Replication | Reliable Pipelines | @rpc("reliable") / MultiplayerSynchronizer |
| Best For | AAA shooters, massive worlds | Mobile, cross-platform, modularity | Indie multiplayer, rapid prototyping |
| Low-Level Access | ISocketSubsystem / Sockets | NetworkDriver | Direct PacketPeerUDP / StreamPeerTCP |
References & Further Reading
- Unreal: Netcode Deep Dive by Cyrex - An excellent look at how Unreal handles bunches and channels.
- Unity: Unity Transport Documentation - Official breakdown of the transport layer and pipelines.
- General: Gaffer on Games- The "Bible" for anyone wanting to build their own RUDP layer from scratch.
🔗 Related Notes
- Index for Unreal — Unreal Engine C++ architecture.
- Types in Odin — Using
#packedstructs for binary network packet payloads. - GDExtension Notes — Integrating C/C++ networking libraries like
GameNetworkingSocketsinto Godot. - Game Development Hub — Central game development index.