How to remove an instance of a class
  • SciQuestSciQuest February 17

    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?

  • Andrew_StaceyAndrew_Stacey February 17

    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.

  • MarkMark February 17

    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.

Howdy, Stranger!

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

Sign In with OpenID Sign In with Google

Sign In Apply for Membership

In this Discussion

Tagged