That’s awesome but I personally would not use these kind of methods in production as it’s a hacky solution. I will probably go with adding .insert function as @HugeCoolboy2007 suggested, although I am probably gonna look for different solution to it as well because I want to avoid name collisions (eg. local tbl = proxify({ insert = "cool" }) ).
I could make a module like ptable(proxy table) to avoid overwriting global built-in functions.
local ptable = {}
function ptable.insert(tbl, value, index)
getmetatable(tbl).__newindex(tbl, index, value, true)
end
return ptable
Also, could you explain what is going on with the .__newindex(...), what is that true for?
Fixed so that you don’t need to use getfenv New example.rbxm (6.4 KB)
You just need to paste this when requiring
local Proxy = require(script.Parent.Proxy);
local getmetatable = Proxy.getmetatable;
local setmetatable = Proxy.setmetatable;
local ipairs = Proxy.ipairs;
local pairs = Proxy.pairs;
local rawget = Proxy.rawget;
local rawset = Proxy.rawset;
I believe I solved the issue with name collision, I changed the __index meta method to this:
meta.__index = function(self, key)
local idx = tbl[key] or internalMethods[key]
local tableMethod = table[key]
if idx and type(idx) == "table" then
idx = proxify(idx)
end
return idx or (tableMethod and function(...)
return tableMethod(tbl, ...)
end) or nil
end
If the key exists on the table first, then the key value from the will be returned, otherwise it will search the internalMethods table and do the same thing. If it doesn’t exist in either of the tables, then it will search the table global for the key and return a function that runs the built-in function from the global.
Code used:
Summary
For some, reason I can’t copy all of the code on mobile
tbl.insert("test") -- removed the "nil" argument
print(tbl[1])
for key, value in tbl.Modes do
print(key, value)
end
That is a complicated one. What you will need to do is a custom print that loops through the table and creates a copy of a real table to print it. Any other method requires too much work, and it is just better to allow roblox to display the table.