CHAPTER 15
Intermediate
Multiplayer and Networking Basics
Updated: May 16, 2026
35 min read
# CHAPTER 15
Multiplayer and Networking Basics
1. Introduction
Taking a game from a single-player experience to a shared multiplayer experience across the internet is a massive architectural leap. You are no longer just coding the rules of a game; you are coding the communication between multiple different realities trying to agree on the same rules. If Player A shoots Player B, both computers must mathematically agree on exactly when and where that bullet hit. In this chapter, we will demystify Multiplayer Networking. We will understand the Client-Server model, learn how variables are synchronized across the internet, and master Remote Procedure Calls (RPCs) to send C# method executions across the web.2. Learning Objectives
By the end of this chapter, you will be able to:- Understand the Authoritative Client-Server architecture.
- Differentiate between a Host, a Client, and a Dedicated Server.
- Understand how Network Transforms synchronize positions.
- Use Remote Procedure Calls (RPCs) to trigger events across the network.
- Understand the dangers of "Trusting the Client."
3. The Client-Server Model (The Referee)
Multiplayer requires a "Server" to manage the rules, and "Clients" to play the game.- The Server (The Referee): This machine holds the absolute truth. If the Server says Player A is dead, Player A is dead. It receives input from clients, calculates the physics, and broadcasts the results.
- The Client (The Player): The player's computer. It is essentially a "dumb terminal." It says, "I pressed the W key," sends that to the Server, and then waits for the Server to reply, "Okay, draw your character moving forward."
4. Hosting vs. Dedicated Servers
- Dedicated Server: A massive computer sitting in a server farm (like AWS). It runs a version of the game with *no graphics*. It purely calculates math for 100 players. (Used for MMOs and Battle Royales).
- Listen Server (Host): Player A starts the game and acts as BOTH the Server and a Client. Player B connects directly to Player A's IP address. (Used for co-op indie games like *Lethal Company*).
5. Synchronizing Variables (State Replication)
If Player A walks to the right, how does Player B see them move? Modern networking libraries (like Unity Netcode or Mirror) handle this automatically via Network Variables.-
Instead of using a standard
int health;, you use aNetworkVariable<int> health;.
- When the Server changes the health variable, the engine automatically packages that data, sends it across the internet 30 times a second, and applies it to Player A's avatar on Player B's screen.
6. Remote Procedure Calls (RPCs)
Variables sync automatically, but what about single events? (e.g., Playing an explosion sound effect). You use an RPC. An RPC is a regular C# method, but you attach a specific networking attribute to it.- ServerRpc: A Client calls this, but the code ONLY runs on the Server. (e.g., "Server, I would like to buy this sword, please check my gold.")
- ClientRpc: The Server calls this, and the code runs on EVERY Client's computer. (e.g., "Clients, the boss is dead, play the victory music!")
csharp
7. Visual Learning: The RPC Flow
txt
8. Best Practices
- Network Authority: By default, the Server owns everything. If a Client tries to move a network bullet, the Server will ignore it. You must explicitly set "Ownership". The Server must grant Client #2 ownership of Character #2, allowing that specific C# script to send movement updates.
9. Common Mistakes
-
Trusting the Client: If you let the Client's UI subtract $100 from their inventory and send an RPC saying
BuySwordServerRpc(Sword), a hacker can easily intercept that code, alter the RPC to say they have infinite gold, and give themselves infinite swords. *Never trust the client.* The client should only send an RPC sayingRequestPurchaseServerRpc(). The Server checks the gold, calculates the math, and syncs the new inventory back to the client.
10. Mini Project: Networked Movement Logic
Objective: Ensure a movement script only runs for the player who owns it. If you attach a movement script to a Player Prefab, and 2 players join, there are 2 Player objects in the scene. If you press 'W', BOTH characters will move forward! You must restrict the C# logic using networking flags.
csharp
11. Practice Exercises
-
1.
What is the fundamental difference between a
ServerRpcand aClientRpc?
- 2. Why is the "Authoritative Server" model the industry standard for preventing hacking in online games?
12. MCQs with Answers
Question 1
You want to ensure that when a player clicks a button, a massive visual explosion occurs on the screens of all 10 players currently in the game. Which networking attribute must you place above the Explode() C# method?
Question 2
When writing a movement script for a multiplayer game, what if statement must be placed at the very top of the Update() function to prevent Player A's keyboard from accidentally moving Player B's character?
13. Interview Questions
- Q: Explain the concept of "Network Authority." Why must a specific peer ID be assigned ownership of a Node before allowing a client to control it?
- Q: Walk me through the security vulnerability of "Trusting the Client." Give an example of how a client-side hit detection script for a sniper rifle could be exploited, and how you would fix it using Server Authority.
- Q: Contrast state replication (Network Variables) with Remote Procedure Calls (RPCs). When would you explicitly choose to use an RPC instead of a Network Variable?
14. FAQs
Q: Do I need to buy a server to test multiplayer C# code? A: No! For testing, you use the IP address127.0.0.1 (Localhost). This tells your computer to send the internet packets directly to itself, allowing you to run two game windows on one PC and watch them interact perfectly.
15. Summary
In Chapter 15, we connected our code to the world. We learned that multiplayer development is built on the Client-Server model, where the server acts as the incorruptible referee calculating the ultimate truth. We utilizedNetworkVariables to achieve continuous positional replication. Most importantly, we mastered Remote Procedure Calls ([ServerRpc], [ClientRpc]), allowing our C# scripts to trigger visual events across the internet. The game is now a shared reality.