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

Issue calling API functions from inside the API

Started by MrObsidy, 20 September 2016 - 11:32 PM
MrObsidy #1
Posted 21 September 2016 - 01:32 AM
Hi :D/>

Okay, recently I am coding an API (Advanced Math API) and the problem I've ran into is the following:

I have a function -for the sake of simplicity let's just call it "func_1"- and another local function -naming it "func_2" and both are situated in
the "root" layer of that program (e.g. are not in another function, but can be called with MyAPI.func_1, func_2 is local), so the program
looks -more or less- like this:
Spoiler


--Test Program!--

function func_1(text1, text2)
  print(text1)
  func_2(text2)
end

function func_2(text_to_print)
  print(text_to_print)
end


Then we have another program, loading the API and running it's func_1 function:

Spoiler


--API Loader--

os.loadAPI("Path/to/api")

api.func_1("Hello", "World")


What I would expect the program to output is this:

Spoiler

Hello
World

But instead I get an error at

Test Program: 5: Attempt to call nil

I have no Idea what causes this at what fixes it, I tried making the function a global.. nothing worked, and I dont want the function func_1 saying something like

function func_1(text1, text2)
  print(text1)
  api.func_2(text2)
end
because that is slowing down the program and also making it able to call random "internal use only" functions in the program.


Any help?

Thanks in advance, Alex
Edited on 20 September 2016 - 11:34 PM
Lyqyd #2
Posted 21 September 2016 - 01:50 AM
Declare func2 as local and declare it above func1.
MrObsidy #3
Posted 21 September 2016 - 01:52 AM
Thats it? I would expect more of a fuzz. So just declare all internal-use functions first? That's the easiest fix ever!

Thanks alot!
Dog #4
Posted 21 September 2016 - 02:15 AM
Not just first, but also as local.
MrObsidy #5
Posted 21 September 2016 - 03:50 AM
Thats a really easy fix.
Thanks alot!
-Alex