WaffloidJoin Date: 2011-07-14 Post Count: 1606 |
setfenv(1,setmetatable({},{}))
Am I the only one who finds that 1 line of code (which works and doesn't error) absolutely amazing? You can track new variables or make variables do weird things when they're referenced (e.g. kek = 5 print(kek), prints 23) |
|
cntkillmeJoin Date: 2008-04-07 Post Count: 49450 |
Once you have a basic understanding of metatables and environment, you'll realize it's really not that special. Especially since local variables are not stored in the environment and most people use locals. |
|
cntkillmeJoin Date: 2008-04-07 Post Count: 49450 |
environments* |
|
|
If you like that, check out my models Class Module and Function Environment GO BOOM!!!! cuz they are pretty neat. |
|
|
well.... to me it looks like it appends a blank metatable to a blank table, and sets the table as the environment... oh wait actually
that could be good
we can troll people and make operators do the opposite thing
https://www.youtube.com/watch?v=onhFH7jpq2c |
|
Agluk15Join Date: 2016-02-13 Post Count: 193 |
@CpModerator
Please tell. |
|
|
nvm you cant
i mean you could but you would need the debug.setmetatable, not the normal setmetatable
https://www.youtube.com/watch?v=onhFH7jpq2c |
|
Agluk15Join Date: 2016-02-13 Post Count: 193 |
Oh, because I was skeptical on reversing + and - and whatnot. |
|
|
i forgot that setmetatable was restricted to tables only
but debug.setmetatable isnt
with debug.setmetatable, you can give numbers a metatable, which affects all numbers
so then thats how you give numbers a metatable
https://www.youtube.com/watch?v=onhFH7jpq2c |
|
Agluk15Join Date: 2016-02-13 Post Count: 193 |
This would be cool:
debug.setmetatable(+,-)
debug.setmetatable(-,+)
However that doesn't work because you can't pass a function a syntactical argument, and after the first line + is - so - would still be -. |
|
|
no
local x = 5
debug.setmetatable(x,{_add=function(a,b) return a-b end})
print(x+3)
-- output should be 2 if using vanilla lua
https://www.youtube.com/watch?v=onhFH7jpq2c |
|
cntkillmeJoin Date: 2008-04-07 Post Count: 49450 |
-.- why do people talk about stuff they don't understand |
|
|
idk why it shows 8 though, but i read about debug.setmetatable a long time ago so maybe its for a previous version
https://www.youtube.com/watch?v=onhFH7jpq2c |
|
cntkillmeJoin Date: 2008-04-07 Post Count: 49450 |
No, it's not for a previous version lol you just don't know how it works |
|
WaffloidJoin Date: 2011-07-14 Post Count: 1606 |
the main reason i found this interesting is because setmetatable(getfenv(),{}) errors, so i thought it was impossible till' i did that |
|
cntkillmeJoin Date: 2008-04-07 Post Count: 49450 |
It only errors because setfenv expects a function (or afunction based on the call stack), getfenv() returns a table. |
|
|
And the metatable of the environment (IIRC) is locked.
Also, if you swapped "+" and "-", wouldn't they end up doing the same thing at the end? Either that or a stack overflow. Because indexing directly in "__index" results in a stack overflow, so ye, have fun. |
|
cntkillmeJoin Date: 2008-04-07 Post Count: 49450 |
only on roblox is the global env of scripts locked. and you can't "swap" + and -, and I don't see how on earth that would lead to a stack overflow.
indexing in __index doesn't cause a stack overflow, it's only when you index that same table that has that __index metamethod in the first place. |
|
|
yes cnt.
To "swap" "+" and "-", you might so something like this:
```
debug.setmetatable(0, {
__add = function(A,B) return A - B end,
__sub = function(A,B) return A + B end
})
print(5 + 5)
```
~That _would_ cause a stack overflow~.
Nevermind, it doesn't. huh.
~ Shakespeare's Soggy Left Nut |
|
|
that code doesnt even work for me
https://www.youtube.com/watch?v=onhFH7jpq2c |
|
cntkillmeJoin Date: 2008-04-07 Post Count: 49450 |
No it wouldn't, learn metatables. Metamethods are FALLBACKS.
FALLBACKS: `an alternative plan that may be used in an emergency.
"teaching was a last resort, a fallback"`
|
|
|
I will swear that has worked before..
~ Shakespeare's Soggy Left Nut |
|
|
i literally made a file that did the exact same thing as that and it didnt work
wait i used lua 5.1.4 so it should work though
https://www.youtube.com/watch?v=onhFH7jpq2c |
|
|
When anyone asks about metatables in the future, this will be my go-to article explaining them, just because of all the wrong information.
|
|
|
Cnt you're right, they're fallbacks. I swear I remember somewhere reading or testing to the effect that you could do operator overloading with them.. |
|