| View previous topic :: View next topic |
sandworm
Joined: 11 Aug 2009 Posts: 31
|
Posted: Wed Nov 04, 2009 7:32 am Post subject: Array variable names |
|
|
|
Hey guys,
This might be a n00b question, but...
I have a bunch of variables for the needs of my character (say, needSleep, needFood, needFun, etc). They are float values that need to be sorted from least to greatest.
I can sort the values with Array.Sort(), but I can't seem to figure out how to get the variable names back from it.
If I print out my array, I get "15, 20, 22" etc, but what I need is "needFood, needSleep, needFun" in order.
Is there something like Array[0].name or something like that which can return the name instead of the value? Or, in this case, do I have to use a multidimensional array?
|
|
| Back to top |
|
|
sandworm
Joined: 11 Aug 2009 Posts: 31
|
Posted: Wed Nov 04, 2009 9:39 am Post subject: |
|
|
|
Ok, I've tried using a multidimensional array, but it was a no-go, because Array.Sort() can't sort it for whatever reason, even if the first row is integers.
Anyone have an idea? (please?)
|
|
| Back to top |
|
|
userno101
Joined: 04 Oct 2009 Posts: 41 Location: Vrbovec, Croatia
|
Posted: Wed Nov 04, 2009 9:54 am Post subject: |
|
|
|
Array store specific data value. You can create structure for your character :
| Code: | public struct str {
public int index;
public string Name;
.
.
.
etc...
} |
Then create array of str . This is just a hit of how to approach , you should post your code to see us how to correct it.
|
|
| Back to top |
|
|
sandworm
Joined: 11 Aug 2009 Posts: 31
|
Posted: Wed Nov 04, 2009 10:49 am Post subject: |
|
|
|
I don't really understand how this will work, since I have to use Array.Sort to sort by an integer value...
Can you elaborate?
For example, I have:
needSleep = 14
needFood = 3
needFun = 84
I need to use some function to sort these into the following (least to greatest, while returning the names):
needFood
needSleep
needFun
|
|
| Back to top |
|
|
Barbur
Joined: 30 Oct 2009 Posts: 25 Location: Barcelona, Spain
|
Posted: Wed Nov 04, 2009 11:11 am Post subject: |
|
|
|
| As userno101 says you need to create an structure with the variable name and the value and create an array of that struct. Then in the Sort function you should use a IComparer to indicate the value inside the struct you want to use for the sort. Look up on google for more examples.
|
|
| Back to top |
|
|
duck

Joined: 21 Oct 2008 Posts: 166 Location: Somerset, UK
|
Posted: Wed Nov 04, 2009 11:37 am Post subject: |
|
|
|
There are a number of ways to go about this. Probably one of the simplest ways would be to have 2 arrays - one for the names, and one for the values, and use the special overload of Array.Sort which allows you to sort 2 arrays in tandem, by the first array's values. Eg:
| Code: | string[] needs = new string[] { "sleep", "food", "fun" };
int[] needValues = new int[] { 5, 3, 4 };
Array.Sort(needValues, needs);
for (int i=0; i<needs.Length; ++i)
{
Debug.Log( needs[i] + ": " + needValues[i] );
} |
Output:
| Code: | food: 3
fun: 4
sleep: 5 |
Another way would be to use a different type of collection which supports key-value pairs such as a Hashtable or a Generic Dictionary.
_________________ Ben Pitt | irc (duckets) | twitter (robotduck) | blog |
|
| Back to top |
|
|
userno101
Joined: 04 Oct 2009 Posts: 41 Location: Vrbovec, Croatia
|
Posted: Wed Nov 04, 2009 11:53 am Post subject: |
|
|
|
I write this for console but with some minor modifications will work in Unity.
| Code: | class Program
{
public struct needdef
{
public string NeedName;
public int Value;
}
static void Main(string[] args)
{
needdef[] need = new needdef[3];
need[0].NeedName = "Eat";
need[1].NeedName = "Fun";
need[2].NeedName = "Sleap";
need[2].Value = 10;
need[1].Value = 123;
need[0].Value = 5;
IEnumerable<needdef> query = need.OrderBy(defin => defin.Value);
foreach (needdef defin in query)
{
Console.WriteLine("{0} - {1}", defin.NeedName , defin.Value );
}
Console.ReadLine();
}
} |
This is output :
|
|
| Back to top |
|
|
sandworm
Joined: 11 Aug 2009 Posts: 31
|
Posted: Wed Nov 04, 2009 5:58 pm Post subject: |
|
|
|
Wow, thank you both for your replies! I really appreciate your help. I'm trying Duck's solution first, because it's the easiest
Duck, I have one issue, I'm trying to use your logic, but I'm using JS, so I've tried to convert it, however I'm getting the error message: An instance of type 'Array' is required to access non static member 'Sort'.
Here's my code, any tips? (needSleep, needFun, and needFood are floats)
| Code: | var needs = new Array ("needSleep", "needFun", "needFood");
var needValues = new Array (needSleep, needFun, needFood);
var i = int;
Array.Sort(needValues, needs);
for (i == 0; i < needs.Length; i++)
{
Debug.Log( needs[i] + ": " + needValues[i] );
} |
Thanks again both of you guys for helping me!
|
|
| Back to top |
|
|
duck

