|
I am making a new flag system.
Inside it is a table that I will be used to store connections.
After a while, I wont need the connections.
I am hoping that there is a way to remove the connections with the lua garbage collector.
So, is there any metamethod that I can attach to the table that will be attached to a function that will disconnect the connection, then remove it (instead of just remove it normaly)? |
|
|
When you do not need the connection, just disconnect it
function variable:disconnect()
I don't know why you'd do it any other way. |
|
|
connection = Part.Touched:connect(function() print('blah') end)
connection:disconnect() |
|
|
But I will be having multiple connections, it would be easier for me to keep track of them in a table. |
|
|
ConnectionTable = {}
connection = Part.Touched:connect(function() print('touched') end)
ConnectionTable[1]=connection
function Disconnect()
for k, v in pairs(ConnectionTable) do
v:disconnect()
end
end |
|
|
Well, I was hoping for a method that would use the garbage collector for collecting other parts at the same time.
But this method does work, and I can refine it to work in a script in a more customised way (though not like I would of though of).
Thanks |
|
|
Just put them all in a table and make that table a weak table.
And disconnect them when you don't need them anymore.
Here's an example:
local connections = setmetatable({}, {__mode = 'kv'})
Just put the connections in the connections table and disconnect them when you don't need them. The garbage collector will pick them up, because the connections table is a weak table. |
|
|
But will that method disconnect the event at the same time, or just stop reference to them? |
|
|
would it work to get the gc metamethod to take in each connection as a value then get that function to manualy disconnect it? |
|
|
We have answered your question. What you're trying to do makes no sense to us. It sounds to me like you do not know much about RBX.Lua. If you want a connection to disconnect after a certain amount of time, just disconnect it when you want it disconnected. |
|
|
Sounds like you don't understand the Lua Garbage Collector (can't blame you, its not fully working in Roblox and not often used).
I will still be looking into the garbage collector method, but you are 1/4 (according to last post) percent right (do not just assume please; I do understand the RBX.Lua Object Oriented Programming).
Go ahead, call this question answered, but I will continue to look around for the answer.
Question Closed |
|