** this post was originally a feature request but it has been solved **
It would be nice to have a metamethod to detect if a table is indexed AT ALL, not just if the value of the index was nil, would be pretty useful in some cases. I have to utilize extremely hacky workarounds to detect if a table was indexed regardless of it’s potential value.
Same goes for __newIndex
(let met also add that the metamethods wouldn’t necessarily have to override assignments/lookups, just detect them)
The hacky workaround in question:
local realTbl = {}--table that actually has values
setmetatable(
{},--table that will remain permanently empty
{
__index = function(tbl, index)
indexrawDetected(tbl, index)
return realTbl[index]
end
__newindex = function(tbl, index, value)
newindexrawDetected(tbl, index, value)
realTable[index] = value
end
},
)--this code might be wrong, i just wrote it while making this post, but i hope you get the idea
I’m basically creating a mock table that is nil for all values, so the __index and __newindex metamethods will always fire for lookups and assignments on that table.
This workaround isn’t fun because the normal table that has a metatable applied is now is not normal. For example, if you print the example table i used, it will be empty {} and not show the values that its supposed to have. Same goes for iterating it. You will have to use this table in a fundamentally different way.
A metamethod like __indexraw and __newindexraw would be nice.
or (__indexdetected and __newindexdetected)
(idk I’m not good at naming methods)
if anyone else has metamethod ideas you should put them below!! Its about time roblox gets some new metamethods, I’m sure there are plenty that would be useful.