Referencing Numbered Objects in a FOR loop?

Posted more or less the same question in another thread with some more details, but hopefully this one’s less convoluted :slight_smile:

How can I reference an object within a for loop using the count as part of the objects reference?

For simplicities sake, let’s say I have 2 images on screen:

local image1 = display.newImage( “firstimage.png”, 0, 0)    
local image2 = display.newImage( “secondimage.png”, 0, 0)  
 

If I want to use a for loop to access both images - what’s the proper syntax to reference them, using the “image” prefix + the “i” count?

Something like…

for i = 1, 2, 1 do
    tryingtoaccess = “image” … i
    print(tryingtoaccess)
end    

…outputs the right string when printing it - but I can’t actually seem to reference an object with that strings name.  When I try, I get the error:

"attempt to index global ‘i’ (a string value)"

I’d be using this to move multiple existing images within that for loop, and had hoped something like this would work:

for i = 1, 2, 1 do
    tryingtoaccess = “image” … i
    tryingtoaccess.y = 200
end  
 

(which would move both image1 and image2 to that y co-ordinate accordingly.)

Any help much appreciated!

Put them into a table. The problem you have is that “image”…i is a string, not a reference to one of the display objects. So try: local image1 = display.newImage( “firstimage.png”, 0, 0) local image2 = display.newImage( “secondimage.png”, 0, 0) ImageTable = {image1, image2} for i =1, 2 do local tryingtoindex = ImageTable[i] end Tables are the key to doing anything even remotely complex in Lua, when you need to iterate through groups of objects put them in a table (or multiple tables) before hand to make things much easier further down the line.

It really depends what you need to do with this “data structure”.  I personally use a mix of tables, groups and “object.id = some number”.

As mentioned by Alan, the table option is always a good one, and it gives you performance (quick access, fast removal of the object if you do not re-index the table by using table.remove – just use display.remove(imageTable[2]) and imageTable[2] = nil).  I would also say that you can consider other two alternatives:

(1) creating a display group and transverse that using the # operator [for performance reasons, not recommended if the content of the groups is always changing quickly OR you need to access quickly a specific display object].  You can move the entire group by changing x and y coordinates for the group itself.  Do not abuse of setting x and y, for example in an enterFrame listener (the operation will suck the processor unconscious if done to too many objects at each frame).

(2) assign an “ID” to the object (i.e. image1.id = 1 or “my photo A” or image1.my_id = 1 or “my photo B” or whatever name you want to give to the idea of “ID”).  That is good for collision detection or, generally speaking, where the display object i passed to some listener and you need to id the object only at that point (e.g. event.target.id to identify which button has been pressed).

Also if you have many images call them image1.png, image2.png and so on so that you can access the file with the name “image”…i…".png" in for do loops. Remember that operator … is somewhat slow.  Even better put them all in an image sheet in consecutive frames. 

Thanks so much to the both of you!

Put them into a table. The problem you have is that “image”…i is a string, not a reference to one of the display objects. So try: local image1 = display.newImage( “firstimage.png”, 0, 0) local image2 = display.newImage( “secondimage.png”, 0, 0) ImageTable = {image1, image2} for i =1, 2 do local tryingtoindex = ImageTable[i] end Tables are the key to doing anything even remotely complex in Lua, when you need to iterate through groups of objects put them in a table (or multiple tables) before hand to make things much easier further down the line.

It really depends what you need to do with this “data structure”.  I personally use a mix of tables, groups and “object.id = some number”.

As mentioned by Alan, the table option is always a good one, and it gives you performance (quick access, fast removal of the object if you do not re-index the table by using table.remove – just use display.remove(imageTable[2]) and imageTable[2] = nil).  I would also say that you can consider other two alternatives:

(1) creating a display group and transverse that using the # operator [for performance reasons, not recommended if the content of the groups is always changing quickly OR you need to access quickly a specific display object].  You can move the entire group by changing x and y coordinates for the group itself.  Do not abuse of setting x and y, for example in an enterFrame listener (the operation will suck the processor unconscious if done to too many objects at each frame).

(2) assign an “ID” to the object (i.e. image1.id = 1 or “my photo A” or image1.my_id = 1 or “my photo B” or whatever name you want to give to the idea of “ID”).  That is good for collision detection or, generally speaking, where the display object i passed to some listener and you need to id the object only at that point (e.g. event.target.id to identify which button has been pressed).

Also if you have many images call them image1.png, image2.png and so on so that you can access the file with the name “image”…i…".png" in for do loops. Remember that operator … is somewhat slow.  Even better put them all in an image sheet in consecutive frames. 

Thanks so much to the both of you!