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

Cant call a function from an API

Started by DELTA_12, 16 August 2015 - 11:36 AM
DELTA_12 #1
Posted 16 August 2015 - 01:36 PM
Hello, i have a program that should call a function from a custom API but it displays just Attempt to index ? (a nil value) when it gets to that point. Weird thing is that my other APIs work perfectly fine. I also tried to call another function from that API but no functions from it work

code:

bpmaker

os.loadAPI("bpmakerbp")
os.loadAPI("gui")
function triggerButton(id)
os.shutdown()
end
function button1_click()
os.shutdown()
end
term.clear()
bpmakerbp.implement() --error here

bpmakerbp

os.loadAPI("gui")
os.loadAPI("bpmaker")
local lastx = 0
local lasty = 0
local e,s
local clicked = false
local run = true
local buttons = {{1,1,10,1,"Add button",'button1_click'} }
function getClicks()
e,s,lastx,lasty = os.pullEvent("mouse_click")
clicked = true
end
function draw()
--addbutton
for i=1, #buttons do
gui.drawBox(buttons[i][1],buttons[i][2],buttons[i][3],buttons[i][4],buttons[i][5])
if clicked then
if gui.intersect(buttons[i], {lastx, lasty}) then
bpmaker.triggerButton(buttons[i][6])
end
clicked = false
end
--end
end
end
function implement()
while true do
parallel.waitForAll(getClicks, draw)
end
HPWebcamAble #2
Posted 17 August 2015 - 12:39 AM
It looks like both files try to load the other as an API.
There shouldn't be a need to do that, one file is an API, the other is the program that uses the API.


Also, the second file is missing an 'end', at the bottom, but I assume that is just a copy/paste mishap.
TYKUHN2 #3
Posted 17 August 2015 - 04:58 AM
As HPWebcamAble said loading APIs is global among all programs that ever run until the API is either unloaded or the computer is restarted. Loading "gui" twice is redundant.
HPWebcamAble #4
Posted 17 August 2015 - 05:12 AM
Loading "gui" twice is redundant.

Well, that too, but I was refering to 'os.loadAPI("bpmaker")' in 'bpmakerbp' and 'os.loadAPI("bpmakerbp")' in 'bpmaker'

Maybe I should elaborate more:

It seems to me that you want to be able to run either file to run the program.
That isn't how an API works - Assume your API will never be run.
Usually, 100% of an API is functions - No code runs if the API is executed like a program.
(There are exceptions - like my Turtle Tweaks API)

It makes no sense to load 'bpmaker' as an API. Because it isn't supposed to be an API, its a program.

In regards to loading 'gui' in both files - don't.
Only load it in the one that actually calls the functions.
Edited on 17 August 2015 - 03:13 AM