我希望开发一款使用 Lua 作为脚本语言的游戏。
我有以下声明:
foe:add_component 'render_info':set_scale(0.5)
我明白
:
意味着调用父对象将作为第一个参数传递给被调用方法。但在这种情况下,render_info
后面没有括号。 ;有什么似乎是一个裸字符串文字,上面有一个被调用的方法,这似乎真的没有意义。我也理解元表和元方法的概念,其中
add_component
可能是一个例子。但是,我发现没有可以覆盖的“空白”运算符。我想知道我是否遗漏了 Lua 语法方面的重要内容。上面的代码语句是什么意思?每个部分是什么,它在做什么?
最佳答案
如果函数只有一个参数并且该参数是文字字符串或表构造函数,则可以在函数调用时省略括号。
引用 lua 引用手册:
A call of the form f{fields} is syntactic sugar for f({fields}); that is, the argument list is a single new table. A call of the form f'string' (or f"string" or f[[string]]) is syntactic sugar for f('string'); that is, the argument list is a single literal string.
所以这相当于你的电话:
foe:add_component('render_info'):set_scale(0.5)
关于lua - 函数名后没有括号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36126249/