- Home /
Listing players by score
Hello everyone. I am working on a networked game and wanted to improve my current score board system. Basically, since all my players are listed in classes, I wanted to do the follow:
foreach(Player pl in NetworkManager.Instance.PlayerList)
{
if(pl.Score < "other pl".Score)
{
pl.row = "other pl".row ++;
Debug.Log("Switched score around");
}
}
Is it possible to compare different "pl" with each other? Or do I need to call back on some other solution. Thank you in advance!
Answer by fendorio · Apr 05, 2014 at 04:56 PM
Use linq?
foreach(var item in NetworkManager.Instance.Playerlist.OrderBy(x => x.Score))
{
//Now your list is ordered by score...
}
Or more appropriately for listing by scores where highest is top, use OrderByDescending
foreach(var item in NetworkManager.Instance.Playerlist
.OrderByDescending(x => x.score))
{
//Now they're ordered highest-lowest (Descending order).
}
You'll need to use the Linq library.
using System.Linq;
That is super awesome! Could you just tell me what "x" is? Something specific?
X is just a variable name. You could use x, y, hello_this_is_a_variable, whatever you like :)
Please consider up-voting and accepting if you implemented it in your solution :)
I am going to...I just have to think about how I am going to implement this. See I have this code (this actually includes your solution with link):
if(ShowBoard && Network$$anonymous$$anager.Instance.$$anonymous$$atchStarted == true)
{
GUILayout.BeginArea(new Rect(Screen.width/4, Screen.height/4, (Screen.width) - (Screen.width/2), (Screen.height) - (Screen.height/2)), GUIContent.none, "box");
GUILayout.EndArea();
GUI.Label(new Rect(Screen.width * (2.6f/10f),Screen.height * (1.7f/6.3f), Screen.width * (0.3f/2.3f), Screen.height * (0.3f/6.3f)), "Player Name");
GUI.Label(new Rect(Screen.width * (4.0f/10f),Screen.height * (1.7f/6.3f), Screen.width * (0.3f/2.3f), Screen.height * (0.3f/6.3f)), "Score");
GUI.Label(new Rect(Screen.width * (5.0f/10f),Screen.height * (1.7f/6.3f), Screen.width * (0.3f/2.3f), Screen.height * (0.3f/6.3f)), "$$anonymous$$ills");
GUI.Label(new Rect(Screen.width * (6.0f/10f),Screen.height * (1.7f/6.3f), Screen.width * (0.3f/2.3f), Screen.height * (0.3f/6.3f)), "Assists");
GUI.Label(new Rect(Screen.width * (7.0f/10f),Screen.height * (1.7f/6.3f), Screen.width * (0.3f/2.3f), Screen.height * (0.3f/6.3f)), "Deaths");
foreach(Player pl in Network$$anonymous$$anager.Instance.PlayerList.OrderByDescending(x => x.score))
{
GUI.Label(new Rect(Screen.width * (2.6f/10f),Screen.height * (1.7f/6.3f) + Row * rowHeight, Screen.width * (0.3f/2.3f), Screen.height * (0.3f/6.3f)), pl.PlayerName);
GUI.Label(new Rect(Screen.width * (4.0f/10f),Screen.height * (1.7f/6.3f) + Row * rowHeight, Screen.width * (0.3f/2.3f), Screen.height * (0.3f/6.3f)), pl.Score.ToString());
GUI.Label(new Rect(Screen.width * (5.0f/10f),Screen.height * (1.7f/6.3f) + Row * rowHeight, Screen.width * (0.3f/2.3f), Screen.height * (0.3f/6.3f)), pl.$$anonymous$$ills.ToString());
GUI.Label(new Rect(Screen.width * (6.0f/10f),Screen.height * (1.7f/6.3f) + Row * rowHeight, Screen.width * (0.3f/2.3f), Screen.height * (0.3f/6.3f)), pl.Assists.ToString());
GUI.Label(new Rect(Screen.width * (7.0f/10f),Screen.height * (1.7f/6.3f) + Row * rowHeight, Screen.width * (0.3f/2.3f), Screen.height * (0.3f/6.3f)), pl.Deaths.ToString());
Row ++;
}
}
How would I make it so that the whole thing is sorted properly via GUI.
Sorted properly via GUI? Would need more specifics, and that technically should be a new question :P