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

[Help/Questin] API calling a function outside itself

Started by kelevra.1337, 16 July 2013 - 03:37 PM
kelevra.1337 #1
Posted 16 July 2013 - 05:37 PM
Hey guys,

*Pls dont mind the typo in the title :)/> *

Is there any way for a loaded API to call a Function that is in the script in wich the API is loaded?
I cant think of any better way to describe it so i try to show it on the problem i have:

This is a part of the API i wrote, the problem is now that i try to have a function call for a function that isnt in the API itself but is defined and executed in the main script. Here it is the update() function that is called after goToPos().


function gotoTable()
loadConfig()
loadTarget(true)
print(config["start"].." "..#target)
local i = config["start"]
while i <= #target do
  if target[i] == "home" then
	 -- go to deploy pos
  else
   goToPos(target[i][1],target[i][2],target[i][3],config["goto"])
   update()
   i = i + 1
   config["start"] = i
   save(config,"config")
   savePos()
  end
end
end

The main code could look like this:


local testVar = 1
os.loadAPI("TheAPI")
function update()
  print("yay i cant believe this works")
  testVar = testVar + 1
end
TheAPI.gotoTable()

Thanks in advance if someone knows the answer to this or give a hint to a better solution.
Edited on 16 July 2013 - 03:38 PM
OmegaVest #2
Posted 16 July 2013 - 05:46 PM
Uh, no. But you can make the API require that function. Just put "update" as a variable, and if it doesn't exist, error. If it does exist, then you can call "update()".

EDIT: At least, I think you can do this. It's been a while since I tried something this. . . complex.
Bubba #3
Posted 16 July 2013 - 05:46 PM
Uh, no.

That's incorrect. This is certainly possible. You'll have to modify the api to take an additional argument - the function that it needs to call.

For example, here is an API:

function doFunction(func, var)
  func(var)
end

And here is an example program:

local function saySomething(str)
  print(str)
end

os.loadAPI("example")

example.doFunction(saySomething, "Hello, world!")

This would output "Hello, world!"

If the above does not suit your needs, you can go another route: use the global environment. I can't imagine a situation where this would be preferred over the above, but just to cover all bases I'll talk about it anyway.

If you didn't know, the _G variable refers to a table that contains what we call the "global environment". The global environment is where every global variable and function is stored (e.g. anything that is not specifically initialized with the keyword "local"). So in the API, if you know the name of the function that needs to be called, it is as simple as this:

API:

function doStuff()
  _G["theFunction"]("Pass this var")
end

And the main program:

function theFunction(var) --#Note that this is a global function! If it were local, the api would not work!
  print(var)
end

os.loadAPI("example")
example.doStuff()

The program would output "Pass this var".


Keep in mind that, in general, there would be no reason to use the global environment in order to access functions that you write. If, however, you are trying to hook into another person's program without modifying their code, _G could possibly be the way to go.

Hope this helped :)/>
kelevra.1337 #4
Posted 16 July 2013 - 06:04 PM
Thanks for the advanced explanation, and yeah, it helped quite a lot :)/> . Ive got a lot of these kind of problems and i always used a kinda ugly method to work around it. Its good to know there is a proper solution :)/>
Edited on 16 July 2013 - 04:11 PM