- Home /
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.
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!
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
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
Follow this Question
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