Individually identifying many objects?

Definitely a set of beginner questions here, but how do I control many objects individually? I’m creating a game simulating an infinite 2D space area, where I create a large number of celestial bodies. Much of the code is borrowed from the excellent beginner’s guide, but there are a few things I still need to figure out. 

local function createCelestial() -- the function run when a new celestial body is created local newCelestial = display.newImageRect("gameAsteroids.png", 102, 85) table.insert(celestialTable, newCelestial) table.insert(celestialX, math.random(-500,500)) table.insert(celestialY, math.random(-500,500)) print(#celestialX) --placeholder values newCelestial.x = 100 newCelestial.y = 100 physics.addBody(newCelestial, "dynamic", {radius =40, bounce = 0.8}) newCelestial.myName = "celestial" end
  1. When each celestial is created, in addition to their entry in the table celestialTable, I also generate an x and y value for them which is inserted directly into celestialX and celestialY directly. How can I automatically have these bodies always “go” to their individual positions on the screen?

  2. Secondary question. I need to be able to identify each celestial body individually, so later I can name each body and associate different data with each. My knee-jerk instinct is to use the index number of their entry in the table, but how would I retrieve this? Would something like local id = #celestialTable work? 

Why do you need to add the X and Y values to their own table?

Just set the X and Y values on lines 70/71, and if you need to retrieve these values later just use celestialTable[index].x and celestialTable[index].y.

The reason I would use tables instead of celestialTable[index].x and y is because they need to be retrieved constantly (i.e. every time the player moves), since they’re essentially located on an infinitely scrolling plane and when the ship “moves” up 1 the celestial bodies have to be moved down one (to mimic movement). 

Luckily, I found a fix by looking into display groups. By assigning all the CBs to a display group I can just move the display group, and the bodies move with it. 

Why do you need to add the X and Y values to their own table?

Just set the X and Y values on lines 70/71, and if you need to retrieve these values later just use celestialTable[index].x and celestialTable[index].y.

The reason I would use tables instead of celestialTable[index].x and y is because they need to be retrieved constantly (i.e. every time the player moves), since they’re essentially located on an infinitely scrolling plane and when the ship “moves” up 1 the celestial bodies have to be moved down one (to mimic movement). 

Luckily, I found a fix by looking into display groups. By assigning all the CBs to a display group I can just move the display group, and the bodies move with it.