Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

removing multiple objects from a table

edited January 2013 in General Posts: 58

I have a table called

myTable = {object1, object2, object3}

All objects from the table are from the same class, and they all have a variable called index
To I remove object2, let's say, from the table, I would call the function removeObject(index), with a parameter of object2.index
Of course all the indecies will change after removing each object, so I call the function updateObjectIndecies() to make sure all the indecies of all the objects are accurate

function removeObject(index) --2
table.remove(myTable, index)
updateObjectIndecies()
end

function updateObjectIndecies()
for i, object in ipairs(myTable) do
object.index = i
end
end

Is there an easier way to do this? Sometimes an object doesn't get removed from the table for some reason (in my program). Any suggestions?

Comments

  • Jmv38Jmv38 Mod
    Posts: 3,295

    Suggestion: usually you do not need the object to have an index pointing to its position in the table. Because the way to access objects is: for i,obj in pairs(list), or touch, or collide. So the 'easier way to do this' could be 'don't'.

  • Posts: 58

    I'm confused about what you think I should do... Do you think I should leave everything the same but not use the updateObjectIndecies() function, or something else?

  • dave1707dave1707 Mod
    Posts: 7,533

    ..@edat44 Here is an example where I constantly create and destroy objects in a table. I’m showing the counts of the created and destroyed objects. I also show the count of the objects currently still in the "btab" table. A new object is created every .3 seconds and one gets destroyed when it crosses the line at y=300.


    displayMode(FULLSCREEN) supportedOrientations(PORTRAIT) function setup()     created=0     destroyed=0     et=0     btab={} end -- create the object and insert it in the table function createBall()     c=physics.body(CIRCLE,20)        c.x=math.random(WIDTH)     c.y=1000       c.gravityScale=math.random()     table.insert(btab,c)     created = created + 1 end -- destroy the object, then remove it from the table function destroyBall(a,b)     b:destroy()     b=nil     table.remove(btab,a)     destroyed = destroyed + 1 end function draw()     background(40, 40, 50)     fill(255)     str=string.format("Created %d        Destroyed %d       btab size %d",         created,destroyed,#btab)     text(str,380,200)     stroke(255)     strokeWidth(4)     line(0,300,WIDTH,300)     if ElapsedTime-et>.3 then         createBall()         et=ElapsedTime     end     noFill()     for a,b in pairs(btab) do         ellipse(b.x,b.y,40)         if b.y<300 then             destroyBall(a,b)         end     end end
  • Jmv38Jmv38 Mod
    Posts: 3,295

    .@edat44 what do you need the index for? Where do you use it? My question is 'do you really need it?' and if you find out you don't need it, well, yes, give it up, and the update too. It will be simpler. But i don't know your program, so my suggestion might not be relevant.

Sign In or Register to comment.