- Home /
RPC Call for Round Countdown Executes Late on Remote Clients
I'm using an RPC call to initiate the pre-round countdown for my multiplayer game (PUN), but the countdown doesn't begin on the remote clients until it's finished on the master client.
public void roundCountdown()
{
GetComponent<PhotonView>().RPC("roundCountdown_RPC", PhotonTargets.All);
}
[RPC]
void roundCountdown_RPC()
{
roundStartScreen = GameObject.Find("RoundStartScreen(Clone)");
roundStartScreen.transform.GetChild(0).GetComponent<Text>().text = "Round Starting In";
InvokeRepeating("decreaseTimeRemaining", 1.0f, 1.0f);
if (roundCountdownTimer == 0)
{
Debug.Log("roundCountdownTimer = " + roundCountdownTimer);
startPlayer();
HUDCanvasGO.transform.FindChild("RoundTimer").GetComponent<RoundTimer>().hasStarted = true;
}
}
Could someone explain the reason that the countdown executes on the master client (the only client that can start the round without a full room) and THEN the other clients? I thought PhotonTargets.All meant that the target function would be run on all clients at approximately the same time.
Answer by tobiass · Jul 10, 2015 at 09:20 AM
PhotonTargets.All uses a "shortcut" and executes the RPC instantly on the local client. The others get it a bit later, when the message got through the network.
You can use PhotonTargets.AllViaServer to send the RPC back to the client which called it.
The script InRoomRoundTimer.cs could be interesting to check out, too. It sets a property when the game starts. Based on this, rounds start over and over again, but you can also just use it to initially start your game.
AllViaServer sounds like what I need, but nothing seems to happen when I use it. Are there any criteria for what kind of functions can be called via RPC? I'm trying to just call a function that calls StartCoroutine() to start the timer function. Is there another technique I should be using if I want to use AllViaServer?
I do get this error when I try to use AllViaServer, though
TargetParameterCountException: parameters do not match signature
System.Reflection.$$anonymous$$ono$$anonymous$$ethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Reflection/$$anonymous$$ono$$anonymous$$ethod.cs:188)
System.Reflection.$$anonymous$$ethodBase.Invoke (System.Object obj, System.Object[] parameters) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Reflection/$$anonymous$$ethodBase.cs:115)
PhotonView.ExecuteComponentOnSerialize (UnityEngine.Component component, .PhotonStream stream, .Photon$$anonymous$$essageInfo info) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonView.cs:530)
PhotonView.SerializeComponent (UnityEngine.Component component, .PhotonStream stream, .Photon$$anonymous$$essageInfo info) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonView.cs:440)
PhotonView.SerializeView (.PhotonStream stream, .Photon$$anonymous$$essageInfo info) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonView.cs:337)
NetworkingPeer.OnSerializeWrite (.PhotonView view) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:3286)
NetworkingPeer.RunViewUpdate () (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:3207)
PhotonHandler.Update () (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonHandler.cs:90)
Here's the new code I'm working with:
public void roundCountdown()
{
GetComponent<PhotonView>().RPC("roundCountdown_RPC", PhotonTargets.AllViaServer);
Debug.Log("Running round countdown");
}
[RPC]
void roundCountdown_RPC()
{
Debug.Log("Running round countdown RPC");
roundStartScreen = GameObject.Find("RoundStartScreen(Clone)");
roundStartScreen.transform.GetChild(0).GetComponent<Text>().text = "Round Starting In";
//InvokeRepeating("decreaseTimeRemaining", 1.0f, 1.0f);
StartCoroutine(countdownEnum());
// Debug.Log("Time Remaining: " + niceTime);
}
IEnumerator countdownEnum()
{
Debug.Log("countdownEnum");
yield return new WaitForSeconds(1);
roundCountdownTimer--;
roundStartScreen.transform.GetChild(1).GetComponent<Text>().text = roundCountdownTimer.ToString();
if (roundCountdownTimer >= 0) {
StartCoroutine(countdownEnum());
}
}