I have a string, something like:
local func = "1 == 3"
How can I convert this into a function to execute and get a result from within another function? Like:
function CheckFunc(func)
local ret = functon() return func end
return ret
end
loadstring() is the function You're looking for :)
In your case it would be used like:
local func = loadstring("return (1==3)")
local func = "1 == 3"
function wrap(s)
return loadstring("(function() return "..s.." end)()")
end
funcWrapped = wrap(func)
if funcWrapped() then
print "One eqauls Three"
else
print "One doesn't equal Three"
end
Would output
One doesn't equal Three
NOTE: You should use @Kamiccolo's loadstring in place of mine within wrap
In Lua 5.1, you can use loadstring, as the other answers have already said:
local func = loadstring("return(1==3)")
In Lua 5.2, it's better to use load
local func = load("return(1==3)")