generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: Metamethods for the lazy

Previous Thread :: Next Thread 
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
03 Feb 2015 09:46 PM
Too lazy to create em yourself? Here you go:

do

local oldrawset = rawset
function rawset(t, k, v)
local meta = getmetatable(t)
if type(meta) == "table" and meta.__rawset then
return meta.__rawset(t, k, v)
else
return oldrawset(t, k, v)
end
end

local oldrawget = rawget
function rawget(t, k)
local meta = getmetatable(t)
if type(meta) == "table" and meta.__rawget then
return meta.__rawget(t, k)
else
return oldrawget(t, k)
end
end

local oldpairs = pairs
function pairs(t)
local meta = getmetatable(t)
if type(meta) == "table" and meta.__pairs then
return meta.__pairs(t)
else
return oldpairs(t)
end
end

local oldipairs = ipairs
function ipairs(t)
local meta = getmetatable(t)
if type(meta) == "table" and meta.__ipairs then
return meta.__ipairs(t)
else
return oldipairs(t)
end
end

local oldnext = next
function next(t, i, arg)
local meta = getmetatable(t)
if type(meta) == "table" and meta.__next then
return meta.__next(t, i, arg)
else
return oldnext(t, i, arg)
end
end

local oldprint = print
function print(...)
local realargs = {}
for I,v in pairs({...}) do
local meta = getmetatable(v)
if type(meta) == "table" and meta.__print then
table.insert(realargs, meta.__print)
else
table.insert(realargs, v)
end
end
return print(unpack(realargs))
end

end


Before this post I had 6666 post count!

--You can easily extend this for other functions too.
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
03 Feb 2015 09:48 PM
print already calls __tostring :(
Report Abuse
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
03 Feb 2015 09:49 PM
@cnt But what if I wanted the tostring function to return something, but print to print something different?
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
03 Feb 2015 09:56 PM
how the hell do I have 29K posts and still this young
Report Abuse
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
03 Feb 2015 09:57 PM
Join Date: 2008

That's how.
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
03 Feb 2015 10:04 PM
I started foruming like, 2013 I think
Report Abuse
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
03 Feb 2015 10:10 PM
Searches for posts on SH earlier than 2013 made by you*
Report Abuse
vacharya is not online. vacharya
Joined: 06 Jan 2011
Total Posts: 511
03 Feb 2015 10:18 PM
#stalker for laif
Report Abuse
ForeverDev is not online. ForeverDev
Joined: 04 Oct 2008
Total Posts: 13300
03 Feb 2015 10:32 PM
how old are you @cnt
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
03 Feb 2015 10:39 PM
e^2.77258872224
Report Abuse
128GB is not online. 128GB
Joined: 17 Apr 2014
Total Posts: 8056
03 Feb 2015 10:47 PM
(e^2.77258872224) + 1
Report Abuse
Voiliax is not online. Voiliax
Joined: 05 Nov 2009
Total Posts: 15554
03 Feb 2015 11:03 PM
Just wanted to point out that your way to check for a locked metatable is not the best way because you can set a __metatbale metamethod to any other table.

The right way is by trying to set it again with setmetatable and check if it errors or not using pcall.

if pcall(setmetatable, tab, getmetatable(tab)) then
-- not a locked metatable
end
Report Abuse
ForeverDev is not online. ForeverDev
Joined: 04 Oct 2008
Total Posts: 13300
03 Feb 2015 11:26 PM
(e^2.77258872224) - 1

:(
Report Abuse
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
04 Feb 2015 03:09 PM
@Voil I know that, but if they want to send us to another table to proxy a metatable, so be it, I'll use whatever's in that. Just like how they expect in their code.
Report Abuse
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
06 Feb 2015 06:53 AM
Bump, because I know people who wouldn't know how to change the behaviour of rawset/rawget
Report Abuse
Voiliax is not online. Voiliax
Joined: 05 Nov 2009
Total Posts: 15554
06 Feb 2015 06:59 AM
Yeah it won't do much difference since it's just a custom metamethod but that was just for knowledge.
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image