Skip to main content
Unreal Engine 5 – Complete Beginner to Advanced Guide
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 a NetworkVariable<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
1234567891011121314151617181920212223
// Example using a generic networking structure

// 1. Client asks the Server to fire a gun
[ServerRpc]
void RequestFireServerRpc()
{
    // Server checks if we have ammo
    if (ammo > 0)
    {
        ammo--;
        // Server tells EVERYONE to play the visual effect
        PlayShootEffectClientRpc(); 
    }
}

// 2. Server forces all Clients to run this visual code
[ClientRpc]
void PlayShootEffectClientRpc()
{
    // This happens on Player A, B, and C's screens simultaneously!
    Instantiate(muzzleFlash, gunBarrel.position, Quaternion.identity);
    audioSource.Play();
}

7. Visual Learning: The RPC Flow

txt
12345678910111213
[ Player A (Client 1) ]
Presses &#039;Fire' Button -> Calls 'RequestFireServerRpc()'
         |
    (Internet)
         |
         v
[ Server (The Host) ] -> Receives RPC, checks Ammo, executes &#039;PlayShootEffectClientRpc()'.
         |
    (Internet)
         |
         v
[ Player B (Client 2) ] -> Receives RPC -> Automatically executes &#039;PlayShootEffect()'.
Player B sees Player A shoot!

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 saying RequestPurchaseServerRpc(). 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
12345678910111213
class NetworkPlayer : NetworkBehaviour // (Using generic netcode structure)
{
    void Update()
    {
        // MAGIC CHECK: Am I the human sitting at this specific keyboard?
        // If not, STOP reading the input! Let the network sync my position instead.
        if (!IsOwner) return;

        // I own this object, so I can control it!
        float h = Input.GetAxis("Horizontal");
        transform.Translate(new Vector3(h, 0, 0) * Time.deltaTime);
    }
}

11. Practice Exercises

  1. 1. What is the fundamental difference between a ServerRpc and a ClientRpc?
  1. 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 address 127.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 utilized NetworkVariables 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.

16. Next Chapter Recommendation

Writing code that works is one thing; writing code that a team of 10 people can read and scale is another. Proceed to Chapter 16: Clean Code and Game Architecture.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·