This document is about: QUANTUM 2
SWITCH TO

Quantum 105 - Multiplayer

Overview

At this point the game is playable in single player mode by entering play mode in the Game scene. Turning gameplay like this into a high quality multiplayer game is usually quite the challenge. But as you can see this is already the last section of this tutorial. So how will we go about adding multiplayer?

The answer is that multiplayer support has already been there all along! With Quantum you simply focus on writing gameplay code and the Quantum engine turns it into a multiplayer game! All that is left is to hook up a menu scene quickly that allows players to join the game online.

Open the Menu scene in the Photon/QuantumDemo/Menu/ folder. The menu scene is a sample scene that comes with all functionality needed to run a Quantum game online including a lobby system.

the menu scene
The Menu scene.

Custom Callbacks

Open the CustomCallbacks script on the QuantumCallbacks GameObject in the scene. This script injects player data into a Quantum session similar to the debug runner but for online sessions.

Adjust the code to the following to inject the custom player data:

C#

using Quantum;
using UnityEngine;

public class CustomCallbacks : QuantumCallbacks {

  public RuntimePlayer PlayerData;
  
  public override void OnGameStart(Quantum.QuantumGame game) {
    // IsPaused is true when a player late joins or reconnects to a room.
    // This prevents the spawning of another player object when reconnecting.
    if (game.Session.IsPaused) return;

    foreach (var localPlayer in game.GetLocalPlayers()) {
      Debug.Log("CustomCallbacks - sending player: " + localPlayer);
      game.SendPlayerData(localPlayer, PlayerData);
    }
  }

  public override void OnGameResync(Quantum.QuantumGame game)
  {
    // OnGameResync is called when a player reconnects.
    Debug.Log("Detected Resync. Verified tick: " + game.Frames.Verified.Number);
  }
}

Return to the unity editor and drop the character prototype into the CharacterPrototype field of the CustomCallbacks component.

Online Play

Next we will build a player to test online play.

First go to Edit > ProjectSettings > Player > Resolution and Presentation and change Fullscreen Mode to Windowed. Then go to File > Build Settings and create a build.

Start the built application and enter play mode in the editor to start 2 instances of the game. In the name field in-game give each client a unique name then press Play Online. You see both clients appear in the same lobby. Press Start Game on the master client to start the game. 2 player characters have been spawned, and each instance is in control of their player character.

two clients playing the game

Note: Even though the clients are connected to a region far away (from Germany to USA East) the gameplay still feels perfectly responsive and there is no indication of lag visible on the clients. This is made possible through the deterministic rollback network architecture of Quantum.

Back to top