- Home /
Organising player Score from highest to lowest?
Hi gang! I managed to sync my players scores in Multiplayer witch is Awesome! I want to add a feature that determines the winner.
(Orders highest Score and Lowest scores) (float) I have each players scores streamed all I am missing is a way top put them order So the results can be displayed.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Photon;
public class ScoreTRacker : Photon.MonoBehaviour {
public float Player1Score;
public float Player2Score;
public float Player3Score;
public float Player4Score;
public float AcualPlayer1Score;
public float AcualPlayer2Score;
public float AcualPlayer3Score;
public float AcualPlayer4Score;
public Text FistPlace;
public Text SecondPlace;
public Text FourthPlace;
public Text FithPlace;
public float FistPlaceScore;
public float SecondPlaceScore;
public float FourthPlaceScore;
public float FithPlaceScore;
public GameConnectClassic GameConnect;
public PlayerGUI PlayerG;
public GameObject GameMananger;
// Use this for initialization
void Start () {
GameMananger = GameObject.FindGameObjectWithTag ("GameMananger");
GameConnect = GameMananger.GetComponent<GameConnectClassic> ();
}
// Update is called once per frame
void Update () {
if(GameConnect.Player1 = true){
Player2Score = AcualPlayer2Score;
Player3Score = AcualPlayer3Score;
Player4Score = AcualPlayer4Score;
}
if(GameConnect.Player2 = true){
Player1Score = AcualPlayer1Score;
Player3Score = AcualPlayer3Score;
Player4Score = AcualPlayer4Score;
}
if(GameConnect.Player3 = true){
Player2Score = AcualPlayer2Score;
Player1Score = AcualPlayer1Score;
Player4Score = AcualPlayer4Score;
}
if(GameConnect.Player4 = true){
Player2Score = AcualPlayer2Score;
Player3Score = AcualPlayer3Score;
Player1Score = AcualPlayer1Score;
}
if (PlayerG.GameGoing == false) {
DetermainWinner ();
}
}
void DetermainWinner(){
// get the highest score.....
FistPlace.text = FistPlaceScore;
SecondPlace.text = SecondPlaceScore;
FourthPlace.text = FourthPlaceScore;
FithPlace.text = FithPlaceScore;
}
// SYNC SCORES.......
public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
//we are reicieving data
if (stream.isReading)
{
//receive the next data from the stream and set it to the truLoc varible
if(GameConnect.Player1 = true){
AcualPlayer2Score = (float)stream.ReceiveNext(); //the stream send data types of "object" we must typecast the data into a Vector3 format
AcualPlayer3Score = (float)stream.ReceiveNext();
AcualPlayer4Score = (float)stream.ReceiveNext();
}
if(GameConnect.Player2 = true){
AcualPlayer1Score = (float)stream.ReceiveNext(); //the stream send data types of "object" we must typecast the data into a Vector3 format
AcualPlayer3Score = (float)stream.ReceiveNext();
AcualPlayer4Score = (float)stream.ReceiveNext();
}
if(GameConnect.Player3 = true){
AcualPlayer2Score = (float)stream.ReceiveNext(); //the stream send data types of "object" we must typecast the data into a Vector3 format
AcualPlayer1Score = (float)stream.ReceiveNext();
AcualPlayer4Score = (float)stream.ReceiveNext();
}
if(GameConnect.Player4 = true){
AcualPlayer2Score = (float)stream.ReceiveNext(); //the stream send data types of "object" we must typecast the data into a Vector3 format
AcualPlayer3Score = (float)stream.ReceiveNext();
AcualPlayer1Score = (float)stream.ReceiveNext();
}
}
//we need to send our data
else
{
//send our posistion in the data stream
if(GameConnect.Player1 = true){
stream.SendNext(Player1Score);
}
if(GameConnect.Player2 = true){
stream.SendNext(Player2Score);
}
if(GameConnect.Player3 = true){
stream.SendNext(Player3Score);
}
if(GameConnect.Player4 = true){
stream.SendNext(Player4Score);
}
}
}
}
}
Answer by TBART82 · Oct 12, 2017 at 07:01 AM
Hello there! @zak666 I have come up with a great way to sort through the scores to get a list of scores from the highest number to the smallest number. Here it is:
// Create a list to hold all four scores
public List<int> scores;
void DetermineWinner()
{
// Add each players score to the 'score' list.
scores.Add(Player1Score);
scores.Add(Player2Score);
scores.Add(Player3Score);
scores.Add(Player4Score);
// Use the sorting function to sort out numbers
// from smallest to largest
scores.Sort();
// Create a temp list called 'finalList'
List<int> finalScores = new List<int>();
// Iterate through the scores backwards to get
// scores from greates to smallest number.
for (int i = scores.Count; i --> 0;)
{
finalScores.Add(scores[i]);
}
// Set the main scores to the greatest to smallest score.
scores = finalScores;
}
If there is anything which you need me to clarify, be sure to let me know!
TBART82
You can leave the for loop and the finalScores list out by using the List.Reverse() Function. With your solution you will not know what player had what score. I know that OP didn't mention that behaviour but I think he will need it.
Thanks for the corrections. I will update my answer soon.
Thanks a lot $$anonymous$$8, Yeah I figured how to Display the Scores In order of player once I had the scores In a order Why didn't i think of lists before.... XD
void DetermainWinner() {
// Add each players score to the 'score' list.
scores.Add(Player1Score);
scores.Add(Player2Score);
scores.Add(Player3Score);
scores.Add(Player4Score);
// Use the sorting function to sort out numbers
// from smallest to largest
scores.Sort();
FistPlaceScore = scores.IndexOf(1);
SecondPlaceScore = scores.IndexOf(2);
ThirdPlaceScore = scores.IndexOf(3);
FourthPlaceScore = scores.IndexOf(4);
// get the highest score.....
FistPlace.text = FistPlaceScore.ToString();
SecondPlace.text = SecondPlaceScore.ToString();
ThirdPlace.text = ThirdPlaceScore.ToString();
FourthPlace.text = FourthPlaceScore.ToString();
if(PlayerG.LocalScore == FistPlaceScore){ // I A$$anonymous$$ THE WINNER........
RewardsFirstPlaceGO.SetActive(true);
}
if(PlayerG.LocalScore == SecondPlaceScore){ // I A$$anonymous$$ THE Second........
RewardsFirstPlaceGO.SetActive(true);
}
if(PlayerG.LocalScore == ThirdPlaceScore){ // I A$$anonymous$$ THE Third........
RewardsFirstPlaceGO.SetActive(true);
}
if(PlayerG.LocalScore == FourthPlaceScore) { // I A$$anonymous$$ THE forth........
RewardsFirstPlaceGO.SetActive(true);
}
}
scores.IndexOf(1) will return you the index of the element with the value 1. Don't know if you really want that. you can just access the highest value in the list by:
scores[0]
And add a scores.Reverse() after scores.Sort() to get the right order
Answer by Der-Die-Das · Oct 12, 2017 at 06:29 AM
So you just want to know which Player won? And you have all the scores ready?
Just compare the Scores? Either with if statements or with Mathf.Max()
And to say something about your code.. It's very redundant. Create a Player class and add the scores etc in it. Afterwards you can loop through a array of the players and you would have way less code than that. Write me again if you would like to get more information about that. ^^
Also.. What Photon Tutorial did you use? I really would like to learn networking :3
Spent ages figuring out how to use it on the documents. :) After I publish my game ($5 per month to play) I'm going to make a tutorial series.
Do you have a YouTube Channel where you will publish it? And how will it take you approx to make the tutorial? Would love to learn from you :pp
Your answer
Follow this Question
Related Questions
Getting playerID from the Google Play Leaderboard? 0 Answers
Score Question Java Script, problem 1 Answer
high score script? 0 Answers
live system for sword 1 Answer