function test(text)
print(text)
end
func = "test"
arg = "hello World"
This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
Call Functions with string
Started by Wilma456, 15 June 2016 - 12:22 PMPosted 15 June 2016 - 02:22 PM
Why I can call the function test with the string func and the args arg?
Posted 15 June 2016 - 03:24 PM
I'm not entirely sure what your question is, but here's my best guess at an answer.
function example(input)
print(input)
end
example("this is a test") --#calling the function
word = "second test"
example(word) --#passing a variable
Posted 15 June 2016 - 03:31 PM
I want to write
func(arg)
to call function testPosted 15 June 2016 - 05:31 PM
Umm… You could achieve that with a lot of metatable magic I think. Or, actually… You can't because the string metatable is "hidden".
You could try to do
You could try to do
getfenv()[func](arg)
Edited on 15 June 2016 - 03:31 PM
Posted 15 June 2016 - 06:02 PM
function test(text)
print(text)
end
func = test
arg = "hello World"
You were setting func to the string "test". This sets func to equal test so you can call func(arg).
Posted 15 June 2016 - 06:25 PM
function test(text) print(text) end func = test arg = "hello World"
You were setting func to the string "test". This sets func to equal test so you can call func(arg).
I believe the OP want's to call a function using a string, and not a pointer. Although your approach would "fix" the problem too.
Posted 16 June 2016 - 04:19 AM
You could try to dogetfenv()[func](arg)
This seems correct to me. But I'd instead suggest:
local funcs = {}
funcs.test = function(text)
print(text)
end
local func = "test"
local arg = "hello World"
funcs[func](arg)
… as dumping everything into the environment table (as globals) is messy.
http://lua-users.org/wiki/ScopeTutorial
Posted 16 June 2016 - 12:52 PM
Thanks. But both methods work only with functions in the same script. if I load an API, it doesent work.
That does not work. How can I call function from another skript?
Method 1
--Script
os.loadAPI("/testapi")
func = "testapi.test"
arg = "hello World"
getfenv()[func](arg)
--testapi.test works
--testapi
function test(text)
print(text)
end
Method 2
--skript
os.loadAPI("/testapi")
func = "test"
arg = "Hello World"
funcs[func](arg)
--testapi
funcs = {}
funcs.test = function(text)
print(text)
end
Posted 16 June 2016 - 02:03 PM
Both of the methods have a similar problem when trying to use an api
It would need to be getfenv()["testapi"]["test"](arg)
If you wanted to use an api with the first method you would probably want to do something like:
For the second method you would need to use testapi.funcs[func](arg)
The problem you are experiencing is due to how variables/functions are loaded into apis.
It would need to be getfenv()["testapi"]["test"](arg)
If you wanted to use an api with the first method you would probably want to do something like:
func = "testapi.test"
arg = "hello world"
local function callFunc(func,arg)
local fenv = getfenv()
for word in func:gmatch("[^ %.]+") do
fenv = fenv[word]
end
return fenv(arg)
end
callFunc(func,arg)
For the second method you would need to use testapi.funcs[func](arg)
The problem you are experiencing is due to how variables/functions are loaded into apis.
Edited on 16 June 2016 - 12:15 PM