This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Selim's profile picture

Get handler within function within handler

Started by Selim, 05 February 2016 - 02:09 PM
Selim #1
Posted 05 February 2016 - 03:09 PM
If I have a custom handler for an object and I have a function within the handler, how would I get the handler variable to the function?
Example:

handler = {
handlerFunction = function()  end,
}
In the above example, how would I get handler to handerFunction?
Edited on 05 February 2016 - 02:15 PM
Lyqyd #2
Posted 05 February 2016 - 03:24 PM

local handler = {}

handler.handlerFunction = function() return handler end
Selim #3
Posted 05 February 2016 - 03:25 PM
Wow…should have thought of that…
wilcomega #4
Posted 05 February 2016 - 03:26 PM
I am not to sure on this, i have had similar problems, one theoretical sollution i thought of was to set the function environment of all functions to the handler and then give the handler a metatable with __index = _G.

but you will have to test it, maybe someone else has a better sollution


local handler = {}

handler.handlerFunction = function() return handler end
Wow…should have thought of that…

this works if you have one object with that name, but wont work if all objects are in the same array probably, maybe my sollution will work
Lyqyd #5
Posted 05 February 2016 - 03:29 PM
Uh, no, you might want to review scope, upvalues, and closures. It'll work anywhere as long as it's set up that way.