ZeroBrane的分析:在库对象上出现“未知的全局变量”

我开始在 ZeroBrane Studio 中使用 Project / Analyze 来进行 Marmalade Quick 项目,我发现它对于让我了解全局与局部变量有很大的帮助。

然而,这里有一个恼人的问题:它会将库文件标记为未定义的全局变量:

.../resources/main.lua(11): first use of unknown global variable 'system'
.../resources/main.lua(12): first use of unknown global variable 'json'
.../resources/main.lua(13): first use of unknown global variable 'device'
.../resources/main.lua(14): first use of unknown global variable 'physics'
.../resources/main.lua(15): first use of unknown global variable 'color'
.../resources/main.lua(16): first use of unknown global variable 'director'
.../resources/main.lua(17): first use of unknown global variable 'key'

有没有办法去掉这些库的警告(或特定变量名的警告)?

作为部分解决方案,我开始在文件开头添加以下内容-这并不解决问题,只是把它们提到了顶部:

local system = system
local json = json
local device = device
local physics = physics
local color = color
local director = director
local key = key

顺便问一下,这会影响性能吗?理论上,我正在将全局的 director 转换为局部的 director,这应该更快...

点赞
用户1442917
用户1442917

我目前没有办法关闭特定变量的警告,但是有一个可以使用的解决方法来抑制警告。你可以使用"local director = _G.director" 或者 "local director = rawget(_G, "director")"来代替"local director = director"。

在更快的访问方面,使用局部变量比表访问更快(请参见Lua性能贴士的第3页),但是你可能需要在循环中运行大量调用才能看到区别。请注意,LuaJIT会进行自己的优化,这可能会改变性能影响。

2015-04-06 04:08:27