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 Arch · Feb 01, 2011 at 07:12 AM · arrayvariablenamesort

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?

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 Bunny83 · Feb 01, 2011 at 07:32 AM 0
Share

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.

avatar image Bunny83 · Feb 03, 2011 at 11:17 PM 0
Share

$$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

3 Replies

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

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.

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
0

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&lt;CHighscoreItem&gt;();
 }
 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 ;)

Comment
Add comment · Show 2 · 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 Arch · Feb 02, 2011 at 01:53 PM 0
Share

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?

avatar image Bunny83 · Feb 03, 2011 at 11:09 PM 0
Share

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

avatar image
0

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.

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

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

No one has followed this question yet.

Related Questions

Find Nearest Object from an Array of Transforms 1 Answer

Sorting Variables Help 1 Answer

String as variable name, reflection or dictionary or other 2 Answers

Trying to sort an array. What's wrong here? 1 Answer

How to make an array of Interactive Clothes in one variable 0 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