I was wondering if somebody can help with me with some sample code for an array? I want to make a grid, lets say 3 columns by 3 rows.
in Java i’d do something like this…
int cols = 10;
int rows = 10;
grid = new Cell[cols][rows];
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
grid[i][j] = new Cell(i*20,j*20,20,20,i+j);
}
}
or something like that.
Am really new to lua ( and processing for that matter)
Any sample code would be appreciated…
Tak!
Sid [import]uid: 41518 topic_id: 7948 reply_id: 307948[/import]
I’m also new to lua and in the open spirit of sharing (that I’ve already benefited a LOT from try this…
local cols = 10
local rows = 10
local grid = { }
for i=1,cols do
grid[i] = { }
for j=1,rows do
grid[i][j] = { x=i\*20,y=j\*20,width=20,height=20,index=i+j }
end
end
My understanding of this is that each variable can be of any type it needs (including objects - in which case the variable is a table)
The above code creates an empty table called grid and then as it loops through cols it sets each entry in grid to be a new empty table, then as the inner loop is processed the row table entries within the col table entry are created from a simple table constructor (I guessed at the field names based on what I thought you might be doing).
This kinda matches up to the Java concept of a 2D array actually being an array of arrays.
Also note it seems to be a convention in Lua that arrays start at 1 rather than 0 so a 10 element array (or table) is referenced from [1] to [10] rather than from [0] to [9].
Feel free to rip this completely apart if I’ve missed the point.