Joined: 21 Oct 2008 Posts: 166 Location: Somerset, UK
|
Posted: Wed Nov 04, 2009 7:10 pm Post subject: |
|
|
|
| sandworm wrote: | | I'm trying to use your logic, but I'm using JS, so I've tried to convert it, however I'm getting the error message: An instance of type 'Array' is required to access non static member 'Sort'. |
The problem is, you're changing the code to use Array() which - confusingly - is a completely different type of container to built-in arrays like int[] and String[]. This is a direct translation of my C# to JS:
| Code: | var needs : String[] = ["needSleep", "needFun", "needFood"];
var needValues : int[] = [5, 3, 4];
System.Array.Sort(needValues, needs);
for (var i = 0; i < needs.Length; ++i)
{
Debug.Log( needs[i] + ": " + needValues[i] );
} |
_________________ Ben Pitt | irc (duckets) | twitter (robotduck) | blog |
|
| Back to top |
|
|
duck

Joined: 21 Oct 2008 Posts: 166 Location: Somerset, UK
|
Posted: Wed Nov 04, 2009 7:19 pm Post subject: |
|
|
|
And on this subject (the confusion between types of array in unity), may I recommend my latest blog article hot off the press:
"Unity Coding: Arrays, Hashtables and Dictionaries explained"
http://robotduck.wordpress.com/2009/11/04/88/
It goes into a fair amount of detail, but confusion between these different types of container is something I see come up a lot!
_________________ Ben Pitt | irc (duckets) | twitter (robotduck) | blog |
|
| Back to top |
|
|
sandworm
Joined: 11 Aug 2009 Posts: 31
|
Posted: Thu Nov 05, 2009 3:07 am Post subject: |
|
|
|
Oh man, thank you so much! I so much appreciate your help! I'll check out your blog this morning...
One question, just for my understanding, why do you have to call System.Array.Sort?
|
|
| Back to top |
|
|
andeeee Forum Moderator

Joined: 19 Jul 2005 Posts: 1603 Location: Blackpool, United Kingdom
|
Posted: Thu Nov 05, 2009 3:41 pm Post subject: |
|
|
|
| sandworm wrote: | | One question, just for my understanding, why do you have to call System.Array.Sort? |
You mean as opposed to just Array.Sort? Array.Sort works with Unity JS arrays, whereas System.Array.Sort works with the "built-in" arrays.
_________________ None of you understand... I'm not logged-in here with you - YOU'RE LOGGED-IN HERE WITH ME! |
|
| Back to top |
|
|
duck

Joined: 21 Oct 2008 Posts: 166 Location: Somerset, UK
|
Posted: Fri Nov 06, 2009 3:36 pm Post subject: |
|
|
|
| andeeee wrote: | | Array.Sort works with Unity JS arrays, whereas System.Array.Sort works with the "built-in" arrays. |
Actually I don't think it is quite correct.
Javascript arrrays do have a .Sort() function, and you call it like this:
...but this is not the same as Array.Sort(myArray) - which is for built-in arrays. However, because Javascript has its own built-in type called "Array", this overrides the meaning of the word "Array". This is why you need to prefix it with "System", to disambiguate which meaning of the word "Array" you're talking about
- Ben
_________________ Ben Pitt | irc (duckets) | twitter (robotduck) | blog |
|
| Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You can attach files in this forum You can download files in this forum
|
|