[Resolved] change value of index in for loop table?

hey guys, I am really stuck with this one. and I would really appreciate some help. I think I am just not use to the syntax. using a for loop, I spawn 10 objects. I want to label them each 1,2,3,4,1,2,3,4,5. I want to start the count over so that I can place all the objects with a 1 on one side of the screen and 2 on another part of the screen etc.
Here is my code.
local spawnTable = {}

[lua]–Function to spawn an object
local function spawn(params)
local object = display.newImage(params.image)
physics.addBody (object, {density = 1})
–Set the objects table to a table passed in by parameters
object.objTable = params.objTable
–Automatically set the table index to be inserted into the next available table index
object.index = (#object.objTable + 1)

–Give the object a custom name
object.myName = object.index
–Insert the object into the table at the specified index
object.objTable[object.index] = object
return object
end
–Create 2 spawns
for i = 1, 5 do
local spawns = spawn(
{
image = “green.png”,
objTable = spawnTable,
}
)
end

–Now print the names of both created spawns
for i = 1, #spawnTable do
print(spawnTable[i].myName)

spawnTable[i] = object

print(spawnTable[i])
– spawnTable[1].y = 300
end[/lua]
Any help at all would be great. Thanks tons in advance!
SM

*Peach: Edited this to add in < lua > tags and make code readable :wink: [import]uid: 79586 topic_id: 28529 reply_id: 328529[/import]

Hey, I edited your original post to add in < lua > tags as it wasn’t really readable without them; please try to do them in the future :wink:

Are you able to post some plug and play?

How many objects are you planning on spanning today? Just the 9 (1,1,2,2,3,3,4,4,5?) [import]uid: 52491 topic_id: 28529 reply_id: 115108[/import]

Hi,
Thanks for responding. I actually was not aware of the tags, I will do that in the future, thanks. Also, I did eventually figure out a solution. Thanks again.
SM [import]uid: 79586 topic_id: 28529 reply_id: 115183[/import]

No worries, glad to hear you found a solution.

Good luck with your project!

Peach :slight_smile: [import]uid: 52491 topic_id: 28529 reply_id: 115358[/import]