First off, this does not involve the love library in anyway, this is about lua.
Okay, so I have custom index for my metatable, which works fine and dandy. However, I'm trying to call the metatable's metatable and I can't seem to get it to work. I keep getting the error "stack overflow"
variables={}
test={}
test_mt={}
test_mt.__index=function(tbl,key)
if variables[key]~=nil then return variables[key]
else
local mt=getmetatable(tbl)
mt.__index(tbl,key)
end
end
test_mt2={}
test_mt2.__index=function(tbl,key)
print(tostring(tbl) .. "." .. key .. " has not been set in variables!")
return nil
end
setmetatable(test_mt,test_mt2)
setmetatable(test,test_mt)
print(test.stuff)
Drakkahn wrote:First off, this does not involve the love library in anyway, this is about lua.
Okay, so I have custom index for my metatable, which works fine and dandy. However, I'm trying to call the metatable's metatable and I can't seem to get it to work. I keep getting the error "stack overflow"
variables={}
test={}
test_mt={}
test_mt.__index=function(tbl,key)
if variables[key]~=nil then return variables[key]
else
local mt=getmetatable(tbl)
mt.__index(tbl,key)
end
end
test_mt2={}
test_mt2.__index=function(tbl,key)
print(tostring(tbl) .. "." .. key .. " has not been set in variables!")
return nil
end
setmetatable(test_mt,test_mt2)
setmetatable(test,test_mt)
print(test.stuff)
getmetatable(tbl) is test_mt, so your __index calls itself infinetely, causing a stack overflow.