- Home /
Explanation on how to use LIST<> in UnityScript
Hi,
I'm on a mission to find the index number in a List of a transform by it's name.
I was previously using Arrays but I have been pointed in the direction of Lists. From what I can tell they are like Arrays but with lots of cool built in functions. There is a tiny performance hit from what I can tell, but apparently nothing to worry about in smaller projects. Here is an excellent reason why to use Lists instead of Array:
http://answers.unity3d.com/questions/198318/javascript-array-use-with-a-struct-.html
So they sound great but I'm having problems getting to grips with it. So far I have found out how to get the name of an item from it's index array. But I can't get it to work the other way around, and give me the index number by searching the name of the item in the list. Here is an example of my code so far:
import System.Collections.Generic;
var attackerList = new List.< Transform>()
//attackerList list populate here
function Start () {
var thisItem = attackerList[2];
print ("Attacker name is=" + thisItem);
}
So does anyone know of any good guides out there? And any suggestions on how to jump the final hurdle ;)
Thanks!
Paul
[1]: http://answers.unity3d.com/questions/198318/javascript-array-use-with-a-struct-.html
Answer by Eric5h5 · Oct 03, 2012 at 09:58 PM
Use List.FindIndex:
import System.Collections.Generic;
var attackerList : List.< Transform>;
function Start () {
var index = attackerList.FindIndex (function (tr : Transform) tr.name == "TransformNameHere");
Debug.Log (index);
}
Hi Eric5h5. That works a treat, and I've just realised that you introduced me to Lists in a previous question. I never knew they existed and missed what exactly was going on. Thanks once again, and thanks to everyone who helped me in my lists adventure! I'm going to play with them more tonight!
I'm still looking for a tutorial, even though this problem is fixed, it would be great to find a good guide. Feel free to shout out!
Paul
I just found this which may be useful: http://wiki.unity3d.com/index.php?title=Which_$$anonymous$$ind_Of_Array_Or_Collection_Should_I_Use%3F
I am now completely sold on Lists. Now that I am able to locate the index number of my transform, I can remove it from my list with ease (which is how this whole journey started!). All you have to do is:
attackerList.RemoveAt(index);
Of course in that example I am referencing my own variables to match Eric5h5 solution above. The reference structure is: myList.RemoveAt(i);
Hope this helps others.
Answer by MickM · Oct 03, 2012 at 09:47 PM
I use a basic loop in something I am working on to add an ability by its index on a master list by using the below:
function AddAbility(skill : Ability){
var abilityIndex = GetAbilityIndex(skill, masterAbilityList);
if (abilityIndex != -1){
AddAbility(abilityIndex); //This calls an AddAbility (int) method in case you were wondering
}
}
function GetAbilityIndex(theItem : Ability, theList : AbilityList){
for (var i = 0; i < theList.abilities.length; i++){
if (theList.abilities[i] == theItem){
Debug.Log("Item is at #" + i);
return i;
}
}
Debug.Log("Error Ability Index!");
return -1;
}
So I would assume for you, something like:
for (var i = 0; i < attackerList.length; i++){
if (attackerList[i] == transform){ //Current script transform -- can replace with other values if needed
Debug.Log("Item is at #" + i);
return i; // returns the index of the transform
}
Thanks $$anonymous$$ick$$anonymous$$! I'm going with Eric5h5 on this one but have an upvote for taking the time ;)