- Home /
Racing Game Leaderboard with Photon
Hallo I am trying to programm an online multiplayer racing game this is the code for my leaderboard. The general problem seems to be that the OnPhotonSerializeView changes the value temporary. A debug.log statement insede the method returns the correct values but in the update loop the same index of the array seems to have no value or 0. I also triedd to sync it with RPC calls but the problem is that if you pass in the trackvalue as a parameter every client is using his own trackvalue and not the one which was passsed in by an other client. I used a RPC method like this RPC("synctrackvalue", PhotonTargets.AllViaServer, myActornumber, mytrackvalue). The Method just sets the value myTrackvalue on the index myActornumber-1 in the array idOnTrackvalue.
So does someone know a solution for my problem? Or an other way to solve this? I am appreciating your help and I am extremely sorry for my bad englisch.
private double time = 0;
private float trackvalue = 0;
private Transform LastCheckpoint;
public Text Position1;
public PhotonView photonView;
private string[] idOnNickame = new string[4];
private float[] idOnTrackvalue = new float[4];
private void Awake()
{
PhotonNetwork.LocalPlayer.NickName = PhotonNetwork.LocalPlayer.ActorNumber.ToString() + "Player";
string myNickname = PhotonNetwork.LocalPlayer.NickName;
int myID = PhotonNetwork.LocalPlayer.ActorNumber;
photonView.RPC("SyncNickname", RpcTarget.All, myID, myNickname);
}
private void Update()
{
CurrentLapTime = LapTimerTimestamp > 0 ? Time.time - LapTimerTimestamp : 0;
//Set curent laptime
UILapTimer.text = $"Laptime: {(int)CurrentLapTime / 60}:{(CurrentLapTime) % 60:00.000}";
if (Time.time > time + 1)
{
if (LastCheckpoint != null)
{
//value increses with the players progress
//indikator for position
trackvalue = CurrentLap * 4000 + lastCheckpointPassed * 400 + (transform.position -
LastCheckpoint.position).magnitude;
//OnPhotonSerializeView should just put all other players values into the array so we need
//to add ours ourself
int myID = PhotonNetwork.LocalPlayer.ActorNumber;
idOnTrackvalue[myID - 1] = trackvalue;
//new array to sort the trackvalues
float[] compareTrackvalues = idOnTrackvalue;
Array.Sort(compareTrackvalues);
//reserve so the greatest value is the first in the array
Array.Reverse(compareTrackvalues);
StringBuilder leaderboard = new StringBuilder();
for (int i = 0; i < compareTrackvalues.Length; i++)
{
//idOnNickname connects the online actor number (actor number-1 = index) with his
//Nickname (value in the array)
//idOntrackvalue connects the online actor number (actor number-1 = index) with his
//trackvalue (value in the array)
//compareTrackavlues at this point should list all trackvalues sorted by progress so the
//compareTrackvalues[0] is the
//of the raceleader
//then we get the value and search which index it got in the idOnTrackvalue
//with the index we got the actor number and with the actor number the index of the
//string array with the player names
leaderboard.Append(idOnNickame[Array.IndexOf(idOnTrackvalue,compareTrackvalues[i])] + "\n");
}
Position1.text = leaderboard.ToString();
}
time = Time.time;
}
}
public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if(stream.IsWriting)
{
stream.SendNext(PhotonNetwork.LocalPlayer.ActorNumber);
stream.SendNext(trackvalue);
}
else if (stream.IsReading)
{
idOnTrackvalue[(int)stream.ReceiveNext()-1] = (float)stream.ReceiveNext();
Debug.Log(idOnTrackvalue[0]);
//A Debug.Log(idonTrackvalue[whatever actor number is in here]) statement would show the
//right value of the other online players. So at this point it connects the
//actornumber of the player (actornumber of the online player -1 = index in the
//arrayidOnTrackvalue)
//and the value of the index is the onlineplayers trackvalue:
}
}
[PunRPC]
private void SyncNickname(int playerID, string playerNickname)
{
//this part works perfectly fine
idOnNickame[playerID - 1] = playerNickname;
}
Your answer
Follow this Question
Related Questions
How do i change a sprite when another gameobject with the same prefab is colliding / is near 1 Answer
How do i change a sprite when another gameobject with the same prefab is colliding / is near 1 Answer
Set default length for an array of elements of a custom class in inspector 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers