loadstring()在Lua中包含一个函数

问题描述 投票:0回答:1

说,我有此备忘录行文本:

Memo.Lines.Text = 'print('Hello World')
local test = Memo.Lines.Text
loadstring(test) ()

--result: Hello World

现在,如果是这样的话:

Memo.Lines.Text = [[
function playTest()
 local a = 10
 local b = 20
 local c = a + b
 print('This is a test of a math calculation: ')
 print(a..' + '..b..' is '..c)
end
]]

local test = Memo.Lines.Text
loadstring(test) ()
playTest()

-- result: error attempt to call a nil value 'playTest'

如何从字符串/以上面的备注行文本形式返回函数?

lua
1个回答
0
投票

糟糕,没关系,这是答案

loadstring(test) (PlayTest)
playTest()
© www.soinside.com 2019 - 2024. All rights reserved.