167 posts
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?
1190 posts
Location
RHIT
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.
160 posts
Location
Netherlands
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
167 posts
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()
8543 posts
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?
1190 posts
Location
RHIT
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.
160 posts
Location
Netherlands
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
1190 posts
Location
RHIT
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.
167 posts
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.
1190 posts
Location
RHIT
Posted 09 July 2013 - 05:23 PM
This will work for what you want to do.
12 posts
Location
string expected, got nil
Posted 09 July 2013 - 05:55 PM
Wow. I wish you could do this stuff that easily in real life programming.
1190 posts
Location
RHIT
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.
1522 posts
Location
The Netherlands
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.htmlNo, 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)