lua call function from a string with function name

后端 未结 5 1987
感动是毒
感动是毒 2020-12-13 05:19

Is it possible in lua to execute a function from a string representing its name?
i.e: I have the string x = \"foo\", is it possible to do x() ?

5条回答
  •  有刺的猬
    2020-12-13 05:44

    It sounds like you want to do an 'eval', which is supported in Lua like so:

    assert(loadstring(x))()
    

    You'll probably want to concatenate the "()" onto x first, though.

提交回复
热议问题