How to remove an instance of a class

Currently I am putting together a few simple ‘teach myself codea’ programs. In my main draw function I have:

for i,s in pairs(stones) do if s.y < 0 then table.remove(stones, i) else s:draw() end end

The table stones is appended in the main touched event:

function touched(t) if t.state == BEGAN then table.insert(stones, Stone(t.x,t.y)) end end

That feels like all I am doing is dereferencing the class, not removing it, but my performance impact as the stones fall off the screen feels appropriate.

Am I doing it the right way?

Lua has a garbage collector so once there’s no longer a reference to something then it will be marked as “garbage” and collected next time the binmen call. It’s every few seconds, so should be fairly efficient so long as you don’t create too many in one go.

If you have objects being created in the draw loop (or by a routine called on each pass through draw) you can definitely run afoul of the garbage collection. Otherwise, I haven’t run into any issues.