Hi, I have just started programming on Lua. I have looked through a number of books and I have some previous experience in Processing.
I need to create an array of objects, how do I do this? Is there an example, or some special concept I should look for? Tables don’t look like the data structure I need, neither do the strings. It seems I am missing something.
I have read some of the sources and I am still confused. (I knew I could have finished reading everything first - but I still lack some basic understanding). In the sources, the authors mainly deal with numerical / string values for the “table” data structure.
From what I gathered, if I have a number in some cell of the table, then I will be able to add some number to it, or do other math operations on it. Basically, this cell is now a number.
Now, what if I have a Meteor class, each instance of which is a specific meteor. As I need many of these it won’t do me any good to just manually define objects from this class. What I want is a table of objects which I will define / initialize automatically.
Can I put an object (instance) into a cell and then deal with cell the way I would with the corresponding object?
Say,
Asteroids = {}
for i = 1, 5, 1 do
Asteroids [i] = Meteor (x,y)
i ++
end
And then, later:
Asteroids [1].update
–“update” is a method (from the Meteor class)
In such case, it will be storing this object’s info in the hash part of he table, right?
I am new using Codea/Lua and I have a particular problem accessing 2d arrays. My 2d array is working fine but the next operation is not valid in Codea/Lua.
tmp = tiles[x][y-1]+tiles[x][y+1]+tiles[x-1][y]
I cannot understand why not. Compiler say me that It is accessing a nil value.
I don’t see a problem with your code. Most likely, one of the items in your table has a nil value (perhaps you haven’t given it a value), and that will cause the error you are getting.
In a case like this, I might add a line just before the one above, to print out the values and see which one is the problem
print(x, y, tiles[x][y-1], tiles[x][y+1], tiles[x-1][y])
@genarojm It could be at the point where you’re trying to access an entry at [y-1] or [x-1]. If you’re at the first entry, then you’re trying to access an entry that doesn’t exit.
Hi dear Ignatz and Dave, thanks for you reply and help.
I print values and just print fine the first two ones (x,y) but other values for array stop the program and none print is done. Explicitly say: attempt to perform arithmetic on field ‘?’ (a nil value)
Hence, I had implemented four functions with a conditional to check correct values for limits of x and y, and return a correct value for y-1, y+1, x-1, and x+1. But the problem persist. Compiler say me that I want an operation on a nil value. Of course, I have a correct software that I done in C-Objective/Cocoa. But testing some quick implementations in Codea works different. Actually I can create, fill and draw my array correctly on my application but I cannot continue correctly the animation because this operation has a conflict. Well, here is my solution.
... tiles[check_left(x-1)][y] + ...
function check_left(xx)
if(xx < 1) then
xx = WIDTH/tileSize -- tileSize is my square
return xx
end
return xx
end
The idea of printing as I suggested, is that if you do it before the line with the error, then it will tell you exactly which values of x and y caused the problem