Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Difference between metatable usage with newproxy and tables?

newproxy

I was looking up newproxy on the RobloxWiki and I noticed it creates a new userdata.

Now, my question is, what is the point of attaching specific meta-methods such as __index and __call, etc when you can attach it to a table aswell with the same functionality?

Why would you use a metatable with userdata values with new proxy instead of just using an array?

Why

local proxyValue = newproxy(true)
getmetatable(proxyValue).__index = {seraplz= function() print("@sera plz") end}
proxyValue.seraplz()

Vs

local meta = {
	__index = {seraplz = "@sera plz"}
}
local array = setmetatable({},meta)
print(array.seraplz)

Other than the obviously shorter way of doing the meta-table.

What are some strong advantages and disadvantages for both?

1 answer

✓ Accepted answer

You can't use a Userdata returned by newproxy with setmetatable or rawset. In effect - they are perfect proxies. The entire point of a proxy is indirect access, which you can read about here.

The downside is that newproxyhas been removed from Lua as of version 5.2, so don't rely on it working. Supposedly new features have made it redundant, but I have yet to actually figure out what they are. (ROBLOX currently uses Lua 5.1 - but I expect an upgrade eventually.)

2 comments

0

I'm still slightly confused. Can you provide an example of how it is used for indirect access?

0

The article I linked gives a better overview of it than I can: http://www.lua.org/pil/13.4.4.html

This discussion is preserved as it was. New answers, comments, and votes are closed.