Object arrays / array lists

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.

Thank you in advance,
Alex

What functionality do you require that tables do not have?

@alexNaumov - welcome! I enjoyed Shiffman’s Processing tutorials, and found it to be very similar to Codea.

Lua is rather strange in that tables are used for EVERYTHING except simple numbers and strings. Arrays? Check. Dictionary? Check. Classes? Check.

It sounds absolutely crazy, but Lua tables are extremely flexible and powerful.

I’ve written an ebook on Lua that tries to explain this. I also have a bunch of blog posts on tables.

You can find all of them here, ebooks at the top

http://coolcodea.wordpress.com/2013/06/19/index-of-posts/

Thank you so much for all the references, I will read everything.

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?

Absolutely. You can store objects and even functions in variables and tables, because Lua simply stores the memory pointer, not the object.

But to be more accurate

for i = 1, 5 do 
    Asteroids [i] = Meteor (x,y) 
end

--and
Asteroids [1]:update --note the colon!!!!!

I have a couple of blog posts on classes which explain the use of colons and “self”…

Thank you. can you please tell me where can I find your blog?

Is it this one?
http://coolcodea.wordpress.com/2013/06/19/index-of-posts/

yep, the ones on classes are here

http://coolcodea.wordpress.com/2013/03/22/7-classes-in-codea/
http://coolcodea.wordpress.com/2013/03/23/8-classes-in-codea-2/
http://coolcodea.wordpress.com/2013/06/14/84-a-practical-example-showing-the-value-of-classes/
http://coolcodea.wordpress.com/2014/10/01/169-why-tables-and-classes-are-so-useful/

and I have some posts on tables too, if you look on that index page

Hi,

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.

Could you help me please?

Thank you very much in advance.
Genaro

Welcome!

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

Yes, it was the problem. Printing just array with random values it is fine. But, printing some x-1 or x+1 index hence print nil. Thank you very much!