function test(text) print(text) end func = "test" arg = "hello World"
Call Functions with string
Started by Wilma456, Jun 15 2016 01:22 PM
8 replies to this topic
#1
Posted 15 June 2016 - 01:22 PM
Why I can call the function test with the string func and the args arg?
#2
Posted 15 June 2016 - 02: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
#3
Posted 15 June 2016 - 02:31 PM
I want to write
func(arg)to call function test
#4
Posted 15 June 2016 - 04: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 by H4X0RZ, 15 June 2016 - 04:31 PM.
#5
Posted 15 June 2016 - 05: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).
#6
Posted 15 June 2016 - 05:25 PM
Dog, on 15 June 2016 - 05:02 PM, said:
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.
#7
Posted 16 June 2016 - 03:19 AM
H4X0RZ, on 15 June 2016 - 04:31 PM, said:
You could try to do
getfenv()[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...i/ScopeTutorial
#8
Posted 16 June 2016 - 11:52 AM
Thanks. But both methods work only with functions in the same script. if I load an API, it doesent work.
Method 1
Method 2
That does not work. How can I call function from another skript?
#9
Posted 16 June 2016 - 01: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 by valithor, 16 June 2016 - 01:15 PM.
2 user(s) are reading this topic
0 members, 2 guests, 0 anonymous users











