- Home /
Storing a variable and it's name
I'm attempting to take multiple variables, put them in order of highest to lowest, then extract the highest and display it's name as part of the GUI.
To be a bit more specific:
I have four variables: hunger, pain, tiredness and boredom. Each of these have a percentage rating from 0-100 (0.1, 0.2, 0.3, etc). I put the four variables in an array, sort them and reverse them, so for example if:
hunger = 0.5 pain = 0.0 tiredness = 0.2 boredom = 0.3
then the array is:
array = 0.5, 0.3, 0.2, 0.0
Now that I have my correct ordering I want to take highest value (0.5) and display 'hunger' on the GUI. But the array no longer knows that 0.5 comes from the variable 'hunger'.
How do I go about connecting the value of 0.5 to the name of the variable so I can dispaly the variable name?
Variable names are supposed to be a hint for the programmer. If you copy the value behind a variable in a array it has no relationship to the variable anymore. You need to pack the name and the value in a struct and use an array of structs. Implement a custom compare function and you can sort the array. I can show you an example in C#. I don't do such advanced stuff in such a buggy language as Javascript.
$$anonymous$$aybe you can post your "translated" code :D i don't want to do that again since you already did it ;). $$anonymous$$ake sure that your class derives from IComparer (extends in JS). I found a thread that suggest also IComparable, but haven't used it. http://forum.unity3d.com/threads/44571-Sort-Objects-in-Javascript
Answer by Arch · Mar 27, 2011 at 09:07 AM
Although the answer given by Bunny83 should work, I found a much quicker and simpler way of solving my specific problem.
Unity allows you to take two arrays, then sort the second based upon the values in the first. This is limited in that you must create the arrays in matching pairs. See the example below for a better understanding:
function OnGUI() { var dName = ["Hungry", "Pain", "Bored", "Sleepy", "Tired"]; var dValue = [hungerPerc, painPerc, boredomPerc, sleepinessPerc, tirednessPerc]; System.Array.Sort(dValue, dName);
highestDrive = dName[4];
GUI.Label(Rect(10,100, 200,20), highestDrive);
}
This code creates two arrays. The values stored in them need to be written in order so that the 'sort' function will link them. In my case the creatures interaction with the world will change the numeric values in the dValue's. The GUI then displays the name of the highest value.
Answer by Bunny83 · Feb 01, 2011 at 08:05 AM
Here an example in C#:
using UnityEngine; using System.Collections; using System.Collections.Generic;
public class CHighscoreItem { public string name; public float value; public CHighscoreItem(string aName, float aValue) { name = aName; value = aValue; } }
public class Highscore : MonoBehaviour,IComparer<CHighscoreItem> { public float hunger = 0.5f; public float pain = 0.0f; public float tiredness = 0.2f; public float boredom = 0.3f; List<CHighscoreItem> myList = null;
void Start ()
{
myList = new List<CHighscoreItem>();
}
void Update()
{
myList.Clear();
myList.Add(new CHighscoreItem("hunger",hunger));
myList.Add(new CHighscoreItem("pain",pain));
myList.Add(new CHighscoreItem("tiredness",tiredness));
myList.Add(new CHighscoreItem("boredom",boredom));
myList.Sort(this);
}
void OnGUI()
{
GUILayout.BeginVertical();
foreach (CHighscoreItem I in myList)
{
GUILayout.Label(I.name + " : " + I.value);
}
GUILayout.EndVertical();
}
public int Compare(CHighscoreItem I1, CHighscoreItem I2)
{
return I2.value.CompareTo(I1.value);
}
}
The trick to sort a list of structs/classes is to implement a Compare function for your type. I used the generic interface IComparer<> and implemented that function in my Highscore class. That's why i can sort the list that way:
myList.Sort(this);
Sort() take as argument an Object that implements a compare function, so i gave it "this".
The script works, just save it as Highscore.cs and put it on any gameobject. If you need a JS version, just ask for a JS freak to adapt this one ;)
I've spent most of the night trying to convert the above to JS (sorry, only language I currently know) and I've just about got it working.
I've made an array of classes, with each class containing a string ("hunger") and a float (0.5). Now all that's left is to sort them.
Annoyingly I keep getting this error and I can't find a way around it: "No IComparable or IComparable interface found"
Any chance someone can get me over this last hurdle?
The class that contains the "Compare" function, in my case i used the "Highscore" class, have to implement the IComparer interface for our "Item-class". That's why i derived "Highscore" from $$anonymous$$onoBehavior and IComparer. Not sure to derive correctly in JS but i'll take a look
Answer by Arch · Feb 04, 2011 at 06:18 AM
Thanks for the continued help Bunny. I'm pretty close to getting it. I think I'm only one or two steps short.
Here's the code I've got so far:
//Chemical percentages static var hungerPerc = 0.0; static var painPerc = 0.0; static var tirednessPerc = 0.0; static var sleepinessPerc = 0.0; static var boredomPerc = 0.0; static var driveArray = new Array();
function Start() {
//create seperate drive info classes
var hungerInfo = new driveInfo("Hunger", hungerPerc);
var painInfo = new driveInfo("Pain", painPerc);
var tirednessInfo = new driveInfo("Tired", tirednessPerc);
var sleepinessInfo = new driveInfo("Sleepy", sleepinessPerc);
var boredomInfo = new driveInfo("Bored", boredomPerc);
//place drive info classes into an array
driveArray = [hungerInfo, painInfo, tirednessInfo, sleepinessInfo, boredomInfo];
//sort the array in terms of highest drive
driveArray.Sort();
//System.Array.Sort(driveArray);
driveArray.Reverse();
}
//Create class to contain drive information
class driveInfo implements System.Collections.Generic.IComparer{
function driveInfo(myDriveName:String, myDriveValue:int)
{
this.driveName = myDriveName;
this.driveValue = myDriveValue;
}
var driveName: String;
var driveValue: int;
}
I think part of the problem may be that I'm attempting to 'sort' the array in the start function, and not in the class, but I'm really not sure.