I've figured out how to emulate parenting with metatables, but is there a way to run code when an object is dereferenced? The documentation I've seen says something about __gc, but I tried that and _gc and they didn't work.
Deconstructor in Lua?
Posted by Cage_fire_2000 on Sun 28 Sep 2008 12:17 AM — 9 posts, 34,753 views.
What do you mean by dereferenced exactly? Lua variables that are not referenced by anything are eventually garbage-collected, however that might be at some future time.
If you have a __gc metatable entry for a table then that function will be called during garbage collection.
If you have a __gc metatable entry for a table then that function will be called during garbage collection.
__gc only works for userdata references. So basically only from the C side of things.
Looks like you are right. :)
I suppose the idea is that normal Lua data knows how to be garbage collected without help from the user.
Thus I can't see any easy way to implement destructors in that case. Even the garbage collection idea wouldn't have worked literally, in the sense that it would not have been called the moment a variable went out of scope, but later (maybe hours later) when the garbage collection was done.
I suppose the idea is that normal Lua data knows how to be garbage collected without help from the user.
Thus I can't see any easy way to implement destructors in that case. Even the garbage collection idea wouldn't have worked literally, in the sense that it would not have been called the moment a variable went out of scope, but later (maybe hours later) when the garbage collection was done.
You can control the garbage collection, but I doubt you really want to get into it...
http://www.lua.org/manual/5.1/manual.html#pdf-collectgarbage
http://www.lua.org/manual/5.1/manual.html#pdf-collectgarbage
What is the need for a destructor of something that's Lua-only? Even if it's holding on to resources, if everything is Lua-only -- or has __gc metamethods implemented correctly -- resources will get freed.
The destructor isn't really supposed to be used to implement functionality that has to happen as soon as the logical entity is gone. That is, if you want something to happen when, say, a character dies, you should handle it somewhere other than __gc. Like Java's finalize method, there is basically no guarantee as to when it will be called.
The destructor isn't really supposed to be used to implement functionality that has to happen as soon as the logical entity is gone. That is, if you want something to happen when, say, a character dies, you should handle it somewhere other than __gc. Like Java's finalize method, there is basically no guarantee as to when it will be called.
Let me explain, I was toying around with making a MiniWindow package or module or whatever it's called to ease creation and manipulation of miniwindows. I wanted to use a deconstructor to call WindowDelete(), although with the way I've been coding it it'd probably be better if I just use a delete method. I've already created a sort of miniwindow 'class' and it has basic methods for defining, moving, resizing, showing/hiding the miniwindow. I'm coding it more for the learning experience than actual practical use right now. But I hope to figure out ways to make buttons and labels and such. Is anybody working on anything similar BTW? I really should download some of the plugins you guys have been making for reference.
Anyway, right now the way my thing works is:
Stuff like that.
Anyway, right now the way my thing works is:
Win1 = miniwindow.new("Win1")
Win1:define(200, 0, 100, 100)
Win1:show(1)
Win1:move(300,100)
Win1:delete()
Stuff like that.
In this case, a deletion method is really what you want, not __gc. If you depend on __gc, you might have to wait quite a bit after the last reference is lost before the window goes away. I'm assuming that you want to control exactly when a window disappears.
Yeah, delete() method would be preferred, but I was thinking of having it as a failsafe in case they forgot. Although no harm done I guess, they just have to close and reopen the world and it's gone.