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
0
Question by divisionstreet0 · Aug 08, 2013 at 08:57 PM · arraydynamicorderturn-basedsort

Dynamic Turn Order and Display Based On Initiative Value (Javascript)?

I have searched and searched for an answer to this but I'm coming up short.

In my game, multiple players create a token that represents themself, they are assigned names and Initiative values at runtime by the player and those values are stored in playerprefs.

I'm trying to sort these tokens by descending Initiative values to determine turn order and to display player names in order in a GUI list. It seems like any array that can store a String and an Int as a Key/Value pair (like a hash table) can't be sorted.

I'm probably missing something really obvious here, but all i need to do is make an array of String(name) and Int(Initiative) pairs, sort the array by the Int(Initiative) value and return the Strings(names) in their new, highest to lowest Initiative order.

so i want to turn some sort of array like this:

token1 with an initiative of 4, token2 with an initiative of 7, token3 with an initiative of 1, token4 with an initiative of 10

into this:

token4 , token2, token1, token3

Also, when a new token is introduced, I need it to slot itself into the correct spot in the initiative order. This would act quite a lot like a scoreboard, where, when a tokens initiative move up the list, the others move down to accommodate it's new position.

Multidimensional array? Hash table? Some sort of custom sorting? Any help with be much appreciated.

EDIT: Does anyone know how to pull this off using Javascript?

Thanks so much.

Comment
Add comment
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

2 Replies

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

Answer by divisionstreet0 · Aug 14, 2013 at 10:47 PM

I finally got this to work. For anyone that's interested:

After failing miserably to understand LINQ queries in Unityscript, I found an example of using bubble sorting to sort one array and apply that sort to another ray.

As the tokens were instantiated, I added their name to an array called "names", their initiatives to an array named "inits" and then ran this function to sort the names and initiatives based of the initiative values.

 //SORTING LOWEST TO HIGHEST
 function bubbleSort(){
             var foundone : boolean;
             var tempobj : String;
             var tempfloat : int;
 
                     foundone = true;
                     while(foundone)
                     {
                       foundone = false;
                       for(n = 0; n < inits.length - 1; n++)
                       {
                         if(inits[n] > inits[n + 1])
                         {
                           tempobj = names[n + 1];
                           tempfloat = inits[n + 1];
                           names[n + 1] = names[n];
                           inits[n + 1] = inits[n];
                           names[n] = tempobj;
                           inits[n] = tempfloat;
                           foundone = true;
                         }
                       }
                     }    
                   }


The issue is that this sorts them into ascending order, but to list them highest to lowest, my OnGUI uses a for loop to iterate through the array backwards. Sloppy, but it's working. Thanks for the input Daniel!

Comment
Add comment · 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
1

Answer by DanielRogersAK · Aug 08, 2013 at 09:24 PM

I would look at using a LINQ query. http://msdn.microsoft.com/en-us/library/vstudio/bb397926.aspx

//The tokens variable should be a IQueryable collection like List //This will produce a list of player name order by the initiative value

var sortedPlayers = (from t in tokens orderby t.Initiative select t.Name).ToList()

//This would be how you would retrieve a token back from the player name

Token myPlayerToken = (from t in tokens where t.Name == selectedPlayerName select t).FirstOrDefault()

Hope this helps or at least points you in a good direction, Daniel

Comment
Add comment · Show 1 · 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 divisionstreet0 · Aug 08, 2013 at 10:04 PM 0
Share

Thanks for your response!

While I'm sure this does exactly what you said it does, I forgot to mention I'm working in UnityScript. The syntax conversion from C# combined with not being familiar with LINQ queries is making my head explode. Is this type of LINQ query possible in JavaScript?

From what I understand of the documentation and your example, I'd make a class called "t" and store the name and initiative in that class. I'd then make a Generic List of the "t" classed objects called "Token" , then use a LINQ query to do the sorting and returning with the code you provided, somehow converted into JavaScript..... Is that right?

Thanks in advance for the help $$anonymous$$.

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

15 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

Related Questions

How can I order an array of RaycastHits in reverse order of distance? 1 Answer

List.Sort with IComparer 2 Answers

How to sort a built-in array in the inspector? 2 Answers

Order a GameObject Array 1 Answer

Storing a variable and it's name 3 Answers


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