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

API to call a function in my Main program

Started by Geko4515, 14 April 2013 - 05:18 AM
Geko4515 #1
Posted 14 April 2013 - 07:18 AM
Hello All,

I am writing an API for movement and all is going great. The only issue I am running into is printing.


local display = true
function updateDisplay()
--**************USER EDIT HERE****************--
--Display goes here or link to a function in main program via
--if display then exampleFunction() end
print(display)
if display then screen() end
end

I want my cords to print on every movement so after the move I call the above function screen() and want it to link back to my original program called Travel.
I get the following error: MoveAPI attempt to index ? (a nill value)

Also a side note, since this is an API I need it to not call a specific program i.e. Travel.screen()

Any help is much appreciated.
LBPHacker #2
Posted 14 April 2013 - 07:29 AM
So… If that's an API… You could put a function into it:
local screen = false
function passScreenFunction(func)
	screen = func
end

After loading the API, call its .passScreenFunction method:
os.loadAPI("MoveAPI")
MoveAPI.passScreenFunction(screen) -- your screen function's name here inside the brackets

This way the API will have a reference pointing to the screen function in your program.

EDIT: Anyways, if the user didn't provide a screen function, that would mean a problem. Solution: call screen only if it's a function:
if type(screen) == "function" then screen() end
Geko4515 #3
Posted 14 April 2013 - 07:39 AM
So… If that's an API… You could put a function into it:
local screen = false
function passScreenFunction(func)
	screen = func
end

After loading the API, call its .passScreenFunction method:
os.loadAPI("MoveAPI")
MoveAPI.passScreenFunction(screen) -- your screen function's name here inside the brackets

This way the API will have a reference pointing to the screen function in your program.

EDIT: Anyways, if the user didn't provide a screen function, that would mean a problem. Solution: call screen only if it's a function:
if type(screen) == "function" then screen() end

Sounds great I'll give it a try now.