Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Borzi · Apr 05, 2014 at 04:52 PM · c#classlistsscoreboard

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!

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fendorio · Apr 05, 2014 at 04:54 PM 0
Share

Have you heard of linq?

avatar image Borzi · Apr 05, 2014 at 04:56 PM 0
Share

I have, but am not sure how to use it.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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;
Comment
Add comment · Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Borzi · Apr 05, 2014 at 05:03 PM 0
Share

That is super awesome! Could you just tell me what "x" is? Something specific?

avatar image fendorio · Apr 05, 2014 at 05:05 PM 0
Share

X is just a variable name. You could use x, y, hello_this_is_a_variable, whatever you like :)

avatar image fendorio · Apr 05, 2014 at 05:05 PM 0
Share

Please consider up-voting and accepting if you implemented it in your solution :)

avatar image Borzi · Apr 05, 2014 at 05:10 PM 0
Share

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.

avatar image fendorio · Apr 05, 2014 at 05:12 PM 0
Share

Sorted properly via GUI? Would need more specifics, and that technically should be a new question :P

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to use generic lists in C#? 1 Answer

Using the object.Equals() method for a custom class 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Getting NullReferenceException when creating class instance using List 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges