Hello all,
I am attempting to load a string from a file, and then coercing that string into the function name. For example, I would like to do something like:
function test_func()
print("yes!")
end
str = "test_func"
str()
So that end part, where I str() is obviously wrong, as a string is not a function. I am at a loss at how to do this. Does anyone have any tricks they can share?
function test_func()
print("yes!")
end
str = loadstring("test_func()") --notice the (), better syntax..
str()
--or..
str = "test_func()"
loadstring(str)() --however, this converts to a function every time
Robin beat me, but I hope this is going to help anyway
EDIT: though robin's suggestion of using assert is good..
function test_func()
print("yes!")
end
str = assert(loadstring("test_func()"))
str()
Explanation:
"test_func()" is the string that gets executed.
loadstring creates a function (or was it called "chunk" or something like that?), containing the code from a string
assert gives an error if something went wrong, otherwise passes the function to str