You are right. I should have explained that loop a bit better.
And probably modify a bit the variable names.
Let me see.
The easiest way to understand the double loops there is by having a look at TileTable
Code: Select all
TileTable = {
{ 1,1,1 },
{ 1,2,1 },
{ 1,1,1 }
}
Let's say that the 1s represent grass and the 2s represent a box. Then we need something that eventually tells us "here goes grass" and "here goes a box".
The easiest way is by making two loops, one inside the other.
The first loop (or "external" loop) is used to set up a variable called "row". It will contain different rows of TileTable. For example, with a TileTable like the one above, row will have the value {1,1,1} on the first iteration, {1,2,1} on the second and {1,1,1} again on the third (and last). That is the same as doing TileTable[1], TileTable[2], and TileTable[3]. That [1],[2],[3] is currently managed by the "y" variable - I should have named it rowIndex or something similar.
But we still don't have something that tells us "grass goes here". What we have with "row" is a "list" of several horizontal tiles. We need individual ones. So, we need to do more stuff with row.
Row will be {1,1,1}, then {1,2,1} and then {1,1,1}. We can do stuff with it before it changes its value.
Code: Select all
{1,1,1} do stuff here {1,2,1} do stuff here {1,1,1} do stuff here
The "stuff" we do is using an
internal loop to go over it. This way, we for example, when row is {1,2,1}, we want something that is 1 on a first iteration, 2 on the second and 1 on the third. That's the "number" variable. At this point, we finally have something that, iteratively, tells us "this is grass" or "this is a box". It's called "number" because it holds "the number that represents a tile in a row". I can't think of a better name for it - I'm open to suggestions.
The 1,2,1 values of number can be obtained by doing row[1], row[2] and row[3]. That "1,2,3" is controlled by the "x" variable. I should rename it to something else, so people don't get confused with "coordinates". But I don't know what.
So, we have something that tells us "grass", "grass", "box", etc. The only thing missing is the "here" of "grass goes here". That is a simple calculation that can be done with "x" and "y" (names will be changed).
Let me know if this explanation is good enough.