Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are
spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the
password reset link.
Entire forum
➜ MUSHclient
➜ Lua
➜ Deconstructor in Lua?
Posting of new messages is disabled at present.
Refresh page
Posted by
| Cage_fire_2000
USA (119 posts) Bio
|
Date
| Sun 28 Sep 2008 12:17 AM (UTC) |
Message
| 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. | Top |
|
Posted by
| Nick Gammon
Australia (23,122 posts) Bio
Forum Administrator |
Date
| Reply #1 on Sun 28 Sep 2008 06:28 AM (UTC) |
Message
| 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. |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
Posted by
| WillFa
USA (525 posts) Bio
|
Date
| Reply #2 on Sun 28 Sep 2008 07:23 AM (UTC) |
Message
| __gc only works for userdata references. So basically only from the C side of things.
| Top |
|
Posted by
| Nick Gammon
Australia (23,122 posts) Bio
Forum Administrator |
Date
| Reply #3 on Sun 28 Sep 2008 09:12 PM (UTC) |
Message
| 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. |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
Posted by
| Larkin
(278 posts) Bio
|
Date
| Reply #4 on Mon 29 Sep 2008 12:01 PM (UTC) |
Message
| 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 | Top |
|
Posted by
| David Haley
USA (3,881 posts) Bio
|
Date
| Reply #5 on Mon 29 Sep 2008 03:04 PM (UTC) Amended on Mon 29 Sep 2008 08:42 PM (UTC) by Nick Gammon
|
Message
| 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. |
David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone
http://david.the-haleys.org | Top |
|
Posted by
| Cage_fire_2000
USA (119 posts) Bio
|
Date
| Reply #6 on Mon 29 Sep 2008 05:03 PM (UTC) |
Message
| 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:
Win1 = miniwindow.new("Win1")
Win1:define(200, 0, 100, 100)
Win1:show(1)
Win1:move(300,100)
Win1:delete()
Stuff like that. | Top |
|
Posted by
| David Haley
USA (3,881 posts) Bio
|
Date
| Reply #7 on Mon 29 Sep 2008 05:36 PM (UTC) |
Message
| 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. |
David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone
http://david.the-haleys.org | Top |
|
Posted by
| Cage_fire_2000
USA (119 posts) Bio
|
Date
| Reply #8 on Mon 29 Sep 2008 05:52 PM (UTC) |
Message
| 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. | Top |
|
The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).
To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.
25,571 views.
Posting of new messages is disabled at present.
Refresh page
top