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

Calling function via name using getfenv(), problems as API

Started by guesswhat123212, 03 April 2014 - 12:11 AM
guesswhat123212 #1
Posted 03 April 2014 - 02:11 AM
Hello all below is a description of the problem I am having and then the code I am using.

Desc: I made a code for touchscreen buttons that will run functions when the button is clicked. The code works as code and even works as an API as long as the functions called are in the API but not the program itself. Does anyone know how to read the functions from the program when called and not the API, or can push me to some reading material?

button API: http://pastebin.com/dqbdbug2

test program: http://pastebin.com/yzakf5qq

error after "button 2" is clicked running testapi: butapi:134: attempt to call nil (it does not see the function as its in testapi and not within butapi, I need to be able to call from testapi)

mind you I do not get this error if I hit button 1 because that function is in the API code as a test function



Edit: I just noticed that there is an error in my code for setting the next button area, it looks for hMax > h when it should say wMax > w, I am currently adding auto assign of multiple pages and found/fixed this issue. However above error is still a problem.
Edited on 03 April 2014 - 02:12 AM
apemanzilla #2
Posted 03 April 2014 - 05:46 AM
Why don't you just pass the function itself to the API? It would simplify everything and get rid of the getfenv entirely.
guesswhat123212 #3
Posted 03 April 2014 - 02:43 PM
I will look into that, when it was just code on my computer and not an API that was the only way I could get the function to run from a string in the table. once I get home today and test that I will update
apemanzilla #4
Posted 03 April 2014 - 04:49 PM
I will look into that, when it was just code on my computer and not an API that was the only way I could get the function to run from a string in the table. once I get home today and test that I will update
Don't pass the string - pass the function itself, and just call it later.
guesswhat123212 #5
Posted 03 April 2014 - 11:53 PM
so with the advice of pass the function not the string I redid a few things and got it working. Instead of using getfenv() I just gave the var fname to what ever was passed and ran fname(). Thanks for the help, I also expanded the API to add page buttons and let you flip between the pages. here are the new links to the code if anyone would like to see/use it for any reason (even learning tho I am sure my code is a bit ugly). its fairly simple for the end user to use.

butapi http://pastebin.com/UYePv49v
testapi http://pastebin.com/dpqux2rp