[Solution] Correct ping time for all players

First of all - adding following code to PhotonNetwork.cs:
	private static int _largestPing = 0;
	public static int largestPing {
		get { return PhotonNetwork._largestPing; }
		set { PhotonNetwork._largestPing = value; }
	}

        /* This one getting to your player instance ping time to wait others */
	public static float GetAvgPing() {
		
		return (PhotonNetwork.largestPing - PhotonNetwork.player.ping) / 1000.0f;
		
	}
	


PhotonPlayer.cs:
	private int pingTime = 0;
	public int ping
    {
        get { return this.pingTime; }
		set { this.pingTime = value; }
    }

NetworkingPeer.cs (Look for executeRPC method. Place it after "object[] inMethodParameters = (object[])rpcData[(byte)4];"):
sender.ping = (int)rpcData[(byte)5];
		
		int largestPing = 0;
		for (int i = 0; i < PhotonNetwork.playerList.Length; i++) {
			
			if (largestPing < PhotonNetwork.playerList[i].ping) {
				largestPing = PhotonNetwork.playerList[i].ping;
			}
			
		}
		PhotonNetwork.largestPing = largestPing;

NetworkingPeer.cs (Look for RPC function with PhotonTargets):
1. Replace def:
RPC(PhotonView view, string methodName, PhotonTargets target, int ping, params object[] parameters)

2. Add after "rpcEvent[(byte)4] = (object[])parameters;":
rpcEvent[(byte)5] = ping;
		
		PhotonNetwork.player.ping = ping;

And the last!
PhotonNetwork.cs:
Looking for all RPC methods. Replace lines looks like
networkingPeer.RPC(view, methodName, target, parameters);
to
networkingPeer.RPC(view, methodName, target, PhotonNetwork.GetPing(), parameters);

Thats all!
After this you can use RPC somthing like this: (JS)
@RPC
public function rpcMove(param : int) {


		if (this.photonView.isMine) {
			yield WaitForSeconds(PhotonNetwork.largestPing / 1000);
		}
yield WaitForSeconds(PhotonNetwork.GetAvgPing());
		
// Do sync actions for all players

}

public function Start() {

/* You must use All if you want get real pings for all players */
this.photonView("rpcMove", PhotonTargets.All, 123);

}

Comments

  • Thanks. Maybe you want to edit your post and explain first what it does and why that's a good thing.