2017-03-22 9 views
1

나는 이것을 시도하지만 실패한다. __newindex는 키가 설정되지 않은 경우에만 트리거됩니다. 작업 아래lua 전역 값을 nil로 설정하는 방법을 찾는 방법은 무엇입니까?

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}) 
+0

당신은 루아 5.2 이상에서'일명, 글로벌 테이블 _ENV'를 프록시 테이블을 설정해야합니다. – lhf

+0

자세한 내용을 표시 할 수 있습니까? –

+1

프록시 설정은 전용 테이블을 사용하여 쓰기/읽기 이벤트를 캡처하는 것을 의미합니다. 실제 데이터는 다른 테이블에 저장해야합니다. 그렇게하면 __newindex가 항상 실행됩니다. 이를 정확히 수행하는 방법은 데이터 구성 방법이 다양하기 때문에 필요에 따라 다릅니다. – Vlad

답변

1

코드는 :

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 

enter image description here