not totally sure what you’re going for here, I’m also learning meta methods. basically you take a table and set a metatable, which can be the same table like so.
--creates table kat, and gives it an empty metatable
kat = setmetatable({}, {})
x = getmetatable(kat)
--slightly better
mouse = setmetatable({15}, mouse)
teal=setmetatable({},{__index=mouse})
print(teal[1])
--create table price, sub tables sell and buy, and make them their own metatable
price = {}
price.sell, price.buy = setmetatable({}, price.sell ), setmetatable({}, price.buy)
-- this won't work, it can't pass the table price inside to the keys for some reason
price = {sell=setmetatable({}, price.sell), buy=setmetatable({}, price.buy)}
index means that if the index doesn’t exist, then we check the metatable. if .__index is a table, then we check that table for the index. if it’s a function, run that function instead.
--here index is a table. if it doesn' find it in x, then it looks for the index in fallback
fallback = {[0]='zero'}
x = setmetatable({1,2,3}, fallback)
fallback.__index = fallback
print(x[0])--zero
print(x[5])--nil
--Index can also be a function.
g = {myIndex=function() return 'No index found' end}
x = setmetatable({10,20,30,[7]=70}, g)
g.__index = g.myIndex
print(x[5], x[6])
-- it keeps going! look in x[5], then fallback[5], then double_fallback[5]
double_fallback = {[5]='five'}
fallback = setmetatable({[0]='zero'}, double_fallback)
double_fallback.__index = double_fallback
x = setmetatable({1,2,3}, fallback)
fallback.__index = fallback
print(x[0])
print(x[5])