在Lua脚本中捕获并列出全局变量 [英] Capture and list Globals in Lua Script
问题描述
如何遍历整个脚本来捕获所有全局变量的列表.
How does one iterate thru an entire script capture a list of all globals.
我知道_G是环境表,它包含所有全局变量,但实际上包含所有全局变量.我只想要脚本中的全局变量
I understand that _G is the environment table and it holds all globals, but it hold literally ALL globals. I want the globals in my script only
推荐答案
除了_G
本身是全局变量之外,_G
是运行时表(因此只能在特定运行时的某个时刻列出) .没错,它将列出由脚本以及Lua状态初始化和使用的其他脚本设置的全局变量.
Other than _G
being a global variable itself, _G
is a runtime table (and so could only be listed at some point during a particular run). You're right that it would list globals that are set by the script as well as by the Lua state initialization and other scripts that are used.
找到编译器将作为全局变量绑定的变量标识符的一种方法是使用luac
命令行工具:
One way to find the variable identifiers that the compiler would bind as globals is to use the luac
commandline tool:
luac -l script.lua | grep [SG]ETGLOBAL
第二列给出行号.最后一列(注释)提供了标识符.
The second column gives the line number. The last column (comment) gives the identifier.
这篇关于在Lua脚本中捕获并列出全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!