- Home /
Loop through array until certain value is found.
Hi everybody
I've made 2 scripts, 1 holds an array of gameobjects and I want the second script to search for the gameobject it's attached to in the array. If this object is found I want this script to tell me in which position in the array this object can be found. How can I do this? I have been told that I must use a for loop to loop through the array, but the array also changes size. I've told the array to add 1 to its length if it's completely filled.
Please help me solve this problem.
@Fattie: List uses an array internally anyway, so if you want to give yourself the extra headache, I say go for it xD.
Answer by whydoidoit · Jun 23, 2012 at 01:15 PM
Don't use an array, use a List.\ by importing System.Collections.Generic
List has an IndexOf function that will give you what you want. You can still write code that looks like an array - the only difference is use Count rather than length for the number or items.
Thanks, that sounds pretty useful. I'll try it as soon as possible and I'll tell if it worked as I expect it to work.
But how can I make sure that all items in my List are different? Is there a special way to do that?
Ah yes, you can use a HashSet.\ or a Dictionary keyed off GameObject.
Answer by Drakestar · Jun 23, 2012 at 01:18 PM
The array should not change size while you're iterating over it (if it is, stop - you need to rethink your approach to what you're doing). You can just do a simple loop using a counter i, and access whatever element satisfies your search criteria by breaking and returning the value of i when you're found it.
int index = -1;
for (int i = 0; i < myArray.Count; i++)
{
if (myArray[i] == <whatever criteria you specify>)
{
index = i;
break;
}
}
if (index != -1)
<act on successful search>
Alternatively, you might consider using a .Net list instead, and search for your object using List.FindIndex() using a Lambda. It returns the index of the element. For example
int index = myList.FindIndex(o => o.Name == "ObjectNameImLookingFor);
if (index != -1)
<act on successful search>
sucks. $$anonymous$$ill it with fire!
is a reality in .NET ILists. It is clearly documented, not bound to be confusing (because in C# arrays and lists -1 is never a valid index, so -1 takes on its own, documented meaning) and used all over the world in professional code. I'm not going to discourage people from using it because of some ideological righteousness. The #1 StackOverflow answer rightfully points out that "Code complete also maintains that you must meet the contracts you agree to and therefore List.Indexof must return -1" - so "sucks, kill it with fire" strikes me as a disproportionate reaction unless you can cite specific examples of where -1 leads to actual (not ideological!) problems in production code.
@Drakestar: Only thing that can possibly counter your argument (as far as I know) is the custom scripting system from Ragnarok Online. -1 wrapped around the array on there so it would return the last value in the array.
@$$anonymous$$izuho Yes - I'm sure there's reasons (I don't know Ragnorak Online, but it sounds like even there this wouldn't cause confusion because the -1 was the result of a search operation, where it retains its "element not found" meaning). But unless those reasons actually affect the person who asked this question, and while we're on a board with a large population of people getting their bearing in program$$anonymous$$g, I don't see much use in getting into largely ideological arguments. Especially when the argument is backed up by a StackOverflow article that itself was "closed as not constructive" by multiple users.