大佬教程收集整理的这篇文章主要介绍了动态 – lua调用函数从一个带函数名的字符串,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
如果是,语法是什么?
x='foo' _G[x]() -- calls foo from the global namespace
如果另一个表中的函数,如x =’math.sqrt’,则需要使用loadString()(或遍历表)。
如果使用loadString(),您将不仅需要使用椭圆(…)来附加括号以允许参数,还可以向前添加返回值。
x='math.sqrt' print(assert(loadString('return '..x..'(...)'))(25)) --> 5
或走桌子:
function findfunction(X) assert(type(X) == "String") local f=_G for v in x:gmatch("[^%.]+") do if type(f) ~= "table" then return nil,"looking for '"..v.."' expected table,not "..type(f) end f=f[v] end if type(f) == "function" then return f else return nil,"expected function,not "..type(f) end end x='math.sqrt' print(assert(findfunction(X))(121)) -->11
以上是大佬教程为你收集整理的动态 – lua调用函数从一个带函数名的字符串全部内容,希望文章能够帮你解决动态 – lua调用函数从一个带函数名的字符串所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。