Multiple call for callbacks IPunTurnManagerCallbacks

I try to use the IPunTurnManagerCallbacks but it seem's that the calls functions like OnTurnTimeEnds, OnTurnBegins are called more than one time and it disturbs me while making my game. Here is the code of my TurnManager.cs

using UnityEngine;
using Photon.Pun;
using Photon.Pun.UtilityScripts;
using Photon.Realtime;
using System;
using ExitGames.Client.Photon;

public class TurnManager : MonoBehaviour, IPunTurnManagerCallbacks
{
  private static TurnManager Instance;
  private PunTurnManager turnManager;

  private int itterator = 0;

  private string currentTeam;

  private void Awake()
  {
    Instance = this;
  }

  public static TurnManager GetInstance()
  {
    return Instance;
  }
  public void InitiateTurnManager(string startTeam )
  {
    if (PhotonNetwork.LocalPlayer.IsMasterClient)
    {
      currentTeam = startTeam;
      turnManager = gameObject.AddComponent<PunTurnManager>();
      turnManager.TurnManagerListener = this;
      turnManager.TurnDuration = 10;
      turnManager.BeginTurn();
      //Timer.GetInstance().StartCountdown(10);
    }  
  }
  public void OnPlayerFinished(Player player, int turn, object move)
  {
    throw new NotImplementedException();
  }

  public void OnPlayerMove(Player player, int turn, object move)
  {
    if (PhotonNetwork.LocalPlayer.IsMasterClient)
    {   
    }
  }

  public void OnTurnBegins(int turn)
  {
    print("ITT " + itterator);
    itterator++;
    if (PhotonNetwork.LocalPlayer.IsMasterClient)
    {
      Hashtable roomProperties = new();
      roomProperties.Add("teamPlaying", currentTeam);
      roomProperties.Add("playerPlaying", "Master");
      PhotonNetwork.CurrentRoom.SetCustomProperties(roomProperties);
    }
  }

  public void OnTurnCompleted(int turn)
  {
    if (PhotonNetwork.LocalPlayer.IsMasterClient)
    {   
    }
  }

  public void OnTurnTimeEnds(int turn)
  {
    itterator++;
    if (PhotonNetwork.LocalPlayer.IsMasterClient)
    {
      DefineTeamAndPlayerNextRound();
      turnManager.BeginTurn();
    }
  }

  private void DefineTeamAndPlayerNextRound()
  {
    Hashtable roomProperties = new();
    switch ((string)PhotonNetwork.CurrentRoom.CustomProperties["teamPlaying"])
    {
      case "Red" when (string)PhotonNetwork.CurrentRoom.CustomProperties["playerPlaying"] == "Master":
        roomProperties.Add("teamPlaying", "Red");
        roomProperties.Add("playerPlaying", "Spy");
        
        break;
      case "Red" when (string)PhotonNetwork.CurrentRoom.CustomProperties["playerPlaying"] == "Spy":
        roomProperties.Add("teamPlaying", "Blue");
        roomProperties.Add("playerPlaying", "Master");
        break;
      case "Blue" when (string)PhotonNetwork.CurrentRoom.CustomProperties["playerPlaying"] == "Master":
        roomProperties.Add("teamPlaying", "Blue");
        roomProperties.Add("playerPlaying", "Spy");
        break;
      case "Blue" when (string)PhotonNetwork.CurrentRoom.CustomProperties["playerPlaying"] == "Spy":
        roomProperties.Add("teamPlaying", "Red");
        roomProperties.Add("playerPlaying", "Master");
        break;
    }
     
    PhotonNetwork.CurrentRoom.CustomProperties["teamPlaying"] = roomProperties["teamPlaying"];
    PhotonNetwork.CurrentRoom.CustomProperties["playerPlaying"] = roomProperties["playerPlaying"];
    print("Properties " + roomProperties);
    GameObject.Find("CardDatabase").transform.gameObject.GetPhotonView().RPC(
        "SetMessageOnRealTime",
        RpcTarget.All,
        "C'est au tour des " + LaunchGame.TranslateTeamFR((string) roomProperties["teamPlaying"]) + " " + LaunchGame.TranslanteRoleFR((string) roomProperties["playerPlaying"]) + " de jouer."
        );
  }
}

Any help please ? :)

Answers

  • Tobias
    Tobias admin
    edited April 2023

    Make sure to register for the callbacks only once and that there is maybe just one script registering.

    Is that something you checked?

    This will only set the key-value locally: PhotonNetwork.CurrentRoom.CustomProperties["teamPlaying"]. Use SetCustomProperties() instead! Then you also don't need to set properties and call an RPC. Don't do that either.