如何确定lua全局值何时设置为nil?
我尝试过这个,但失败了__newindex仅在未设置键时触发如何确定lua全局值何时设置为nil?,lua,lua-table,Lua,Lua Table,我尝试过这个,但失败了__newindex仅在未设置键时触发 setmetatable(_G, { __newindex = function (table, key, value) print("key: ", key, "value: ", value) rawset(table, key, value) if key == "Config" then print("value: ", value) if value == nil then error(deb
setmetatable(_G, {
__newindex = function (table, key, value)
print("key: ", key, "value: ", value)
rawset(table, key, value)
if key == "Config" then
print("value: ", value)
if value == nil then
error(debug.traceback())
end
end
end})
以下工作代码:
local m = {}
local t = {
__newindex = function (table, key, value)
m[key] = value
if key == "Config" then
print("config value: ", value)
if value == nil then
error("Config is set to nil!!!!!!"..debug.traceback())
end
end
end,
__index = m}
setmetatable(_G, t)
Config = Config or {}
Config = nil
您需要为Lua 5.2+中的全局表(也称为
\u ENV
)设置代理表。是否可以显示更多详细信息?设置代理意味着仅使用专用表来捕获写入/读取事件。实际数据应该存储在其他一些表中。这样你就得到了总是被触发的新索引。具体怎么做-取决于你的需要,因为有很多方法可以组织数据。谢谢,我明白了。