x


finding an objects index in a multi dimensional static array?

ok so i have a selector that tells me the name of my object in my multi-dimensional array, but i want to know how i can have it send me back the index of that object. can anybody help me out?

more ▼

asked Oct 10 '11 at 04:21 AM

IMTRIGGERHAPPY9 gravatar image

IMTRIGGERHAPPY9
132 31 43 44

Depends on how your selector works! Presumably, to get the name of the object, you had to have the index at some point anyway- so return that instead!

I think you can use IndexOf(object), but that might only work for generic lists (I can't remember...)

Oct 10 '11 at 04:33 AM syclamoth

my selector is using raycasting

Oct 10 '11 at 04:35 AM IMTRIGGERHAPPY9
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Given that you know the transform (I assume it is an array of transforms?) you can use

var xPos : int = -1;
var yPos : int = -1;

for(var i : int = 0; i < array.GetLength(0); i++)
{
    for(var j : int = 0; j < array.GetLength(0); j++
    {
         if(selectedTransform == array[i, j];
         {
             xPos = i;
             yPos = j;
             break;
         }
    }
    if(xPos != -1)
    {
         break;
    }
}

And then you can use xPos and yPos! If they return -1, it means the selected transform isn't in the grid (oops);

more ▼

answered Oct 10 '11 at 04:44 AM

syclamoth gravatar image

syclamoth
14.8k 7 15 80

why do you have that break there?

Oct 10 '11 at 04:49 AM IMTRIGGERHAPPY9

break tells it "Ok I'm done with this now, don't bother looping through the rest of the iterations. kthxbai!". I've got it there to save time, although you don't techincally need it.

Oct 10 '11 at 04:53 AM syclamoth

if i where to turn this into a function i would have it return the xPos and the yPos right? and then i would send it my array that i have? thats it?

Oct 10 '11 at 04:56 AM IMTRIGGERHAPPY9

thanks dude if i could give you a million credit points i would right now! you have been so helpful!

Oct 10 '11 at 05:00 AM IMTRIGGERHAPPY9

Kind of? You can't return two values at a time without defining a type which contains them, and you'd need to send it both the array, and the transform you're searching for. I'm really not sure what the context of what you're doing is... (well obviously it's your TD that you are making, but more specifically)

Oct 10 '11 at 05:01 AM syclamoth
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2097
x1363
x1095
x65

asked: Oct 10 '11 at 04:21 AM

Seen: 867 times

Last Updated: Oct 10 '11 at 05:18 AM