How To Set Up Photon Multiplayer

Hello, I’m completely new to multiplayer, and I’ve designed a chess game that I want to make multiplayer with photon. It’s for a client, they just want it to be at max 2 concurrent players, so it can be just one room.

As I said before, while I have a fair amount of experience with Unity, I don’t know much about multiplayer. I found a tutorial on making a multiplayer game but it was one of those traps that was only a half tutorial, the rest is behind a paywall lol.

So, I have the server set up, it connects to a server and creates a room on start. I’m not sure if this will join an existing room automatically, or do I need to check if there is a room already created, and if so, join it? So here’s my script,

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using TMPro;
using Photon.Realtime;
public class NetworkManager : MonoBehaviourPunCallbacks
{
    static NetworkManager _networkManager;
    TextMeshProUGUI networkText;
    [SerializeField]
    string roomName;
    bool connected, attemptingConnect;
    public static NetworkManager networkManager
    {
        get
        {
            if(_networkManager != null)
            {
                return _networkManager;
            }
            return null;
        }
    }
    private void Awake()
    {
        networkText = GameObject.Find("NetworkText").GetComponent<TextMeshProUGUI>();
        if (_networkManager != null && _networkManager != this)
        {
            gameObject.SetActive(false);
        }
        else
        {
            _networkManager = GetComponent<NetworkManager>();
        }
        DontDestroyOnLoad(gameObject);
    }
    private void Start()
    {
        PhotonNetwork.ConnectUsingSettings();   
    }
    private void Update()
    {
        if(!connected && !attemptingConnect)
        {
            StartCoroutine(TryReconnect());
        }
    }
    public override void OnConnectedToMaster()
    {
        networkText.text = "Connected to server";
        StopAllCoroutines();
        connected = true;
        attemptingConnect = false; 
    }
    public override void OnDisconnected(DisconnectCause cause)
    {
        networkText.text = "Disconnected from server.";
        connected = false;
    }
    IEnumerator TryReconnect()
    {
        attemptingConnect = true;
        PhotonNetwork.ConnectUsingSettings();
        yield return new WaitForSeconds(1);
        networkText.text = "connecting...";
        yield return new WaitForSeconds(10);
        attemptingConnect = false;
    }
    public override void OnCreatedRoom()
    {
        networkText.text = "Room created with name: " + PhotonNetwork.CurrentRoom.Name;
    }
    public void CreateRoom(string roomName)
    {
        PhotonNetwork.CreateRoom(roomName);
    }
    public void JoinRoom(string roomName)
    {
        PhotonNetwork.JoinRoom(roomName);
    }
    public void ChangeScene(int sceneName)
    {
        PhotonNetwork.LoadLevel(sceneName);
    }
}

So will this automatically connect to the existing room if there is two concurrent players? Is there anything else I should know about multiplayer? I have the prefabs loading from the resource folder as I heard you should do this, but is there anything else? I don’t even know how I ought to test it out without having another computer, which I don’t…

Can someone advise a poor networking noob? lol, I have literally 0 experience with networking, multiplayer always scared me.

You should go through Photon’s getting started stuff on their website. It goes into all of this.

1 Like

Ah, I did not know this existed lol, thank you as always.