Jump to content




[Help/Questin] API calling a function outside itself


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

#1 kelevra.1337

  • Members
  • 31 posts

Posted 16 July 2013 - 04: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 by kelevra.1337, 16 July 2013 - 04:38 PM.


#2 OmegaVest

  • Members
  • 436 posts

Posted 16 July 2013 - 04: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.

#3 Bubba

    Use Code Tags!

  • Moderators
  • 1,142 posts
  • LocationRHIT

Posted 16 July 2013 - 04:46 PM

View PostOmegaVest, on 16 July 2013 - 04:46 PM, said:

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 :)

#4 kelevra.1337

  • Members
  • 31 posts

Posted 16 July 2013 - 05: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 by kelevra.1337, 16 July 2013 - 05:11 PM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users