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

Calling API through a string

Started by Xenthera, 09 July 2013 - 02:59 PM
Xenthera #1
Posted 09 July 2013 - 04:59 PM
Having some minor troubles.

From the wiki as an example:


-- "apiTest" file
function foo(bar)
   print(bar)
end

-- "program" file
os.loadAPI("apiTest")
apiTest.foo("this is a test")

I want to call apiTest like this


-- "apiTest" file
function foo(bar)
   print(bar)
end

-- "program" file
os.loadAPI("apiTest")
x = "apiTest"
x.foo("this is a test")

I know you can do x = apiTest (no quotes) but I need to call it from a string. Any suggestions?
Bubba #2
Posted 09 July 2013 - 05:04 PM
You could do it like this:

os.loadAPI("apiTest")
x = apiTest --#No quotes: if you use quotes, x will just be a string.
x.foo("hello world!")

What's happening here:
os.loadAPI([string]) loads a file into the global environment under a table of the file name. So if my filename is "test", a call to os.loadAPI("test") will load that file under the global environment as the table test. So by saying:

x = apiTest

I am really just saying that x is a reference to the apiTest table that contains all of the functions.
nolongerexistant #3
Posted 09 July 2013 - 05:04 PM
I'm not sure why you would want to call it with a string, and I don't even think that's possible.

If you try to call if from a string, it will just try to call.. wel.. that string which will error
Xenthera #4
Posted 09 July 2013 - 05:10 PM
You could do it like this:

os.loadAPI("apiTest")
x = apiTest --#No quotes: if you use quotes, x will just be a reference to a string.

x.foo("hello world!")
I know you can do x = apiTest (no quotes) but I need to call it from a string.

What i'm trying to do is store a list of files in a table.
All the files have identical functions

files = {fs.list("fakeDirectory")}
files[i].genericFunction()
Lyqyd #5
Posted 09 July 2013 - 05:11 PM
You can do this, but I can nearly guarantee that you don't actually need to. This is likely a case of asking for x, but needing to know y. What's your full use case?
Bubba #6
Posted 09 July 2013 - 05:11 PM
I'm not sure why you would want to call it with a string, and I don't even think that's possible.

If you try to call if from a string, it will just try to call.. wel.. that string which will error

Yes, precisely. However, if you needed to use an api from a string reference for some reason, a good way to do so would be by using the _G table (global environment table).

For example:

local ref_string = "testAPI"
os.loadAPI(ref_string)
local ref_to_loaded_api = _G[ref_string]
ref_to_loaded_api.func() --#That is an unfortunate variable name :/

Here is a more in-depth tutorial on environments.

Edit: Sorry about my confusion. The example in this post is what you could use to do what you want.
nolongerexistant #7
Posted 09 July 2013 - 05:12 PM
You could do it like this:

os.loadAPI("apiTest")
x = apiTest --#No quotes: if you use quotes, x will just be a reference to a string.

x.foo("hello world!")
I know you can do x = apiTest (no quotes) but I need to call it from a string.

What i'm trying to do is store a list of files in a table.
All the files have identical functions

files = {fs.list("fakeDirectory")}
files[i].genericFunction()

If you want that, you could just do:


local files = {fs.list("dir")}

for k, v in pairs(files) do
   os.loadAPI(files[v])
end

files[1].someFunction()

That's untested trough
Bubba #8
Posted 09 July 2013 - 05:15 PM
If you want that, you could just do:


local files = {fs.list("dir")}

for k, v in pairs(files) do
   os.loadAPI(files[v])
end

files[1].someFunction()

That's untested trough

That would not work. fs.list only returns the paths to the files, not references to the apis themselves.
Xenthera #9
Posted 09 July 2013 - 05:20 PM
What i'm trying to do is store a list of files in a table.
All the files have identical functions which I need to call

files = {fs.list("fakeDirectory")}
for i = 1, #files do
files[i].genericFunction()
end

That's about all I need to do. Can't explain much more than that.

But because fs.list()'s table contains strings, I can't exactly do that.
Bubba #10
Posted 09 July 2013 - 05:23 PM
This will work for what you want to do.
bwhodle #11
Posted 09 July 2013 - 05:55 PM
Wow. I wish you could do this stuff that easily in real life programming.
Bubba #12
Posted 09 July 2013 - 06:07 PM
Wow. I wish you could do this stuff that easily in real life programming.

What is real life programming and how is Lua not?
Lua can be used in a regular environment too, not just in ComputerCraft.
Engineer #13
Posted 09 July 2013 - 06:17 PM
Wow. I wish you could do this stuff that easily in real life programming.

What is real life programming and how is Lua not?
Lua can be used in a regular environment too, not just in ComputerCraft.
To add to that, here is even a download! http://www.lua.org/download.html
No, but seriously, Lua is still used for certain games and probably everything else.. So that makes it a programming language.

However, Lua is a scripting language, and you cannot make another programming language with it. However, you can with C, Java, and whatnot. Since those are not scripting languages. I dont know the proper name, but C++ and Java are OOP (object-orientated programming)