@Ignatz
This works. You need something that creates a unique key though, such as a physics body call. Not sure how else you could this if you weren’t using physics.
function setup()
t={} --create two instances
a("alive") a("doomed")
for i,v in pairs(t) do
print(v.status) --prints alive, doomed
if v.status=="doomed" then v:kill() end
end
for i,v in pairs(t) do
print(v.status) --prints alive
end
end
a=class()
function a:init(status)
self.status=status
self.body=physics.body(CIRCLE, 10)
t[self.body]=self --take advantage of unique memory address returned by body
end
function a:kill()
self.body:destroy()
t[self.body]=nil
end
@Ignatz on second thoughts, you can just index it with the self, you don’t need a physics body. This seems to work:
function setup()
t={} --create two instances
a("alive") a("doomed")
for i,v in pairs(t) do
print(v.status) --prints alive, doomed
if v.status=="doomed" then v:kill() end
end
for i,v in pairs(t) do
print(v.status) --prints alive
end
end
a=class()
function a:init(status)
self.status=status
t[self]=self
end
function a:kill()
t[self]=nil
end
The difference, when you think about it, is that in my code, t is indexed by 1 and 2, not by the self variables. So t[self]=nil doesn’t do anything, of course. It would need to be t[1]=nil or t[2]=nil.
If I define my table like this, it works.
local s1,s2=a("alive"), a("doomed")
t={}
t[s1],t[2]=s1,s2