Room Timer / Round Updater

Options

Heyyyy everyone!


I'm trying to implement a room timer into the game...i've attached a simplified version of my code below...

The code works for the masterclient, but all the other clients time and rounds arent synced...

Any help would be appreciated.

Thankyou.


using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using Photon.Pun;

using UnityEngine.SceneManagement;

using UnityEngine.UI;


public class InGameManager : MonoBehaviourPunCallbacks

{


  public Text roomTimerText;

  public Text roundText;

  public int currentRound;

  public int MultiplayercurrentRound;

  public int totalRounds;

  public int Timer;

  public int MultiplayerTimer;

  public int RoundTime;

  public int hostSelectionTime;

  public bool flick;

  public bool flick2;

  public bool startedRound;

  public bool startedsync;

  public PhotonView PV;

  public GameObject pm;

  // Start is called before the first frame update

  void Awake()

  {

    PV = GetComponent<PhotonView>();

  }




  void OnSceneLoaded(Scene scene, LoadSceneMode loadSceneMode)

  {

    if (PhotonNetwork.IsMasterClient)

    {

      Timer = RoundTime;

      StartRound();

    }

  }



  // Update is called once per frame

  void Update()

  {


    if(startedRound && Timer == 0 && currentRound == totalRounds)

    {

      Application.LoadLevel("Menu");

    }

    float minutes = Mathf.FloorToInt(Timer / 60);

    float seconds = Mathf.FloorToInt(Timer % 60);

    roundText.text = currentRound.ToString() + " / " + totalRounds.ToString();

    roomTimerText.text = string.Format("{0:00}:{1:00}", minutes, seconds);


    if (PhotonNetwork.IsMasterClient)

    {

      startedsync = startedRound;

      MultiplayerTimer = Timer;

      MultiplayercurrentRound = currentRound;

      PV.RPC("SyncTime", RpcTarget.All, MultiplayerTimer);

      PV.RPC("SyncRound", RpcTarget.All, MultiplayercurrentRound);

      PV.RPC("startRoundbool", RpcTarget.All, startedsync);

      if (!flick && startedRound)

      {

        StartCoroutine(time());

      }


      if (!flick2 && !startedRound)

      {

        StartCoroutine(roundstartdelay());

      }


      if (!startedRound && Timer == 0)

      {

        startedRound = true;

        Timer = RoundTime;

      }

      if(startedRound && Timer == 0)

      {

        startedRound = false;

        StartRound();

        currentRound += 1;

      }

    }


  }

  [PunRPC]

  void SyncTime (int time)

  {

    Timer = time;

  }

  [PunRPC]

  void SyncRound(int round)

  {

    currentRound = round;

  }

  [PunRPC]

  void startRoundbool(bool sr)

  {

    startedRound = sr;

  }



  void StartRound()

  {

    Timer = hostSelectionTime;


  }


  IEnumerator roundstartdelay ()

  {

    flick2 = true;

    yield return new WaitForSeconds(1);

    Timer -= 1;

    flick2 = false;

  }


  IEnumerator time ()

  {

    flick = true;

    yield return new WaitForSeconds(1);

    Timer -= 1;

    flick = false;

  }

}


I appreciate you for looking at my code, thankyou for the assistance.

Best Answer

  • PQ7
    PQ7
    Answer ✓
    Options

    I got it working by swapping to customroomproperties.

Answers

  • PQ7
    PQ7
    Answer ✓
    Options

    I got it working by swapping to customroomproperties.

  • Tobias
    Options

    Glad you found it.

    There is also a Timer prototyping script in the PUN 2 package. That might be worth a look, too.