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

how to use a program's function inside an API?

Started by Glotz659, 08 February 2013 - 05:22 AM
Glotz659 #1
Posted 08 February 2013 - 06:22 AM
i am trying to write my own API for buttons in CC (touchscreen monitors ftw) and i have the following problem:

my button api loadstrings a function stored in a table when one of the buttons is clicked. this works for standart functions, but it doesnt work for any function in the main program code.

i think this is solveable with setfenv and getfenv but i dont know what these do.
theoriginalbit #2
Posted 08 February 2013 - 06:33 AM
You could do it with setfenv and getfenv but its probably just easier to call the stored function


local someTable = {
  function() print("Hello from inside the table") end,
  hello,
}

local function hello()
  print("Hello from outside!")
end

someTable[1]()
someTable[2]()
Glotz659 #3
Posted 08 February 2013 - 07:26 AM
that is possible, but this way, you can't use arguments. If you save them as strings and loadstring() them, you can use them.
a workaround would be this:

derpTable = {
function () myfunction(par1, par2)
}
I think i'll do it this way, thank you!