Search Unity

Multiplayer Round Countdown Issues

Discussion in 'Scripting' started by NullSignal, Jul 2, 2015.

  1. NullSignal

    NullSignal

    Joined:
    Sep 18, 2013
    Posts:
    45
    I'm building a round-based multiplayer game using Photon Unity Networking and I'm having issues with my countdown timer. I'm currently trying to use an RPC to start the timer on all clients, but something odd happens. When I start the timer, it runs on the master client first, then when it's completed the countdown on that client, THEN it executes on remote clients. Here's my code:


    Code (CSharp):
    1. public void roundCountdown()
    2.      {
    3.          GetComponent<PhotonView>().RPC("roundCountdown_RPC", PhotonTargets.All);
    4.      }
    5.      [RPC]
    6.      void roundCountdown_RPC()
    7.      {
    8.          roundStartScreen = GameObject.Find("RoundStartScreen(Clone)");
    9.          roundStartScreen.transform.GetChild(0).GetComponent<Text>().text = "Round Starting In";
    10.        
    11.          InvokeRepeating("decreaseTimeRemaining", 1.0f, 1.0f);
    12.          if (roundCountdownTimer == 0)
    13.          {
    14.              Debug.Log("roundCountdownTimer = " + roundCountdownTimer);
    15.              startPlayer();
    16.              HUDCanvasGO.transform.FindChild("RoundTimer").GetComponent<RoundTimer>().hasStarted = true;
    17.          }
    18.      }
    I'm calling roundCountdown() through a button press for testing purposes. I've tried changing the PhotonTargets to Master and AllBuffered with the same result. I truly don't understand why this causes the countdown to finish on the master before executing everywhere else. Can someone explain the logic here? Am I just completely doing it wrong or what?
     
  2. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,066
    The client where you press the button will start the timer immediately. The RPC is called locally, as PhotonTargets.All allows that. It's a trick to save bandwidth but it does mess with the execution order (due to lag for the others).
    The others should definitely not wait until the local player is done with the countdown. Did you try a longer time?

    You could take a look at the InRoomRoundTimer component from the PUN package. It uses a Custom Property to sync the start time for the room. This works with players already in the room as well as joining players.