local ents = {
GetLocalPlayer = function()
local tbl = {
localplayer = {"Ava", "1", {213,234,234}},
GetIndex = function(self)
return self.localplayer[2]
end,
}
setmetatable(tbl, getmetatable(tbl.localplayer))
return tbl
end
}
local function main()
print(ents.GetLocalPlayer()[2])
endmain()打印返回零。但是,如果我要执行ents.GetLocalPlayer():GetIndex(),它将返回1。
这样做的想法是,如果我不执行诸如GetIndex()之类的操作,则默认返回值为localplayer。
发布于 2021-12-06 00:37:25
表没有默认的元可用,这就是为什么您的getmetatable调用返回零。为了执行任何操作,setmetatable的第二个参数必须是至少有一个元方法的表。(__index是最常见的元方法。)
解决方案是将getmetatable(tbl.localplayer)更改为{__index = tbl.localplayer}。
https://stackoverflow.com/questions/70237789
复制相似问题