- Home /
Find the correct index number for array inside two loops
Hey everyone. I'm trying to make some simple inventory with GUI.Box or something. I've made an List of type 'Item' wich is a custom class that control every single slot and I need to access each index of this list when I open the inventory. For this, I'm using the following code:
public int rows, columns;
void Start () {
for(int x = 0; x < rows; x++) {
for(int y = 0; y < columns; y++) {
int index = ???
Inventory[index].inUse = false;
}
}
}
index = x + (y * columns) used to work but just when is a square-like inventory, like 5x5. Anybody have an idea of an algorithm that can calculate the current index based on x and y and in order to access the List ? Otherway I just receive 'Argument is out of range' errors, wich makes total sense.
Any help would be appreaciated! Thanks from now!
Here's a image of what the index should looks like(don't need to start with 1).
125125
Eric5h5, I've done a lot of progress in this system already and I think if I rebuild the base I'll have too much work to put everything to work again. With 2D array you mean an Array of Vector2?
No, just a 2D array.
inventory = new Item[rows, columns];
That can be a possibility, too! Works both with array and lists?
No, it's a 2D array. There isn't such a thing as a 2D List per se, though you could have a List of Lists and accomplish basically the same thing.
Answer by Bunny83 · May 02, 2012 at 10:57 PM
It's very confusing when you swap x and y. x is usually always horizontal (from left to right). Here's how i would go about it:
public int rows = 3;
public int columns = 4;
void Start ()
{
for(int y = 0; y < rows; y++) {
for(int x = 0; x < columns; x++) {
int index = x + (y*columns);
Inventory[index].inUse = false;
}
}
}
This will give you an index from 0 to 11 (12 elements == rows*columns)
The important thing is that the next row starts at the next index where the last column ends. first row: 0 1 2 3
so the second row starts at 4 so all indices have to be offset by 4. 4 is actually the length of 1 row (the column count). So the offset is just the row index multiplied with the column count.
x + y * columns
0 + 0 * 4 == 0
1 + 0 * 4 == 1
2 + 0 * 4 == 2
3 + 0 * 4 == 3
0 + 1 * 4 == 4
1 + 1 * 4 == 5
2 + 1 * 4 == 6
3 + 1 * 4 == 7
0 + 2 * 4 == 8
1 + 2 * 4 == 9
2 + 2 * 4 == 10
3 + 2 * 4 == 11
That works perfectly! I don't know what I was doing wrong, maybe I got confused about the rows and columns... Thank you and thanks to everyone wich posted here to help me! Also, thanks for the explanation about a loop inside another.
$$anonymous$$an what an incredibly simple tutorial, you just wouldn't be able to believe HOW $$anonymous$$UCH this helped me to understand arrays, you're incredible really I don't have even words to describe how glad I am since I've been studying hard for months already.
Wish you all the best and I just created this account to thank you! haha XD
@$$anonymous$$acronaut123:
:D Thanks, i'm glad i could help :)
Answer by rutter · May 02, 2012 at 10:28 PM
For each row you've passed, you'll need to add the number of items per row to your index -- this happens to equal the number of columns in your grid. This seems to match your existing formula:
index = x + (y * columns);
Take a look at your loops, though. You're using x
to track rows
(vertical movement), and y
to track columns
(horizontal movement). Is that what you wanted?
Remember also that arrays are indexed from zero. If I understand your plans correctly, that box you've labelled "1" is Inventory[0]
. Seems like you understand that, but it's good to be sure. ;)
rutter, thanks for the reply, I'll double check the formula's work. Yes, at least to me the x (row) and y (column) makes total sense to me. Row isn't a line( ___ ) wich represents x axis? If I wrote something wrong is probably because I'm brazilian and not an English pro yet. The part of the array index I'm doing correctly. I tried to add the index.ToString() to the box in order to display the current index and it doesn't comes straight forward, but I'll double check this.
Yes a row is a line that goes from left to right, but the x coordinate specifies the position along this line therefore which column. y tells you which row you want to access(from top to bottom).
At least that's the usual geometric / mathematical definition ;)
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Random object, Array index out of range 0 Answers
Wierd Animation Bug 0 Answers
array problem index out of range ? why ? 2 Answers
ArgumentOutOfRange Exception 1 Answer