Jump to content




Cant call a function from an API


  • You cannot reply to this topic
3 replies to this topic

#1 DELTA_12

  • Members
  • 4 posts

Posted 16 August 2015 - 12: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


#2 HPWebcamAble

  • Members
  • 933 posts
  • LocationWeb Development

Posted 16 August 2015 - 11:39 PM

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.

#3 TYKUHN2

  • Members
  • 210 posts
  • LocationSomewhere in this dimension... I think.

Posted 17 August 2015 - 03: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.

#4 HPWebcamAble

  • Members
  • 933 posts
  • LocationWeb Development

Posted 17 August 2015 - 04:12 AM

View PostTYKUHN2, on 17 August 2015 - 03:58 AM, said:

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 by HPWebcamAble, 17 August 2015 - 04:13 AM.






3 user(s) are reading this topic

0 members, 3 guests, 0 anonymous users