0
votes

I am trying to create a simple 2D Turn based multiplayer game using Photon Unity Networking.

It is just a simple turn based game where a player 1 (host) presses his button and it adds his score and changes its turn to player 2 (client) who presses his button to add score and change turn to player 1. It continues to infinite.

I was able to connect two players in the game using the basic Photon documentation. Now I need to add the networking logic of taking turns and changing them.

I searched the internet but I can't understand the RPC and SerializeView of Photon. I am really confused with that. Please Help me. Thank you in future. Here is my GameManager Script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class GameManager : Photon.PunBehaviour
{    
    public Text roomName;
    public Text player1Name,player2Name;
    public List<string> playersConnected = new List<string>();
    int scoreP1 = 0, scoreP2 = 0;
    public Text scoreTextP1, scoreTextP2;
    public Button p1Btn, p2Btn;
    int playerTurn = 1;

    void Start()
    {
        roomName.text = SceneManager.GetActiveScene().name;
        p2Btn.gameObject.SetActive(false);
        p1Btn.gameObject.SetActive(true);
    }    

    public void AddScoreP1()
    {
        scoreP1++;
        scoreTextP1.text = scoreP1.ToString();
        ChangePlayerTurn();
    }


    public void AddScoreP2()
    {
        scoreP2++;
        scoreTextP2.text = scoreP2.ToString();
        ChangePlayerTurn();
    }

    void ChangePlayerTurn()
    {
        if (playerTurn == 1)
        {
            playerTurn = 2;
            p2Btn.gameObject.SetActive(true);
            p1Btn.gameObject.SetActive(false);
        }
        else
        {
            playerTurn = 1;
            p1Btn.gameObject.SetActive(true);
            p2Btn.gameObject.SetActive(false);
        }
        print("Player Turn: P" + playerTurn);
    }

    void LoadArena()
    {
        if (!PhotonNetwork.isMasterClient)
        {
            Debug.LogError("PhotonNetwork : Trying to Load a level but we are not the master Client");
        }
        Debug.Log("PhotonNetwork : Loading Level : " + PhotonNetwork.room.PlayerCount);
        PhotonNetwork.LoadLevel("Room for " + PhotonNetwork.room.PlayerCount);
    }

    public override void OnLeftRoom()
    {
        SceneManager.LoadScene(0);
    }      

    public void LeaveRoom()
    {
        PhotonNetwork.LeaveRoom();
    }

    public override void OnPhotonPlayerConnected(PhotonPlayer other)
    {
        Debug.Log("OnPhotonPlayerConnected() " + other.NickName); // not seen if you're the player connecting
        foreach (PhotonPlayer _player in PhotonNetwork.playerList)
        {
            playersConnected.Add(other.NickName);
        }

        if (PhotonNetwork.isMasterClient)
        {
            Debug.Log("OnPhotonPlayerConnected isMasterClient " + PhotonNetwork.isMasterClient); // called before OnPhotonPlayerDisconnected

            LoadArena();
        }
    }

    public override void OnPhotonPlayerDisconnected(PhotonPlayer other)
    {
        Debug.Log("OnPhotonPlayerDisconnected() " + other.NickName); // seen when other disconnects
        foreach (PhotonPlayer _player in PhotonNetwork.playerList)
        {
            playersConnected.Remove(other.NickName);
        }

        if (PhotonNetwork.isMasterClient)
        {
            Debug.Log("OnPhotonPlayerDisonnected isMasterClient " + PhotonNetwork.isMasterClient); // called before OnPhotonPlayerDisconnected
            LoadArena();
        }
    }
}
1
I don't know Photon only UNet and unfortunately I don't see where your attempt of invoking methods on the server/client is.. but Rpc is simply a method invoked from the server but executed on (ALL) clients. It is usually used in combination with a Cmd or Command method which does the opposide: it is invoked by a client but only executed on the server. - derHugo
How can we do it using unet? I'm not restricted to using photon. Since I don't know how to use unet I was using photon. Can you help me with unet? Thanks - Saad Tahir

1 Answers

1
votes

RPC is basically a way of invoking a function on a remote client.

Usually in a multiplayer setup you'd want your MasterClient to control the flow of the game. So, when two players join a room, what you need to do is from your MasterClient, decide which player goes first and then call a RPC function from MasterClient telling both client whose the first turn is. Then whichever player's turn it is, just activate its button and let them add score (Send RPC to MasterClient as well for that, so that everyone can stay in sync.) and the update turn and tell everyone via another RPC and so on.

though you can also use Events for such cases, they need less preparation to be used.